R语言 执行线性回归分析 – lm()函数
R语言中的 lm() 函数是一个线性模型函数,用于线性回归分析。
语法: lm(formula)
参数:
formula: 模型描述,如x ~ y
例1 :
# R program to illustrate
# lm function
# Creating two vectors x and y
x <- c(rep(1:20))
y <- x * 2
# Calling lm() function to
# fit a linear model
f <- lm(x ~ y)
# Getting linear model
f
输出
Call:
lm(formula = x ~ y)
Coefficients:
(Intercept) y
1.589e-15 5.000e-01
例2 :
# R program to illustrate
# lm function
# Creating two vectors x and y
x <- c(2, 4, 6, 8)
y <- c(1, 3, 5, 7)
# Calling lm() function to
# fit a linear model
f <- lm(y ~ x)
# Getting linear model
f
输出
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
-1 1