Practice: Functions

Here you will practice applying the function lm, which is one of the frequently-used base-R functions. Please open the help file for lm by executing the function (?function_name) you learned previously in this section. Read the help file to understand which arguments are required for the function to run and which are optional. Execute the examples given at the end of the help file. We will return to this function when introducing statistical models later in the course.

# NOT RUN {
require(graphics)

## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
## Page 9: Plant Weight Data.
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
lm.D90 <- lm(weight ~ group - 1) # omitting intercept
# }
# NOT RUN {
anova(lm.D9)
summary(lm.D90)
# }
# NOT RUN {
opar <- par(mfrow = c(2,2), oma = c(0, 0, 1.1, 0))
plot(lm.D9, las = 1)      # Residuals, Fitted, ...
par(opar)
# }
# NOT RUN {
### less simple examples in "See Also" above
# }


Source: The R Foundation for Statistical Computing, https://www.rdocumentation.org/packages/stats/versions/3.6.2/topics/lm
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.

Last modified: Wednesday, December 7, 2022, 9:50 PM