當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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  

相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Perform Linear Regression Analysis in R Programming – lm() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。