R

时间:2015-06-22 09:49:04

标签: r

我想模拟工厂接受x吨原材料的输入,然后进行处理。在第一步中,除去废料,并产生产品P1。对于"休息"对材料进行再次处理,然后创建另一个产品P2。

问题在于我想知道生产1吨产品P1需要多少原材料以及生产1吨P2需要多少原材料。

我知道原料的数量,成品P1和P2的数量,但仅此而已。

在我看来,这可以通过多元回归建模,使用P1和P2作为因变量,将总原料作为自变量,并找出每个成品的因子< 1。这看起来是对的吗?

另外,如何使用R实现这一目标?通过谷歌搜索,我发现了如何进行多变量回归,但没有在R中进行多 variate 回归。

提前致谢!

编辑:

尝试使用:

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与用于生产两种产品的原材料相同,所以我希望有一些像0,8这样的因子用于P1,并且大约相同P2的因素。

R输出仅为:

 Coefficients:
 (Intercept)      rawMat   
  -65.6702       0.1381  

for fit2。为什么它不包括&#34; rawMat1&#34;,&#34; rawMat2&#34;如J.R。的解决方案?

EDIT2:datass.csv包含3列 - 第一列包含产生P1和P2所需的rawMaterial,第二列表示P1产生的吨数,最后一列表示P2的相同

1 个答案:

答案 0 :(得分:2)

多变量多元回归可以由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

EDIT!: 对于名为data.frame的{​​{1}},您可以执行以下操作:

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]) )
相关问题