# analyzing data rm(list=ls()) hs1 <- read.table("http://www.ats.ucla.edu/stat/R/notes/hs1.csv", header=T, sep=",") attach(hs1) # chi-square test of independence tab1<-table(female, ses) summary(tab1) # one-sample test t.test(write, mu=50) # paired t-test t.test(write, read, paired=TRUE) # two-sample independent t-test by(write, female, var) tapply(write, female, var) # assuming equal variances t.test(write~female, var.equal=TRUE) # assuming unequal variances t.test(write~female, var.equal=FALSE) # one-way anova anova(lm(write~factor(prog))) summary(aov(write~factor(prog))) # an example of a two-way factorial ANOVA # type I sum of squares m2<-lm(write~factor(prog)*factor(female)) anova(m2) # https://stat.ethz.ch/pipermail/r-help/2010-March/230280.html # John Fox's post on type I, II and III sum of squares in ANOVA library(car) # type II sum of squares Anova(m2) # an example of ancova anova(lm(write~factor(prog) + read)) summary(aov(write~factor(prog) + read)) # ols regression summary(lm(write~female+read)) lm2 <- lm(write~read+socst) summary(lm2) # plotting diagnostic plots of lm2 plot(lm2) class(lm2) names(lm2) length(lm2) length(lm2$residuals) length(lm2$coefficients) lm2$coefficients # extracting predicted values and residuals write[1:20] fitted(lm2)[1:20] resid(lm2)[1:20] # logistic regression # creating a binary variable honcomp <- write >= 60 honcomp[1:20] # fitting a logistic regression model lr <- glm(honcomp~female+read, family=binomial) summary(lr) # odds ratios exp(coef(lr)) # Non-Parametric Tests # analog to one-sample t-test wilcox.test(write, mu=50) # analog to the paired t-test wilcox.test(write, read, paired=T) # analog to the independent two-sample t-test. wilcox.test(write, female) # anallog to one-way anova kruskal.test(write, ses) detach(hs1)