R - ANOVA

From Training Material
Jump to navigation Jump to search

Single Factor ANOVA

Is there a difference in language proficiency (LP) as a function of number of hours of weekly practice?

 lp <- read.table("http://training-course-material.com/images/0/0a/Anova-signlefactor.txt",h=T)
 lp

 summary(lp)

 # Calculate the standard deviation for each column
 sapply(lp,sd)
 
 # Changes the format to a factor column
 slp <- stack(lp)

 names(slp) <- c("Test.Result","HoursWeeklyPractise")
 # Create a model
 m <- aov(slp$Test.Result ~ slp$HoursWeeklyPractise)
 m

 # Validate the model
 summary(m)

Two Factors with Replication

Is there a difference in satisfaction level (SAT) over four training sessions (training-Quarter 1 or TQ1, etc.) for 20 unemployed men and women undergoing quarterly training?"

sat <- read.table("http://training-course-material.com/images/e/ed/Anova-twofactors.txt",
                h=T, fill=T, row.names=NULL)
names(sat) <- c("Gender","TQ1","TQ2","TQ3","TQ4")
sat
attach(sat)
str(sat)
s <- data.frame()
s <- rbind(s,cbind(Gender,Sat=TQ1,Quarter="1"))
s <- rbind(s,cbind(Gender,Sat=TQ2,Quarter="2"))
s <- rbind(s,cbind(Gender,Sat=TQ3,Quarter="3"))
s <- rbind(s,cbind(Gender,Sat=TQ4,Quarter="4"))
detach(sat)
rm(sat)
s
s$Gender <- as.factor(s$Gender)
s$Quarter <- as.factor(s$Quarter)
s$Sat <- as.integer(s$Sat) 
attach(s)
str(s)
m <- aov(Sat ~ Gender * Quarter)
summary(m)


Without Replication

sat <- read.table(skip=1,h=T, fill=T, row.names=NULL,na.strings=" ",sep="\t",
                 file=    "http://training-course-material.com/images/0/0a/Anova-two-factors-without-replications.txt")
sat
summary(sat)
s <- data.frame()
s <- rbind(s,cbind(Party="Tory",Sat=sat[1:10,c("Urban")],Area="Urban"))
s <- rbind(s,cbind(Party="Tory",Sat=sat[1:10,c("Rural")],Area="Rural"))

s <- rbind(s,cbind(Party="Labour",Sat=sat[11:20,c("Urban")],Area="Urban"))
s <- rbind(s,cbind(Party="Labour",Sat=sat[11:20,c("Rural")],Area="Rural"))
s
s$Party <- as.factor(s$Party)
s$Area <- as.factor(s$Area)
s$Sat <- as.integer(s$Sat) 
attach(s)
str(s)
plot(s)
m <- aov(Sat ~ Party + Area)
summary(m)