How is the lm function in the R language used?

In R language, the lm() function is used to fit a linear regression model. The basic syntax is as follows:

lm(formula, data)

In this case, the formula is an object used to specify the form of the linear regression model, for example, “y ~ x1 + x2” represents the linear relationship between the dependent variable y and the independent variables x1 and x2; the data is a data frame used to store the data of the dependent and independent variables.

The lm() function will return a linear regression model object, which can be analyzed using the summary() function to see the fit of the model and statistical significance.

For example, here is an example of fitting a linear regression model using the lm() function.

# 创建一个数据框
data <- data.frame(x = 1:10, y = 2*(1:10) + rnorm(10))

# 拟合线性回归模型
model <- lm(y ~ x, data = data)

# 查看模型摘要
summary(model)

Using the lm() function and the summary() function, we can obtain information on the fitting effect, coefficient estimation, significance testing, and other aspects of a linear regression model.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds