# Session 7 # Q1 View(mtcars) ?mtcars # am Transmission (0 = automatic, 1 = manual) names(mtcars) t.test(mpg~am, data=mtcars) boxplot(mpg~am, data=mtcars, main="0 = automatic, 1 = manual") aov1 <- aov( mpg~factor(gear), data=mtcars) summary(aov1) model.tables(aov1, "means") boxplot(mpg~factor(gear), data=mtcars) summary(aov( mpg~gear, data=mtcars)) # forgot the factor() - what is it doing? summary(lm( mpg~gear, data=mtcars)) # note same F test plot(mpg~gear, data=mtcars) abline( lm( mpg~gear, data=mtcars), col="tomato", lwd=2 ) # Q2 myformula <- formula( mpg~wt ) plot(myformula , data=mtcars) str(lm(myformula , data=mtcars)) # inside the guts of the sneaky abline() trick; what are the first 2 numbers abline meets? coef(lm(myformula , data=mtcars)) abline(lm(myformula , data=mtcars), col="forestgreen" ,lwd=2) lines( x=mtcars$wt, y=fitted(lm(myformula , data=mtcars)), col="pink", lwd=10, lty=3) #yuk! # Q3 lawschool <- read.table("http://faculty.washington.edu/kenrice/rintro/lawschool.txt", header=TRUE) law.lm <- lm(LSAT~GPA, data=lawschool) confint(law.lm, "GPA", level=0.95) #version from cor.test (not proportional - cor.test uses a transform) with(lawschool, cor.test(LSAT, GPA)$conf.int) #Q4 titaniclong <- read.table("http://faculty.washington.edu/kenrice/rintro/titaniclong.txt", header=TRUE) par(mar=c(4,4,1,6)) plot(factor(Survived)~factor(Class), data=titaniclong) pr.odds <- c(1/101, 1/5, 1/3,0.5, 2/3, 5/6, 100/101) # axis giving odds axis(side=4, at=pr.odds, labels=c("1/100","1/5","1/2",1,2,5,100), line=3, col=2) glm1 <- glm(Survived~factor(Class), data=titaniclong) summary(glm1) exp(confint.default(glm1)[1,]) exp(confint.default(glm1)[2:4,])