## ## Lab 4 ## ## ------------------------------------------------------------------------ setwd("~r/mooc") ## ------------------------------------------------------------------------ load("smp_V1.rda") ## ------------------------------------------------------------------------ table(smp$subst.cons,smp$abus) ## ------------------------------------------------------------------------ tab <- table(smp$subst.cons,smp$abus) ## ------------------------------------------------------------------------ tab ## ------------------------------------------------------------------------ prop.table(tab) ## ------------------------------------------------------------------------ prop.table(tab,margin = 1) ## ------------------------------------------------------------------------ prop.table(tab,margin = 2) ## ------------------------------------------------------------------------ xtabs(~subst.cons + abus, smp) ## ------------------------------------------------------------------------ barplot(xtabs(~subst.cons + abus, smp)) ## ------------------------------------------------------------------------ barplot(xtabs(~subst.cons + abus, smp),beside = TRUE) ## ------------------------------------------------------------------------ chisq.test(tab) ## ------------------------------------------------------------------------ res <- chisq.test(tab) ## ------------------------------------------------------------------------ res ## ------------------------------------------------------------------------ res$observed ## ------------------------------------------------------------------------ res$expected ## ------------------------------------------------------------------------ fisher.test(tab) ## ------------------------------------------------------------------------ head(smp$age) ## ------------------------------------------------------------------------ table(smp$subst.cons) ## ------------------------------------------------------------------------ table(smp$subst.cons,useNA="always") ## ------------------------------------------------------------------------ tapply(smp$age,smp$subst.class, mean) ## ------------------------------------------------------------------------ tapply(smp$age,smp$subst.class, mean, na.rm=TRUE) ## ------------------------------------------------------------------------ tapply(smp$age,smp$subst.cons, mean) ## ------------------------------------------------------------------------ tapply(smp$age,smp$subst.cons, mean, na.rm=TRUE) ## ----ttest--------------------------------------------------------------- t.test(smp$age[smp$subst.cons == 0], smp$age[smp$subst.cons == 1],var.equal= TRUE) ## ------------------------------------------------------------------------ t.test(age ~ subst.cons, data=smp) ## ------------------------------------------------------------------------ aggregate(age ~ subst.cons, data=smp, mean) ## ------------------------------------------------------------------------ boxplot(age ~ subst.cons, data=smp) ## ------------------------------------------------------------------------ library(ggplots)