Linear Regression in R: Step-by-Step Guide

In R language, performing linear regression analysis typically requires using the lm() function, which is used to fit a linear model. Here is a simple example:

Suppose there is a dataset called data, which includes independent variable x and dependent variable y, you can perform linear regression analysis using the following code:

# 创建一个数据集
data <- data.frame(x = c(1, 2, 3, 4, 5), y = c(2, 4, 6, 8, 10))

# 使用lm()函数进行线性回归分析
model <- lm(y ~ x, data = data)

# 查看线性回归模型的摘要
summary(model)

The code above first creates a dataset called data, then fits a linear regression model of y on x using the lm() function, and stores the results in the model object. Finally, the summary() function can be used to view the summary information of the linear regression model, including regression coefficients, intercept, R-squared, and other statistical indicators.

bannerAds