The “lm” Function in R
Package: Base R (no specific package required)
Purpose: Fits linear models to data.
General Class: Statistical Modeling
Required Argument(s):
formula: A symbolic description of the model to be fitted. In the form response ~ terms.
data: An optional data frame in which to interpret the variables.
Notable Optional Arguments:
subset: An optional vector specifying a subset of observations to be used in the fitting process.
weights: An optional vector of weights to be used in the fitting process.
na.action: A function that indicates what should happen when data contain missing values.
method: The fitting method to be used. The default is “qr” for the QR decomposition method.
...: Additional arguments to be passed to or from methods.
Example:
# Example data
set.seed(123)
x <- rnorm(100)
y <- 2*x + rnorm(100)
# Fit a linear model
model <- lm(y ~ x)
# Display the summary of the model
summary(model)In this example, the lm function is used to fit a linear model with response variable y and predictor variable x. The summary of the model is then displayed, showing coefficients, standard errors, t-values, and other relevant information.