# Solutions for Exercise 3: Introduction to # Descriptive and Inferential Statistics with R # 1 # cardat.csv dat<-read.csv(file.choose()) head(dat) names(dat) # 2 mean(dat$purc) tapply(dat$twoyr, dat$type, mean, tr=.2) tapply(dat$oneyr, list(dat$type,dat$sex), mean) tapply(dat$twoyr, dat$type, describe, trim=.2) describeBy(dat$twoyr,dat$type,trim=.2) describeBy(dat$oneyr, list(dat$type,dat$sex),trim=.2) hist(dat$purc) # 3 library(psych) describe(dat) # 4 tab<-table(dat$type) barplot(tab) # 5 leveneTest(dat$twoyr,dat$sex) dat2<-subset(dat,type=="car" | type=="truck") library(car) leveneTest(dat$twoyr[dat$type=="car"|dat$type=="truck"],dat$type[dat$type=="car"|dat$type=="truck"]) leveneTest(dat2$twoyr,dat2$type) hist(dat$twoyr[dat$sex=="male"]) hist(dat$twoyr[dat$sex=="female"]) tapply(dat$twoyr,dat$sex,shapiro.test) hist(dat2$twoyr[dat2$type=="car"]) hist(dat2$twoyr[dat2$type=="truck"]) tapply(dat$twoyr,dat$type,shapiro.test) t.test(twoyr~sex, data=dat) t.test(twoyr~type, data=dat2) #or t.test(dat$twoyr[dat$type=="car"],dat$twoyr[dat$type=="truck"]) library(WRS2) yuen(twoyr~sex,data=dat) yuen(twoyr~type,data=dat2) # 6 t.test(dat$purc,dat$oneyr, paired=TRUE) # 7 mod1<-lm(twoyr~purc,data=dat) summary(mod1) cutoff <- 4/(length(dat$twoyr)-1) plot(mod1, which=4, cook.levels=cutoff) dat3<-dat[-26,] mod2<-lm(twoyr~purc,data=dat3) summary(mod2) # 8 mod3<-lm(twoyr ~ sex + purc, data=dat) summary(mod3)