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
相关用法
注:本文由纯净天空筛选整理自Kanchan_Ray大神的英文原创作品 Perform Linear Regression Analysis in R Programming – lm() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。