In this report, I worked through the two Chapter 6 examples: a two-way ANOVA looking at cow weight gain, and an ANCOVA looking at limpet egg production. I added some of the data exploration steps discussed by Zuur et al. (2010), such as checking for missing values, looking at possible outliers, and examining the residuals after fitting each model.
Chapter 6 uses the growth.csv and
limpet.csv datasets. The code checks for the files in my
working folder first. If they are not there, it tries to load them from
an online teaching repository, which requires an internet
connection.
## Cow observations: 48
## Limpet observations: 24
For this analysis, I wanted to see whether cow weight gain changes with diet, supplement, or both together. Weight gain is the response variable, while diet and supplement are both categorical. The interaction term checks whether a supplement has a different effect depending on the diet.
Before running the ANOVA, I checked for missing values, counted how many cows were in each group, and looked at the range of weight gain values. This also lets me see whether the treatment groups have similar sample sizes.
## supplement
## diet control agrimore supergain supersupp
## barley 4 4 4 4
## oats 4 4 4 4
## wheat 4 4 4 4
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 15.78 19.28 21.25 21.39 23.63 29.03
## supplement diet gain
## 0 0 0
I used boxplots to compare weight gain across groups and to check for any values that stand out. I would look into an unusual observation before deciding whether it should be excluded.
The boxplots also help me compare how much the observations vary between groups. However, they cannot tell me whether the observations were collected independently, since that depends on the study design.
Next, I calculated the mean weight gain and standard error (SE) for each diet and supplement combination. I then plotted those means to see whether the pattern across supplements looks similar for each diet.
| diet | supplement | n | meanGrow | sdGrow | seGrow |
|---|---|---|---|---|---|
| barley | control | 4 | 23.30 | 1.41 | 0.70 |
| barley | agrimore | 4 | 26.35 | 1.84 | 0.92 |
| barley | supergain | 4 | 22.47 | 1.54 | 0.77 |
| barley | supersupp | 4 | 25.58 | 2.12 | 1.06 |
| oats | control | 4 | 20.49 | 1.01 | 0.51 |
| oats | agrimore | 4 | 23.30 | 1.23 | 0.61 |
| oats | supergain | 4 | 19.66 | 0.70 | 0.35 |
| oats | supersupp | 4 | 21.86 | 0.83 | 0.41 |
| wheat | control | 4 | 17.41 | 0.92 | 0.46 |
| wheat | agrimore | 4 | 19.64 | 1.42 | 0.71 |
| wheat | supergain | 4 | 17.01 | 0.97 | 0.49 |
| wheat | supersupp | 4 | 19.67 | 0.95 | 0.47 |
I used the same model as Chapter 6, including diet, supplement, and their interaction.
## Analysis of Variance Table
##
## Response: gain
## Df Sum Sq Mean Sq F value Pr(>F)
## diet 2 287.171 143.586 83.5201 2.999e-14 ***
## supplement 3 91.881 30.627 17.8150 2.952e-07 ***
## diet:supplement 6 3.406 0.568 0.3302 0.9166
## Residuals 36 61.890 1.719
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Call:
## lm(formula = gain ~ diet * supplement, data = growth.moo)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.48756 -1.00368 -0.07452 1.03496 2.68069
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 23.2966499 0.6555863 35.536 < 2e-16 ***
## dietoats -2.8029851 0.9271390 -3.023 0.00459 **
## dietwheat -5.8911317 0.9271390 -6.354 2.34e-07 ***
## supplementagrimore 3.0518277 0.9271390 3.292 0.00224 **
## supplementsupergain -0.8305263 0.9271390 -0.896 0.37631
## supplementsupersupp 2.2786527 0.9271390 2.458 0.01893 *
## dietoats:supplementagrimore -0.2471088 1.3111726 -0.188 0.85157
## dietwheat:supplementagrimore -0.8182729 1.3111726 -0.624 0.53651
## dietoats:supplementsupergain -0.0001351 1.3111726 0.000 0.99992
## dietwheat:supplementsupergain 0.4374395 1.3111726 0.334 0.74060
## dietoats:supplementsupersupp -0.9120830 1.3111726 -0.696 0.49113
## dietwheat:supplementsupersupp -0.0158299 1.3111726 -0.012 0.99043
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.311 on 36 degrees of freedom
## Multiple R-squared: 0.8607, Adjusted R-squared: 0.8182
## F-statistic: 20.22 on 11 and 36 DF, p-value: 3.295e-12
From the ANOVA, diet was significant (F(2, 36) = 83.52, p < 0.001), and supplement was significant (F(3, 36) = 17.81, p < 0.001).
The interaction was not significant (F(6, 36) = 0.33, p = 0.917).
This suggests there is not enough evidence that the supplement effect changes between diets.
After fitting the model, I checked the residuals. I plotted them against the fitted values to look for patterns or changes in spread, used a Q-Q plot to check approximate normality, and compared residuals across treatment groups.
## integer(0)
For the second analysis, I looked at how limpet egg production
(EGGS) changes with density and season. Density is
continuous, while season is categorical, so ANCOVA can be used here. I
also included the interaction to see whether the relationship between
density and egg production changes between seasons.
I started by checking the data for missing values and looking at how the density measurements are spread across the seasons. I also plotted egg production against density so I could see the overall trend and check for any observations that stood out.
## SEASON
## DENSITY spring summer
## 8 3 3
## 15 3 3
## 30 3 3
## 45 3 3
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.3560 0.9165 1.4330 1.4717 1.9227 2.8750
## DENSITY SEASON EGGS
## 0 0 0
This plot helps me see whether egg production tends to increase or decrease with density and whether the trend looks different between seasons. I added a little horizontal jitter so overlapping observations are easier to see. The actual density values are unchanged in the model.
I fitted the same ANCOVA model used in Chapter 6. It includes
density, season, and their interaction. The anova() and
summary() outputs let me check the effects and the model
estimates.
## Analysis of Variance Table
##
## Response: EGGS
## Df Sum Sq Mean Sq F value Pr(>F)
## DENSITY 1 5.0241 5.0241 30.1971 2.226e-05 ***
## SEASON 1 3.2502 3.2502 19.5350 0.0002637 ***
## DENSITY:SEASON 1 0.0118 0.0118 0.0711 0.7925333
## Residuals 20 3.3275 0.1664
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Call:
## lm(formula = EGGS ~ DENSITY * SEASON, data = limp)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.65468 -0.25021 -0.03318 0.28335 0.57532
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 2.664166 0.234118 11.380 3.45e-10 ***
## DENSITY -0.033650 0.008259 -4.074 0.000591 ***
## SEASONsummer -0.812282 0.331092 -2.453 0.023450 *
## DENSITY:SEASONsummer 0.003114 0.011680 0.267 0.792533
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4079 on 20 degrees of freedom
## Multiple R-squared: 0.7135, Adjusted R-squared: 0.6705
## F-statistic: 16.6 on 3 and 20 DF, p-value: 1.186e-05
The ANCOVA showed that density was significant (F(1, 20) = 30.2, p < 0.001), while season was significant (F(1, 20) = 19.53, p < 0.001).
The density-by-season interaction was not significant (F(1, 20) = 0.07, p = 0.793).
This suggests that the relationship with density is fairly similar across seasons, although it does not mean the slopes are exactly equal.
I also used the fitted ANCOVA model to predict average egg production across a range of densities for each season. The confidence bands show the uncertainty around the fitted mean, rather than the range expected for individual limpets.
## DENSITY SEASON fit lwr upr
## 1 8.000000 spring 2.394969 2.019285 2.770654
## 2 8.373737 spring 2.382393 2.011557 2.753229
## 3 8.747475 spring 2.369817 2.003780 2.735854
## 4 9.121212 spring 2.357241 1.995952 2.718529
## 5 9.494949 spring 2.344665 1.988071 2.701258
## 6 9.868687 spring 2.332089 1.980135 2.684042
I checked the ANCOVA residuals in the same way as the ANOVA residuals. I also plotted residuals against density to look for any curved pattern or change in spread that the model might have missed.
## integer(0)
For both analyses, I used the Zuur data exploration ideas that seemed most relevant to these datasets. I checked for missing values, possible outliers, changes in spread, and patterns in the residuals. I did not remove any observations just because they looked unusual on a graph.
I also kept in mind that the independence of observations depends on how the original studies were carried out, not just on the plots.