如何计算自变量的最佳数量以获得最小的模型输出?

时间:2019-04-24 20:03:15

标签: r machine-learning optimization model

在RI中,我制作了一个具有10个变量的多元线性回归模型,在获得具有系数的模​​型之后,我想知道如何计算每个自变量的最小值,该最小值将为我提供输出的全局最小值” y”来自模型?

我尝试阅读的优化软件包做到了这一点,但出现错误

 Error in fn(par, ...) : could not find function "fn"

### calculate model
fit <- lm(Y ~., data=df2)

### create equation in function with parameters and coefficients to get ### global mínimum and with which values each value gets to that mínimum

f <- function(X,X1,X2,X3,X4,X5,X6,X7,X8,X9) -210.50200438 - 1.08319034 * X +  0.07467384 * X1  + 0.02106764 * X2 + 0.08280658 * X3 + 2.07585559 * X4 + 5.09223561 * X5 + -7.57592770 * X6 + 1.62970544 * X7 + 0.45169896 * X8  - 0.32157245 * X9

### Variables
X <- seq(19,75,by=1)
X1 <- seq(3780,9183,by=100)
X2 <- seq(7534,15840,by=100)
X3 <- seq(2810,5100,by=100)
X4 <- seq(185,596,by=1)
X5 <- seq(1.20,48.1,by=0.1)
X6 <- seq(38,78,by=1)
X7 <- seq(33,100,by=1)
X8 <- seq(160,358,by=1)
X9 <- seq(42,458,by=1)
z <- outer(X,X1,X2,X3,X4,X5,X6,X7,X8,X9,f) 
optim(X,X1,X2,X3,X4,X5,X6,X7,X8,X9,f)

我想知道什么是“ y”的全局最小值,以及每个独立变量的值才能达到该最小值,但是我误以为是错误的,如果那不是我应该使用的,请告诉我我怎么能做到这一点。

谢谢

1 个答案:

答案 0 :(得分:0)

与@Luis的评论一样,如果系数为正,则取变量的最小值,如果系数为负,则取最大值。

### Variables
X <- seq(19, 75, by = 1)
X1 <- seq(3780, 9183, by = 100)
X2 <- seq(7534, 15840, by = 100)
X3 <- seq(2810, 5100, by = 100)
X4 <- seq(185, 596, by = 1)
X5 <- seq(1.20, 48.1, by = 0.1)
X6 <- seq(38, 78, by = 1)
X7 <- seq(33, 100, by = 1)
X8 <- seq(160, 358, by = 1)
X9 <- seq(42, 458, by = 1)

f <- function(X, X1, X2, X3, X4, X5, X6, X7, X8, X9)
  (- 210.50200438 
   - 1.08319034 * X 
   + 0.07467384 * X1  
   + 0.02106764 * X2 
   + 0.08280658 * X3 
   + 2.07585559 * X4 
   + 5.09223561 * X5
   - 7.57592770 * X6 
   + 1.62970544 * X7 
   + 0.45169896 * X8  
   - 0.32157245 * X9)

f(max(X), min(X1), min(X2), min(X3), min(X4),  min(X5), max(X6), min(X7), min(X8), max(X9))