#Input data sample1 <- c(22,21,19,23,24,22) sample2 <- c(19,17,19,18) sample3 <- c(23,26,19,18,20) alldata <- c(sample1,sample2,sample3) #Category Totals sum1 <- sum(sample1) sum2 <- sum(sample2) sum3 <- sum(sample3) allsum <- sum(alldata) #Optional. If you don't want to count your sample sizes manually, you can automate it using the length() function. n1 <- length(sample1) n2 <- length(sample2) n3 <- length(sample3) totaln <- n1 + n2 + n3 #SS(between) ssbetween <- (sum1^2)/n1 + (sum2^2)/n2 + (sum3^2)/n3 - ((sum1 + sum2 + sum3)^2)/totaln ssbetween #Also, since we will use it later, you can write sumx <- sum1 + sum2 + sum3 #SS(total) sstotal <- (sum(alldata^2)) - ((sum(alldata))^2)/totaln sstotal #SS(within) sswithin <- sstotal - ssbetween sswithin #Mean Squares msfactor <- ssbetween/(3-1) mserror <- sswithin/(totaln-3) msfactor mserror #F test statistic fstat <- msfactor/mserror fstat #p value (always right tailed). Note, df=k-1 for the numerator, and df=n-k for the denominator 1-pf(fstat,3-1,totaln-3) #Optional. Critical value qf(.95,3-1,totaln-3) mean(sample1) mean(sample2) mean(sample3)
#Input data sample1 <- c(72,84,77,80,81) sample2 <- c(83,73,84,81) sample3 <- c(80,78,84,81,86,79,82) alldata <- c(sample1,sample2,sample3) #Specify group membership for each x in "alldata" #This says, the first 5 things belong to group 1, the next 4 things in group 2, and so on group <- c(rep(1,5),rep(2,4),rep(3,7)) #Put the data into a special object called a ``data frame" #This is organized like a table, rather than just a list data <- data.frame(y = alldata, group = factor(group)) #We create a linear model first and then apply the anova() function to it fit <- lm(alldata ~ group, data) anova(fit)
#Input data sample1 <- c(180,183,172,178,169,179,178) sample2 <- c(185,181,180,179,164,165,180,178) sample3 <- c(170,183,172,175,164,170,176,167,166) alldata <- c(sample1,sample2,sample3) #Specify group membership for each x in "alldata" #This says, the first n1 belong to group 1, the next n2 things in group 2, and so on #We get nj = length(samplej) group <- c(rep(1,length(sample1)),rep(2,length(sample2)),rep(3,length(sample3))) #Put the data into a special object called a ``data frame" #This is organized like a table, rather than just a list data <- data.frame(y = alldata, group = factor(group)) #We create a linear model first and then apply the anova() function to it fit <- lm(alldata ~ group, data) anova(fit)