📌  相关文章
📜  lm (1)

📅  最后修改于: 2023-12-03 15:17:24.815000             🧑  作者: Mango

Introduction to Linear Regression using 'lm'

Linear regression is a statistical modeling technique used to analyze the relationship between a dependent variable and one or more independent variables. The 'lm' function in R is used for fitting linear models.

Syntax of 'lm'

The syntax of the 'lm' function in R is as follows:

lm(formula, data, subset, weights, na.action, method = "qr",
   model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
   contrasts = NULL, offset, ...)
Arguments of 'lm'
  • formula: A formula object with a symbolic description of the model to be fitted. It has the following form: y ~ x1 + x2 + ... + xn.
  • data: A data frame containing the variables in the model.
  • subset: A logical expression specifying a subset of observations to be used in the fitting process.
  • weights: A vector of weights to be used in the fitting process.
  • na.action: A function to handle missing values in the data.
  • method: The method used to fit the model. The default method is "qr".
  • model: A logical value indicating whether the model frame should be included in the output.
  • x, y: A logical value indicating whether the model matrix and the response vector should be included in the output.
  • qr: A logical value indicating whether the QR decomposition should be returned.
  • singular.ok: A logical value indicating whether singular models should be allowed.
  • contrasts: A list or a function specifying the default contrasts to be used.
  • offset: A vector of prior weights to be used in the fitting process.
  • ...: Additional arguments that modify the fitting process.
Example of 'lm'
# Load the 'mtcars' dataset
data(mtcars)

# Fit a linear model between 'mpg' and 'wt'
fit <- lm(mpg ~ wt, data = mtcars)

# Show the model summary
summary(fit)

The output of the above code is as follows:

Call:
lm(formula = mpg ~ wt, data = mtcars)

Residuals:
    Min      1Q  Median      3Q     Max 
-4.5432 -2.3647 -0.1252  1.4096  6.8727 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
wt           -5.3445     0.5591  -9.559 1.29e-10 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.046 on 30 degrees of freedom
Multiple R-squared:  0.7528,	Adjusted R-squared:  0.7446 
F-statistic: 91.38 on 1 and 30 DF,  p-value: 1.294e-10

This shows the model summary which includes the estimated coefficients, the standard error, the t-value, and the p-value for each predictor variable. It also shows the residual standard error, the multiple R-squared, the adjusted R-squared and the F-statistic with corresponding p-value.