欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

R語(yǔ)言線性回歸知識(shí)點(diǎn)總結(jié)

 更新時(shí)間:2021年05月06日 08:18:50   作者:w3cschool  
在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于R語(yǔ)言線性回歸知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以跟著學(xué)習(xí)下。

回歸分析是一種非常廣泛使用的統(tǒng)計(jì)工具,用于建立兩個(gè)變量之間的關(guān)系模型。 這些變量之一稱為預(yù)測(cè)變量,其值通過(guò)實(shí)驗(yàn)收集。 另一個(gè)變量稱為響應(yīng)變量,其值從預(yù)測(cè)變量派生。

在線性回歸中,這兩個(gè)變量通過(guò)方程相關(guān),其中這兩個(gè)變量的指數(shù)(冪)為1.數(shù)學(xué)上,線性關(guān)系表示當(dāng)繪制為曲線圖時(shí)的直線。 任何變量的指數(shù)不等于1的非線性關(guān)系將創(chuàng)建一條曲線。

線性回歸的一般數(shù)學(xué)方程為

y = ax + b

以下是所使用的參數(shù)的描述 

  • y是響應(yīng)變量。
  • x是預(yù)測(cè)變量。
  • a和b被稱為系數(shù)常數(shù)。

建立回歸的步驟

回歸的簡(jiǎn)單例子是當(dāng)人的身高已知時(shí)預(yù)測(cè)人的體重。 為了做到這一點(diǎn),我們需要有一個(gè)人的身高和體重之間的關(guān)系。

創(chuàng)建關(guān)系的步驟是 

  • 進(jìn)行收集高度和相應(yīng)重量的觀測(cè)值的樣本的實(shí)驗(yàn)。
  • 使用R語(yǔ)言中的lm()函數(shù)創(chuàng)建關(guān)系模型。
  • 從創(chuàng)建的模型中找到系數(shù),并使用這些創(chuàng)建數(shù)學(xué)方程
  • 獲得關(guān)系模型的摘要以了解預(yù)測(cè)中的平均誤差。 也稱為殘差。
  • 為了預(yù)測(cè)新人的體重,使用R中的predict()函數(shù)。

輸入數(shù)據(jù)

下面是代表觀察的樣本數(shù)據(jù)

# Values of height
151, 174, 138, 186, 128, 136, 179, 163, 152, 131

# Values of weight.
63, 81, 56, 91, 47, 57, 76, 72, 62, 48

LM()函數(shù)

此函數(shù)創(chuàng)建預(yù)測(cè)變量和響應(yīng)變量之間的關(guān)系模型。

語(yǔ)法

線性回歸中lm()函數(shù)的基本語(yǔ)法是

lm(formula,data)

以下是所使用的參數(shù)的說(shuō)明

  • 公式是表示x和y之間的關(guān)系的符號(hào)。
  • 數(shù)據(jù)是應(yīng)用公式的向量。

創(chuàng)建關(guān)系模型并獲取系數(shù)

x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)

# Apply the lm() function.
relation <- lm(y~x)

print(relation)

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

Call:
lm(formula = y ~ x)

Coefficients:
(Intercept)            x  
  38.4551          0.6746 

獲取相關(guān)的摘要

x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)

# Apply the lm() function.
relation <- lm(y~x)

print(summary(relation))

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

Call:
lm(formula = y ~ x)

Residuals:
    Min      1Q     Median      3Q     Max 
-6.3002   1.6629  0.0412    1.8944  3.9775 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)38.45509    8.04901 4.778  0.00139 ** 
x             0.67461    0.05191  12.997 1.16e-06 ***
---
Signif. codes:  0 ‘***' 0.001 ‘**' 0.01 ‘*' 0.05 ‘.' 0.1 ‘ ' 1

Residual standard error: 3.253 on 8 degrees of freedom
Multiple R-squared:  0.9548,    Adjusted R-squared:  0.9491 
F-statistic: 168.9 on 1 and 8 DF,  p-value: 1.164e-06

predict()函數(shù)

語(yǔ)法

線性回歸中的predict()的基本語(yǔ)法是

predict(object, newdata)

以下是所使用的參數(shù)的描述 

  • object是已使用lm()函數(shù)創(chuàng)建的公式。
  • newdata是包含預(yù)測(cè)變量的新值的向量。

預(yù)測(cè)新人的體重

# The predictor vector.
x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)

# The resposne vector.
y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)

# Apply the lm() function.
relation <- lm(y~x)

# Find weight of a person with height 170.
a <- data.frame(x = 170)
result <-  predict(relation,a)
print(result)

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

       1 
76.22869 

以圖形方式可視化回歸

# Create the predictor and response variable.
x <- c(151, 174, 138, 186, 128, 136, 179, 163, 152, 131)
y <- c(63, 81, 56, 91, 47, 57, 76, 72, 62, 48)
relation <- lm(y~x)

# Give the chart file a name.
png(file = "linearregression.png")

# Plot the chart.
plot(y,x,col = "blue",main = "Height & Weight Regression",
abline(lm(x~y)),cex = 1.3,pch = 16,xlab = "Weight in Kg",ylab = "Height in cm")

# Save the file.
dev.off()

當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果

R中線性回歸

以上就是R語(yǔ)言線性回歸知識(shí)點(diǎn)總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于R語(yǔ)言線性回歸的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論