99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀R語言線性回歸
R語言線性回歸
2017-06-13
收藏

R語言線性回歸

回歸分析是一種應(yīng)用非常廣泛的統(tǒng)計工具來建立兩個變量之間的關(guān)系模型。其中一個變量被稱為預(yù)測變量,其值是通過實驗收集的。另一變量稱為響應(yīng)變量,其值是從預(yù)測變量而得。

線性回歸這兩個變量是通過一個等式,其中這兩個變量指數(shù)(冪)為1有關(guān)。 在數(shù)學(xué)上線性關(guān)系表示直線時為圖形。非線性關(guān)系,其中任何一個變量的指數(shù)不等于1創(chuàng)建曲線。

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

y = ax+b

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

y 是響應(yīng)變量

x 是預(yù)測變量

a 和 b 稱為系數(shù)常數(shù)

建立回歸步驟

回歸的一個簡單的例子是一個人的預(yù)測體重時,身高是已知的。要做到這一點,我們需要有一個人身高和體重之間的關(guān)系。

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

進(jìn)行收集身高觀測值的一個樣本和相應(yīng)的權(quán)重的實驗

使用 lm() 函數(shù)在R創(chuàng)建關(guān)系模型

找到從所創(chuàng)建的模型的系數(shù)和使用這些創(chuàng)建的數(shù)學(xué)方程

獲取關(guān)系模式的總結(jié),以知道在預(yù)測也稱殘值的平均誤差

預(yù)測新的人重量,使用 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ù)測和響應(yīng)變量之間的關(guān)系模型。

語法

下面是 lm() 函數(shù)的線性回歸的基本語法:

lm(formula,data)

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

formula 是一個符號呈遞x和y之間的關(guān)系。

data 是在其公式將被應(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ù)

語法

對于 predict()在線性回歸的基本語法是:

predict(object, newdata)

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

object 使用 lm()函數(shù)創(chuàng)建公式。

newdata 是向量包含預(yù)測變量的新值。

預(yù)測的新的人的重量

# 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é)果:

數(shù)據(jù)分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時表示是新驗證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }