簡體   English   中英

R中的線性多元回歸

[英]Linear multivariate regression in R

我想建模一個工廠輸入的原材料,例如x噸,然后進行處理。 在第一步中,去除廢料,並產生產品P1。 對於物料的“其余”,再次對其進行處理,然后創建另一個產品P2。

問題是我想知道生產1噸產品P1需要多少原材料,生產1噸P2需要多少原材料。

我知道原材料的數量,成品P1和P2的數量,但僅此而已。

在我看來,這可以通過多元回歸建模,使用P1和P2作為因變量,使用總原材料作為自變量,並找到每個成品<1的因子。 這看起來正確嗎?

另外,如何使用R來實現呢? 從谷歌上搜索,我發現如何進行多變量回歸,但並不R.多變量回歸

提前致謝!

編輯:

嘗試使用:

datas <- read.table("datass.csv",header = TRUE, sep=",")

rawMat <- matrix(datas[,1])
P1 <- matrix(datas[,2])
P2 <- matrix(datas[,3])
fit <- lm(formula = P1 ~ rawMat)
fit

fit2 <-lm(formula = P2~rawMat)
fit2

給我的結果肯定與現實不符。 例如,Fit2返回了0,1381,其值應約為0.8。 我也該如何考慮Y1? 例如,Fit2或多或少給了我P2 / RawMat的平均值,但是RawMat是用於生產這兩種產品的原料,因此我希望P1的系數為0,8,而P1的系數大致相同。 P2的因子。

R輸出僅為:

 Coefficients:
 (Intercept)      rawMat   
  -65.6702       0.1381  

適合2。 為什么在JR的解決方案中不包含“ rawMat1”,“ rawMat2”?

EDIT2:datass.csv包含3列-第一列具有生產產品P1和P2所需的rawMaterial,第二列代表P1產生的噸數,最后一列與P2相同

多元多元回歸可以通過lm() 這是有據可查的,但是這里有一個小例子:

rawMat <- matrix(rnorm(200), ncol=2)
noise <- matrix(rnorm(200, 0, 0.2), ncol=2)
B <- matrix( 1:4, ncol=2)
P <- t( B %*% t(rawMat)) + noise 

fit <- lm(P ~ rawMat)
summary( fit )

具有摘要輸出:

Response Y1 :

Call:
lm(formula = Y1 ~ rawMat)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.50710 -0.14475 -0.02501  0.11955  0.51882 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept) -0.007812   0.019801  -0.395    0.694    
rawMat1      1.002428   0.020141  49.770   <2e-16 ***
rawMat2      3.032761   0.020293 149.445   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.1978 on 97 degrees of freedom
Multiple R-squared:  0.9964,    Adjusted R-squared:  0.9963 
F-statistic: 1.335e+04 on 2 and 97 DF,  p-value: < 2.2e-16


Response Y2 :

Call:
lm(formula = Y2 ~ rawMat)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.60435 -0.11004  0.02105  0.11929  0.42539 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.02287    0.01930   1.185    0.239    
rawMat1      2.05474    0.01964 104.638   <2e-16 ***
rawMat2      4.00162    0.01978 202.256   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.1929 on 97 degrees of freedom
Multiple R-squared:  0.9983,    Adjusted R-squared:  0.9983 
F-statistic: 2.852e+04 on 2 and 97 DF,  p-value: < 2.2e-16

編輯!:在你的情況有data.frame名為datas你可以這樣做:

datas <- data.frame( y1 = P[,1], y2=P[,2], x1 = rawMat[,1], x2 = rawMat[,2])
fit <- lm( as.matrix(datas[ ,1:2]) ~ as.matrix(datas[,3:4]) )

或者改為:

fit <- with(datas, lm( cbind(y1,y2) ~ x1+x2 ))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM