r:twoway_anova
                Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| r:twoway_anova [2017/11/06 08:57] – [3] hkimscil | r:twoway_anova [2018/10/19 08:39] (current) – [e.g. 5] hkimscil | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | data sample: {{: | ||
| + | |||
| ====== Twoway ANOVA ====== | ====== Twoway ANOVA ====== | ||
| recap of oneway ANOVA | recap of oneway ANOVA | ||
| Line 78: | Line 80: | ||
| 45 Carrier 3 Office 5 15.04 | 45 Carrier 3 Office 5 15.04 | ||
| - | > library(ggplot2) | + | > library(" | 
| - | > ggplot(delivery.df,  | + | > ggline(delivery.df,  | 
| - | > | + | |
| </ | </ | ||
| Line 217: | Line 218: | ||
| [{{: | [{{: | ||
| - | ===== 5 ===== | + | ===== e.g., 5 ===== | 
| - | download  | + | download  | 
| - | < | + | < | 
| > #display the data | > #display the data | ||
| - | > data | + | > stressdata | 
|     |     | ||
| 1      | 1      | ||
| Line 252: | Line 253: | ||
| 27    | 27    | ||
| - | < | + | < | 
| - | Call: | + | > stressdata <- read.csv("http:// | 
| - |     | + | > | 
| - | + | > a.mod <- aov(StressReduction~Treatment*Age,  | |
| - | Terms: | + | |
| - |                 d$Treatment d$Age d$Treatment: | + | |
| - | Sum of Squares  | + | |
| - | Deg. of Freedom  | + | |
| - | + | ||
| - | Residual standard error: 1 | + | |
| - | Estimated effects may be unbalanced | + | |
| - | > a.mod <- aov(d$StressReduction~d$Treatment*d$Age, d) | + | |
| > summary(a.mod) | > summary(a.mod) | ||
| - |                    | + |                | 
| - | d$Treatment  | + | Treatment  | 
| - | d$Age 2 162 81 81 1e-09 *** | + | Age 2 162 81 81 1e-09 *** | 
| - | d$Treatment:d$Age  4      0        | + | Treatment: | 
| - | Residuals  | + | Residuals  | 
| --- | --- | ||
| Signif. codes:  | Signif. codes:  | ||
| - | ></ | + | > | 
| + | </ | ||
| - | < | + | < | 
| + | Tukey multiple comparisons of means | ||
| + | 95% family-wise confidence level | ||
| - | Pairwise comparisons using t tests with pooled SD | + | Fit: aov(formula = StressReduction ~ Treatment * Age, data = stressdata) | 
| - | data:  d$StressReduction and d$Treatment  | + | $Treatment | 
| + |                   | ||
| + | mental-medical  | ||
| + | physical-medical  | ||
| + | physical-mental  | ||
| - |           | + | > | 
| - | mental  | + | </ | 
| - | physical 0.45 0.45 | + | < | 
| + | > TukeyHSD(a.mod, which=" | ||
| + | Tukey multiple comparisons of means | ||
| + | 95% family-wise confidence level | ||
| - | P value adjustment method: none</ | + | Fit: aov(formula = StressReduction  | 
| - | < | + | |
| - | Pairwise comparisons using t tests with pooled SD | + | $Age | 
| + |           diff        | ||
| + | old-mid  | ||
| + | young-mid  | ||
| + | young-old  | ||
| + | > | ||
| + | </ | ||
| - | data: d$StressReduction and d$Age | ||
| - | |||
| - |       mid      | ||
| - | old    | ||
| - | young 2.5e-05 2.3e-10 | ||
| - | |||
| - | P value adjustment method: none | ||
| - | > | ||
| - | </ | ||
| <WRAP info> | <WRAP info> | ||
| Line 317: | Line 316: | ||
| </ | </ | ||
| - | < | ||
| - | Pairwise comparisons using t tests with pooled SD | + | ===== e.g., 6 ===== | 
| - | data:  d$StressReduction and d$Treatment  | + | < | 
| + | tdata | ||
| + | str(tdata) | ||
| + | tdata$dose <- factor(tdata$dose) | ||
| + | # or a nicer way | ||
| + | tdata$dose <- factor(tdata$dose, | ||
| - |           | + | tdata | 
| - | mental  | + | |
| - | physical 0.45    0.45   | + | |
| - | P value adjustment method: none</ | + | table(tdata$supp, tdata$dose) | 
| - | < | + | </ | 
| - | Pairwise comparisons using t tests with pooled SD | + | ==== Visualize the data ==== | 
| - | data: d$StressReduction and d$Age | + | < | 
| - |        | + | library(" | 
| - | old    | + | ggboxplot(tdata, | 
| - | young 2.5e-05 2.3e-10 | + |           palette = c(" | 
| + | ggline(tdata, | ||
| + |         | ||
| + |         | ||
| + | |||
| + | # prettier | ||
| + | boxplot(len ~ supp * dose, data=tdata, frame = FALSE,  | ||
| + |        | ||
| + | |||
| + | # or | ||
| + | interaction.plot(x.factor = tdata$dose, trace.factor = | ||
| + | tdata$supp, response = tdata$len, fun = mean, | ||
| + |       type = " | ||
| + |       xlab = " | ||
| + |       pch=c(1, | ||
| - | P value adjustment method: none | ||
| - | > | ||
| </ | </ | ||
| + | ==== Compute ANOVA test ==== | ||
| + | < | ||
| + | summary(res.aov2) | ||
| + |             Df Sum Sq Mean Sq F value    | ||
| + | supp          | ||
| + | dose         2 2426.4  | ||
| + | Residuals  | ||
| + | --- | ||
| + | Signif. codes:   | ||
| + | 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1</ | ||
| + | |||
| + | |||
| + | < | ||
| + | res.aov3 <- aov(len ~ supp + dose + supp:dose, data = tdata) | ||
| + | summary(res.aov3) | ||
| + |             Df Sum Sq Mean Sq F value    | ||
| + | supp          | ||
| + | dose         2 2426.4  | ||
| + | supp: | ||
| + | Residuals  | ||
| + | --- | ||
| + | Signif. codes:   | ||
| + | 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 | ||
| + | </ | ||
| + | |||
| + | From the ANOVA results, you can conclude the following, based on the p-values and a significance level of 0.05: | ||
| + | |||
| + |   * the p-value of supp is 0.000429 (significant), | ||
| + |   * the p-value of dose is < 2e-16 (significant), | ||
| + |   * the p-value for the interaction between supp*dose is 0.02 (significant), | ||
| + | |||
| + | ==== pair-wise test ==== | ||
| + | |||
| + | < | ||
| + | Tukey multiple comparisons of means | ||
| + | 95% family-wise confidence level | ||
| + | |||
| + | Fit: aov(formula = len ~ supp * dose, data = tdata) | ||
| + | |||
| + | $dose | ||
| + |         diff        | ||
| + | d1-dh 9.130 6.362488 11.897512 0.0e+00 | ||
| + | d2-dh 15.495 12.727488 18.262512 0.0e+00 | ||
| + | d2-d1  6.365  3.597488  | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | </ | ||
| + | ==== Homogeneity ==== | ||
| + | |||
| + | < | ||
| + | |||
| + | < | ||
| + | library(car) | ||
| + | leveneTest(len ~ supp*dose, data = tdata) | ||
| + | |||
| + | Levene' | ||
| + | Df F value Pr(>F) | ||
| + | group 5 1.7086 0.1484 | ||
| + | 54 | ||
| + | </ | ||
| + | p value is greater than .05, which indicates that there is no evidence of significant difference between variances of groups. | ||
| + | |||
| + | ==== check normality ==== | ||
| + | < | ||
| + | </ | ||
| + | The residuals are plotted against the quantiles of normal distribution (the straight line). | ||
| + | |||
| + | ==== unbalance design ==== | ||
| + | < | ||
| + | > my_anova <- aov(len ~ supp * dose, data = tdata) | ||
| + | > | ||
| + | > Anova(my_anova, | ||
| + | + ) | ||
| + | Anova Table (Type III tests) | ||
| + | |||
| + | Response: len | ||
| + |              Sum Sq Df F value    Pr(> | ||
| + | (Intercept) 1750.33  | ||
| + | supp          | ||
| + | dose          | ||
| + | supp: | ||
| + | Residuals  | ||
| + | --- | ||
| + | Signif. codes:   | ||
| + | 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 | ||
| + | </ | ||
r/twoway_anova.1509928056.txt.gz · Last modified:  by hkimscil
                
                