Skip to main content
Statistics LibreTexts

5.1.3: Two-Factor Factorial - Greenhouse Example (R)

  • Page ID
    33636
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    • Load the greenhouse data.
    • Produce a boxplot to plot the differences in heights for each species organized by fertilizer.
    • Produce a “means plot” (interval plot) to view the differences in heights for each species organized by fertilizer.
    • Obtain the ANOVA table with interaction.
    • Obtain Tukey’s multiple comparisons CIs, grouping, and plot.

    1. Load the greenhouse data by using the following commands:

    setwd("~/path-to-folder/")
    greenhouse_2way_data <-read.table("greenhouse_2way_data.txt",header=T)
    attach(greenhouse_2way_data)
    

    2. Produce the Boxplot by using the following commands:

    library("ggpubr")
    boxplot(height ~ species*fertilizer, data = greenhouse_2way_data,
    xlab = "Species", ylab = "Plant Height",
    main="Distribution of Plant Height by Species",
    frame = TRUE)
    
    R-generated boxplot for distribution of plant height by species.
    Figure \(\PageIndex{1}\): Boxplot of plant height distribution by species.

    3. Produce the means plot (interval plot) by using the following commands:

    library("gplots")
    plotmeans(height ~ interaction(species,fertilizer), data = greenhouse_2way_data,connect=FALSE,n.label=FALSE,
    xlab = "Fertilizer*species", ylab = "Plant Height",
    main="Means Plot with 95% CI") 
    
    Means plot with 95% confidence intervals for plant height vs. Fertilizer*Species
    Figure \(\PageIndex{2}\): Means plot for plant height vs fertilizer*species.

    4. Obtain the ANOVA table with interaction by using the following commands:

    anova<-aov(height~fertilizer+species+fertilizer*species,greenhouse_2way_data)
    summary(anova)
    #                    Df Sum Sq Mean Sq F value   Pr(>F)    
    # fertilizer          3  745.4  248.48   73.10 2.77e-16 ***
    # species             1  236.7  236.74   69.64 2.71e-10 ***
    # fertilizer:species  3   50.6   16.86    4.96  0.00508 **
    # Residuals          40  136.0    3.40                     
    # ---
    # Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
    

    5. Obtain Tukey multiple comparisons of means with 95% family-wise confidence level by using the following commands:

    library(multcomp)
    library(multcompView)
    tukey_multiple_comparisons<-TukeyHSD(anova,conf.level=0.95,ordered=TRUE)
    tukey_multiple_comparisons
    Tukey multiple comparisons of means
    95% family-wise confidence level
    factor levels have been ordered
    Fit: aov(formula = height ~ fertilizer + species + fertilizer * species, data = greenhouse_2way_data)
    $fertilizer
    diff       lwr       upr     p adj
    f2-control  5.608333 3.5908095  7.625857 0.0000000
    f1-control  7.758333 5.7408095  9.775857 0.0000000
    f3-control 10.783333 8.7658095 12.800857 0.0000000
    f1-f2       2.150000 0.1324762  4.167524 0.0328745
    f3-f2       5.175000 3.1574762  7.192524 0.0000002
    f3-f1       3.025000 1.0074762  5.042524 0.0013828
    $species
    diff      lwr      upr p adj
    SppB-SppA 4.441667 3.365986 5.517348     0
    $`fertilizer:species`
    diff        lwr       upr     p adj
    control:SppB-control:SppA  2.700000 -0.7025601  6.102560 0.2100548
    f2:SppA-control:SppA       4.866667  1.4641065  8.269227 0.0010962
    f1:SppA-control:SppA       7.600000  4.1974399 11.002560 0.0000003
    f3:SppA-control:SppA       8.200000  4.7974399 11.602560 0.0000001
    f2:SppB-control:SppA       9.050000  5.6474399 12.452560 0.0000000
    f1:SppB-control:SppA      10.616667  7.2141065 14.019227 0.0000000
    f3:SppB-control:SppA      16.066667 12.6641065 19.469227 0.0000000
    f2:SppA-control:SppB       2.166667 -1.2358935  5.569227 0.4721837
    f1:SppA-control:SppB       4.900000  1.4974399  8.302560 0.0009970
    f3:SppA-control:SppB       5.500000  2.0974399  8.902560 0.0001745
    f2:SppB-control:SppB       6.350000  2.9474399  9.752560 0.0000138
    f1:SppB-control:SppB       7.916667  4.5141065 11.319227 0.0000001
    f3:SppB-control:SppB      13.366667  9.9641065 16.769227 0.0000000
    f1:SppA-f2:SppA            2.733333 -0.6692268  6.135893 0.1979193
    f3:SppA-f2:SppA            3.333333 -0.0692268  6.735893 0.0584747
    f2:SppB-f2:SppA            4.183333  0.7807732  7.585893 0.0072041
    f1:SppB-f2:SppA            5.750000  2.3474399  9.152560 0.0000832
    f3:SppB-f2:SppA           11.200000  7.7974399 14.602560 0.0000000
    f3:SppA-f1:SppA            0.600000 -2.8025601  4.002560 0.9991227
    f2:SppB-f1:SppA            1.450000 -1.9525601  4.852560 0.8685338
    f1:SppB-f1:SppA            3.016667 -0.3858935  6.419227 0.1150225
    f3:SppB-f1:SppA            8.466667  5.0641065 11.869227 0.0000000
    f2:SppB-f3:SppA            0.850000 -2.5525601  4.252560 0.9922487
    f1:SppB-f3:SppA            2.416667 -0.9858935  5.819227 0.3344595
    f3:SppB-f3:SppA            7.866667  4.4641065 11.269227 0.0000001
    f1:SppB-f2:SppB            1.566667 -1.8358935  4.969227 0.8173904
    f3:SppB-f2:SppB            7.016667  3.6141065 10.419227 0.0000019
    f3:SppB-f1:SppB            5.450000  2.0474399  8.852560 0.0002022
    

    We can see the mean differences for fertilizer combinations, for the two species and for all fertilizer*species combinations. By using the confidence intervals or the p-values we can conclude which of these combinations are significant or not.

    6. Obtain Tukey grouping by using the following commands:

    tukey_grouping<-multcompLetters4(anova,tukey_multiple_comparisons)
    print(tukey_grouping)
    $fertilizer
    f3      f1      f2 control
    "a"     "b"     "c"     "d"
    $species
    SppB SppA
    "a"  "b"
    $`fertilizer:species`
    f3:SppB f1:SppB f2:SppB f3:SppA f1:SppA f2:SppA control:SppB control:SppA
    "a"     "b"     "b"    "bc"     "bc"     "cd"      "de"          "e"
    

    7. Obtain a plot of differences in mean response for fertilizer*species combinations by using the following commands:

    par(mar=c(4.1,13,4.1,2.1))
    plot(tukey_multiple_comparisons,las=2)
    detach(greenhouse_2way_data)
    
    95% family-wise confidence level graph for differences in mean levels of Fertilizer:species
    Figure \(\PageIndex{3}\): Graph of differences in mean levels of fertilizer:species, showing 95% family-wise confidence levels.

    This page titled 5.1.3: Two-Factor Factorial - Greenhouse Example (R) is shared under a CC BY-NC 4.0 license and was authored, remixed, and/or curated by Penn State's Department of Statistics.

    • Was this article helpful?