Created
February 3, 2011 23:07
-
-
Save marutter/810421 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # Start with Basic ANOVA | |
| # | |
| p16.7 <- read.table("CH16PR07.txt",col.names=c("improvement","rd","observation")) | |
| attach(p16.7) | |
| rdf <- factor(rd,levels=c(1:3),labels=c("Low","Moderate","High")) | |
| result <- lm(improvement~rdf) | |
| anova(result) | |
| boxplot(improvement~rdf) | |
| # | |
| # Plot of factor level means | |
| # | |
| install.packages("gplots") | |
| library(gplots) | |
| mu.i <- tapply(improvement,rdf,mean) | |
| barplot2(mu.i) | |
| # | |
| # With standard errors | |
| # | |
| anova(result) | |
| mse <- .6401 | |
| (n.i <- tapply(improvement,rdf,length)) | |
| (se.i <- sqrt(.6401/n.i)) | |
| barplot2(mu.i,plot.ci=TRUE,ci.l=mu.i-se.i,ci.u=mu.i+se.i) | |
| # | |
| # With 95% Confidence Intervals | |
| # | |
| anova(result) | |
| qnorm(.975) | |
| qt(.975,24) | |
| barplot2(mu.i,plot.ci=TRUE,ci.l=mu.i-qt(.975,24)*se.i,ci.u=mu.i+qt(.975,24)*se.i) | |
| barplot2(mu.i,plot.ci=TRUE,ci.l=mu.i-qt(.975,24)*se.i,ci.u=mu.i+qt(.975,24)*se.i,ylim=c(0,10)) | |
| barplot2(mu.i,plot.ci=TRUE,ci.l=mu.i-qt(.975,24)*se.i,ci.u=mu.i+qt(.975,24)*se.i,ylim=c(6,10)) | |
| barplot2(mu.i,plot.ci=TRUE,ci.l=mu.i-qt(.975,24)*se.i,ci.u=mu.i+qt(.975,24)*se.i,ylim=c(6,10),xpd=FALSE) | |
| # | |
| # Contrasts of interest | |
| # Pre-planned!!! | |
| # | |
| install.packages("gmodels") | |
| library(gmodels) | |
| # Say we want the difference between Low and Moderate | |
| mu.i[1]-mu.i[2] | |
| levels(rdf) | |
| fit.contrast(result,rdf,c(1,-1,0)) | |
| fit.contrast(result,rdf,c(1,-1,0),conf.int=.95) | |
| # Average of low and high to moderate | |
| # Compare Moderate to Grand Mean | |
| # More than one at a time | |
| fit.contrast(result,rdf, | |
| rbind( "Low-Mod" = c(1,-1,0), | |
| "Mod-High" = c(0,1,-1)),conf.int=.95) | |
| # Weighted mean | |
| fit.contrast(result,rdf,c(.5,.25,.25),conf.int=.95) | |
| # Linear Combinations | |
| c.i <- c(.5,.25,.25) | |
| mu.i*c.i | |
| sum(mu.i*c.i) | |
| (se <- sqrt(mse*sum(c.i^2/n.i))) | |
| sum(mu.i*c.i)+c(-1,1)*qt(.975,24)*se | |
| # | |
| # Check Comparison | |
| # | |
| c.i <- c(1,-1,0) | |
| mu.i*c.i | |
| sum(mu.i*c.i) | |
| (se <- sqrt(mse*sum(c.i^2/n.i))) | |
| sum(mu.i*c.i)+c(-1,1)*qt(.975,24)*se | |
| fit.contrast(result,rdf,c(1,-1,0),conf.int=.95) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment