For this assignment, I used the iris dataset to look at differences in sepal and petal width across three species. I started by exploring variation in sepal width, then fitted a linear mixed model to account for the fact that each flower has both a sepal and petal measurement.
The iris dataset originally has one row per flower, with separate columns for sepals and petals. I changed it to long format so that the measurements could be compared using the same response variable.
I first calculated some summary statistics to compare sepal width between species. I included standard deviation, variance, and interquartile range to look at the spread of the measurements.
Setosa had the highest mean sepal width and the greatest variation, while versicolor had the lowest mean width.
I also used boxplots to compare the distributions and look for possible outliers.
I fitted a mixed model with structure, species, and their interaction as fixed effects. Flower ID was included as a random intercept because sepal and petal widths were measured on the same flower.
This also allows me to compare sepal widths between species while accounting for the paired measurements.
I used estimated marginal means to compare sepal widths between the three species, followed by Tukey-adjusted pairwise comparisons.
After fitting the model, I checked the residuals for patterns, changes in spread, and departures from normality.
\\. mean?In pivot_longer(), the dot is used to separate variable
names such as Petal.Width into two parts:
Petal and Width.
A dot normally means any character in a regular expression, so the backslash escapes it and tells R to look for an actual dot. R requires two backslashes in the string to represent this pattern.
The AIC of the two-way ANOVA was 96.42.
The mixed model had an AIC of 69.92.
The mixed model had a lower AIC, suggesting that accounting for the paired measurements improved the model fit.
I compared models using untransformed width, log(width), and sqrt(width).
Because these transformations change the scale of the response, I corrected their AIC values to the original width scale before comparing them.
The sqrt(width) response had the lowest corrected AIC of -22.55, making it the preferred response among the three models.
The Q-Q plots provide an additional way to compare the residual distributions.
The ICC measures the proportion of remaining variance associated with differences between flowers, after accounting for the fixed effects.
The adjusted ICC was 0.416.
Marginal R-squared represents the variation explained by the fixed effects. Conditional R-squared includes both fixed effects and the flower-level random effect.
The marginal R-squared was 0.938, while the conditional R-squared was 0.964.
I used the model with the lowest corrected AIC to calculate predicted widths for each combination of species and structure.
These are back-transformed fixed-effect predictions, with the flower-level random intercept set to zero.
The iris data showed differences in sepal and petal widths across species. The mixed model was a better fit than the two-way ANOVA based on AIC, suggesting that accounting for measurements from the same flower was useful.
Of the three response options, sqrt(width) had the lowest corrected AIC. The fitted model was then used to estimate sepal and petal widths for each species.