one way anova (cr-p design)
TRANSCRIPT
17/03/2016 Oneway ANOVA (CRp design)
http://www.unikiel.de/psychologie/rexrepos/posts/anovaCRp.html 1/12
R Examples Repository Home (http://www.unikiel.de/psychologie/rexrepos/index.html)» Univariate (http://www.unikiel.de/psychologie/rexrepos/categories.html#Univariateref)» ANOVA (http://www.unikiel.de/psychologie/rexrepos/categories.html#ANOVAref) » anovaCRp
Regression Models
Multiple linear regression (http://www.unikiel.de/psychologie/rexrepos/posts/regression.html)
Robust and penalized regression (http://www.unikiel.de/psychologie/rexrepos/posts/regressionRobPen.html)
Moderation and mediation (http://www.unikiel.de/psychologie/rexrepos/posts/regressionModMed.html)
Logistic regression (http://www.unikiel.de/psychologie/rexrepos/posts/regressionLogistic.html)
Ordinal regression (http://www.unikiel.de/psychologie/rexrepos/posts/regressionOrdinal.html)
Multinomial regression (http://www.unikiel.de/psychologie/rexrepos/posts/regressionMultinom.html)
Poisson regression (http://www.unikiel.de/psychologie/rexrepos/posts/regressionPoisson.html)
Loglinear models (http://www.unikiel.de/psychologie/rexrepos/posts/logLinear.html)
Regression diagnostics (http://www.unikiel.de/psychologie/rexrepos/posts/regressionDiag.html)
Crossvalidation (http://www.unikiel.de/psychologie/rexrepos/posts/crossvalidation.html)Survival analysis
KaplanMeierestimate (http://www.unikiel.de/psychologie/rexrepos/posts/survivalKM.html)
Cox proportional hazards (http://www.unikiel.de/psychologie/rexrepos/posts/survivalCoxPH.html)
Parametric proportional hazards (http://www.unikiel.de/psychologie/rexrepos/posts/survivalParametric.html)t Tests
ttests (http://www.unikiel.de/psychologie/rexrepos/posts/tTest.html)ANOVA
Oneway ANOVA
Twoway ANOVA (http://www.unikiel.de/psychologie/rexrepos/posts/anovaCRFpq.html)
Analysis of covariance (http://www.unikiel.de/psychologie/rexrepos/posts/ancova.html)
Oneway repeatedmeasures ANOVA (http://www.unikiel.de/psychologie/rexrepos/posts/anovaRBp.html)
Twoway repeatedmeasures ANOVA (http://www.unikiel.de/psychologie/rexrepos/posts/anovaRBFpq.html)
Twoway splitplot ANOVA (http://www.unikiel.de/psychologie/rexrepos/posts/anovaSPFpq.html)
Threeway splitplot ANOVA (http://www.unikiel.de/psychologie/rexrepos/posts/anovaSPFpqr.html)
Mixed effects models (http://www.unikiel.de/psychologie/rexrepos/posts/anovaMixed.html)
Sum of squares type I, II, and III (http://www.uni
17/03/2016 Oneway ANOVA (CRp design)
http://www.unikiel.de/psychologie/rexrepos/posts/anovaCRp.html 2/12
kiel.de/psychologie/rexrepos/posts/anovaSStypes.html)General Topics
Assess normality (http://www.unikiel.de/psychologie/rexrepos/posts/normality.html)
Assess variance homogeneity (http://www.unikiel.de/psychologie/rexrepos/posts/varianceHom.html)
Tags
ANOVA (http://www.unikiel.de/psychologie/rexrepos/tags.html#ANOVAref)
Oneway ANOVA (CRp design)TODOInstall required packagesCR ANOVA
Simulate dataUsing oneway.test()Using aov()Model comparisons using anova(lm())
Effect size estimatesPlanned comparisons apriori
General contrasts using glht() from package multcompPairwise tests
Planned comparisons posthocScheffe testsTukey's simultaneous confidence intervals
Assess test assumptionsNormalityVariance homogeneity
Detach (automatically) loaded packages (if possible)Get the article source from GitHub
TODOlink to normality, varianceHom, regressionDiag, regression for model comparison,resamplingPerm, resamplingBootALM
Install required packagescar (http://cran.rproject.org/package=car), DescTools (http://cran.rproject.org/package=DescTools),multcomp (http://cran.rproject.org/package=multcomp)
wants <‐ c("car", "DescTools", "multcomp") has <‐ wants %in% rownames(installed.packages()) if(any(!has)) install.packages(wants[!has])
CR ANOVA
p
t
p
17/03/2016 Oneway ANOVA (CRp design)
http://www.unikiel.de/psychologie/rexrepos/posts/anovaCRp.html 3/12
Simulate data
set.seed(123) P <‐ 4 Nj <‐ c(41, 37, 42, 40) muJ <‐ rep(c(‐1, 0, 1, 2), Nj) dfCRp <‐ data.frame(IV=factor(rep(LETTERS[1:P], Nj)), DV=rnorm(sum(Nj), muJ, 5))
plot.design(DV ~ IV, fun=mean, data=dfCRp, main="Group means")
plot of chunk rerAnovaCRp01
Using oneway.test()
Assuming variance homogeneity
oneway.test(DV ~ IV, data=dfCRp, var.equal=TRUE)
One‐way analysis of means data: DV and IV F = 2.0057, num df = 3, denom df = 156, p‐value = 0.1154
17/03/2016 Oneway ANOVA (CRp design)
http://www.unikiel.de/psychologie/rexrepos/posts/anovaCRp.html 4/12
Generalized Welchtest without assumption of variance homogeneity
oneway.test(DV ~ IV, data=dfCRp, var.equal=FALSE)
One‐way analysis of means (not assuming equal variances) data: DV and IV F = 2.0203, num df = 3.000, denom df = 85.503, p‐value = 0.1171
Using aov()
aovCRp <‐ aov(DV ~ IV, data=dfCRp) summary(aovCRp)
Df Sum Sq Mean Sq F value Pr(>F) IV 3 133 44.35 2.006 0.115 Residuals 156 3450 22.11
model.tables(aovCRp, type="means")
Tables of means Grand mean 0.4318522 IV A B C D ‐0.8643 0.05185 1.042 1.471 rep 41.0000 37.00000 42.000 40.000
Model comparisons using anova(lm())
(anovaCRp <‐ anova(lm(DV ~ IV, data=dfCRp)))
Analysis of Variance Table Response: DV Df Sum Sq Mean Sq F value Pr(>F) IV 3 133.1 44.353 2.0057 0.1154 Residuals 156 3449.7 22.113
anova(lm(DV ~ 1, data=dfCRp), lm(DV ~ IV, data=dfCRp))
17/03/2016 Oneway ANOVA (CRp design)
http://www.unikiel.de/psychologie/rexrepos/posts/anovaCRp.html 5/12
Analysis of Variance Table Model 1: DV ~ 1 Model 2: DV ~ IV Res.Df RSS Df Sum of Sq F Pr(>F) 1 159 3582.8 2 156 3449.7 3 133.06 2.0057 0.1154
anovaCRp["Residuals", "Sum Sq"]
[1] 3449.703
Effect size estimates
dfSSb <‐ anovaCRp["IV", "Df"] SSb <‐ anovaCRp["IV", "Sum Sq"] MSb <‐ anovaCRp["IV", "Mean Sq"] SSw <‐ anovaCRp["Residuals", "Sum Sq"] MSw <‐ anovaCRp["Residuals", "Mean Sq"]
(etaSq <‐ SSb / (SSb + SSw))
[1] 0.03713889
library(DescTools) # for EtaSq() EtaSq(aovCRp, type=1)
eta.sq eta.sq.part IV 0.03713889 0.03713889
,
(omegaSq <‐ dfSSb * (MSb‐MSw) / (SSb + SSw + MSw))
[1] 0.01850809
(f <‐ sqrt(etaSq / (1‐etaSq)))
[1] 0.196396
Planned comparisons apriori
η2̂
ω2̂ f 2̂
17/03/2016 Oneway ANOVA (CRp design)
http://www.unikiel.de/psychologie/rexrepos/posts/anovaCRp.html 6/12
General contrasts using glht() from package multcomp
cntrMat <‐ rbind("A‐D" =c( 1, 0, 0, ‐1), "1/3*(A+B+C)‐D"=c(1/3, 1/3, 1/3, ‐1), "B‐C" =c( 0, 1, ‐1, 0)) library(multcomp) # for glht() summary(glht(aovCRp, linfct=mcp(IV=cntrMat), alternative="less"), test=adjusted("none"))
Simultaneous Tests for General Linear Hypotheses Multiple Comparisons of Means: User‐defined Contrasts Fit: aov(formula = DV ~ IV, data = dfCRp) Linear Hypotheses: Estimate Std. Error t value Pr(<t) A‐D >= 0 ‐2.3351 1.0451 ‐2.234 0.0134 * 1/3*(A+B+C)‐D >= 0 ‐1.3941 0.8589 ‐1.623 0.0533 . B‐C >= 0 ‐0.9906 1.0603 ‐0.934 0.1758 ‐‐‐ Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 (Adjusted p values reported ‐‐ none method)
Pairwise tests
pairwise.t.test(dfCRp$DV, dfCRp$IV, p.adjust.method="bonferroni")
Pairwise comparisons using t tests with pooled SD data: dfCRp$DV and dfCRp$IV A B C B 1.00 ‐ ‐ C 0.40 1.00 ‐ D 0.16 1.00 1.00 P value adjustment method: bonferroni
Planned comparisons posthoc
Scheffe tests
library(DescTools) # for ScheffeTest() ScheffeTest(aovCRp, which="IV", contrasts=t(cntrMat))
t
17/03/2016 Oneway ANOVA (CRp design)
http://www.unikiel.de/psychologie/rexrepos/posts/anovaCRp.html 7/12
Posthoc multiple comparisons of means : Scheffe Test 95% family‐wise confidence level Fit: aov(formula = DV ~ IV, data = dfCRp) $IV diff lwr.ci upr.ci pval A‐D ‐2.3351002 ‐5.288758 0.6185575 0.1770 A,B,C‐D ‐1.3941211 ‐3.821531 1.0332885 0.4538 B‐C ‐0.9906183 ‐3.987210 2.0059738 0.8319 ‐‐‐ Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
Tukey's simultaneous confidence intervals
(tHSD <‐ TukeyHSD(aovCRp))
Tukey multiple comparisons of means 95% family‐wise confidence level Fit: aov(formula = DV ~ IV, data = dfCRp) $IV diff lwr upr p adj B‐A 0.9161596 ‐1.8529795 3.685299 0.8257939 C‐A 1.9067779 ‐0.7743204 4.587876 0.2555117 D‐A 2.3351002 ‐0.3789061 5.049107 0.1185540 C‐B 0.9906183 ‐1.7628388 3.744075 0.7864641 D‐B 1.4189406 ‐1.3665697 4.204451 0.5497967 D‐C 0.4283223 ‐2.2696814 3.126326 0.9762890
plot(tHSD)
17/03/2016 Oneway ANOVA (CRp design)
http://www.unikiel.de/psychologie/rexrepos/posts/anovaCRp.html 8/12
plot of chunk rerAnovaCRp02
Using glht() from package multcomp
library(multcomp) # for glht() tukey <‐ glht(aovCRp, linfct=mcp(IV="Tukey")) summary(tukey)
Simultaneous Tests for General Linear Hypotheses Multiple Comparisons of Means: Tukey Contrasts Fit: aov(formula = DV ~ IV, data = dfCRp) Linear Hypotheses: Estimate Std. Error t value Pr(>|t|) B ‐ A == 0 0.9162 1.0663 0.859 0.826 C ‐ A == 0 1.9068 1.0324 1.847 0.255 D ‐ A == 0 2.3351 1.0451 2.234 0.119 C ‐ B == 0 0.9906 1.0603 0.934 0.786 D ‐ B == 0 1.4189 1.0726 1.323 0.550 D ‐ C == 0 0.4283 1.0389 0.412 0.976 (Adjusted p values reported ‐‐ single‐step method)
17/03/2016 Oneway ANOVA (CRp design)
http://www.unikiel.de/psychologie/rexrepos/posts/anovaCRp.html 9/12
confint(tukey)
Simultaneous Confidence Intervals Multiple Comparisons of Means: Tukey Contrasts Fit: aov(formula = DV ~ IV, data = dfCRp) Quantile = 2.597295% family‐wise confidence level Linear Hypotheses: Estimate lwr upr B ‐ A == 0 0.9162 ‐1.8533 3.6856 C ‐ A == 0 1.9068 ‐0.7746 4.5882 D ‐ A == 0 2.3351 ‐0.3792 5.0494 C ‐ B == 0 0.9906 ‐1.7632 3.7444 D ‐ B == 0 1.4189 ‐1.3669 4.2048 D ‐ C == 0 0.4283 ‐2.2700 3.1266
Assess test assumptions
Normality
Estud <‐ rstudent(aovCRp) qqnorm(Estud, pch=20, cex=2) qqline(Estud, col="gray60", lwd=2)
17/03/2016 Oneway ANOVA (CRp design)
http://www.unikiel.de/psychologie/rexrepos/posts/anovaCRp.html 10/12
plot of chunk rerAnovaCRp03
shapiro.test(Estud)
Shapiro‐Wilk normality test data: Estud W = 0.9937, p‐value = 0.7149
Variance homogeneity
plot(Estud ~ dfCRp$IV, main="Residuals per group")
17/03/2016 Oneway ANOVA (CRp design)
http://www.unikiel.de/psychologie/rexrepos/posts/anovaCRp.html 11/12
plot of chunk rerAnovaCRp04
library(car) leveneTest(aovCRp)
Levene's Test for Homogeneity of Variance (center = median) Df F value Pr(>F) group 3 0.8551 0.4659 156
Detach (automatically) loaded packages (if possible)
try(detach(package:car)) try(detach(package:multcomp)) try(detach(package:survival)) try(detach(package:mvtnorm)) try(detach(package:splines)) try(detach(package:TH.data)) try(detach(package:DescTools))
Get the article source from GitHub
17/03/2016 Oneway ANOVA (CRp design)
http://www.unikiel.de/psychologie/rexrepos/posts/anovaCRp.html 12/12
top
R markdown (https://github.com/dwoll/RExRepos/raw/master/Rmd/anovaCRp.Rmd) markdown(https://github.com/dwoll/RExRepos/raw/master/md/anovaCRp.md) R code(https://github.com/dwoll/RExRepos/raw/master/R/anovaCRp.R) all posts(https://github.com/dwoll/RExRepos/)
© 2014 Daniel Wollschlaeger licensed under CCBYSA (http://creativecommons.org/licenses/bysa/4.0/)