STAT1201 CHEAT SHEET DATA MANIPULATION Essential steps Library(lattice) DATANAME <- read.csv("C:/Users/LILI/Desktop/data.csv") Finding means, sd's, interquartile ranges, etc aggregate(response_variable ~ explanatory_variable, data = data, mean or sd or IQR) Arranges the response variable and explanatory variable into a two-way table that shows the statistic you put in the last space (e.g. mean). Perfect to split the means of groups. mean(data$quantitative_variable) Replace mean with sd, median, IQR to find different statistics for a single variable. as.factor(variable) Useful to force R to treat a quantitative variable as a categorical one. Use it in ANOVA so that one-way ANOVA doesn't become regression ANOVA name = lm( ) Simplifies a linear model down to a name we can use whenever Change = data$column-data$column data$new_variable = data$variable - data$variable2 We can add data to the csv that we have read by defining new variables. Makes a new column for a change in what you want (could use -,+,*) Could make changes to the original excel sheet as well HYPOTHESIS TESTING Binomial probabilities dbinom(x=6, size=10, p =. 3) gives P(X = 6) for a Binomial pbinom(6, 10, .3) pbinom(x=6, size=10, p =. 3) gives P(X ? 6) When finding the probability using X, mean/probability, standard deviation 1 - pbinom(6, 10, .3) 1 - pbinom(x=6, size=10, p =. 3)
gives P(X?7) sum(dbinom(7:10, size = 10, prob = 0.3)) gives a p value for a value out of a total. (X?7) when given sample/n, probability, second probability to find probability z values and significance qnorm(0.975) gives the z statistic corresponding to quantile 0.975 on the Normal distribution pnorm(1.96) when given mean, standard deviation, second probability gives the p value that goes with the z statistic 1.96 on the Normal distribution Finding t values for quantiles 1-pt( t-statistic, df = Gives the p value to the one sided t-statistic, given the degrees of freedom When you have sample/n, mean, standard deviation pt( t-statistic, df = 1 finds the p value for a two sided t test qt( quantile, df = 1 Gives the standard deviations needed for that confidence quantile Used to help find the margin of error, t *= qt(). Margin of error = t*standard error (standard error = sd/square root n t test t.test (response variable ~ explanatory variable, data = data, alternative = 'less' or 'greater') The explanatory variable must have only two choices. Any more and you should refer to ANOVA. This is the Welch approximation t test. For pooled t test, use 'var.equal = TRUE'. The default t test is two sided which will find the p value for a difference either positive or negative.
For a one-tailed test that finds the p value of either greater or less than divide by 2 or use either 'less' or 'greater' depending on the alternative hypothesis. Remember that groups are arranged alphabetically, first group - second group, so greater or less may change. One-way ANOVA / regression ANOVA finds degrees of freedom, sum square, mean square, f value, p value summary(aov(response variable ~ explanatory variable, data =