r - 将 R 中的 MLE 和 NLS 函数用于非线性模型
问题描述
我在实现 MLE 和 NLS 模型以进行我正在尝试执行的一些估计时遇到了一些问题。模型如下
Y=A*(K^b_1)*(L^b_2)+e
其中 e 只是误差项,B 和 D 是输入变量。目标是尝试估计 b_1 和 b_2。我的第一个等式是,我如何将它放入 nls 函数中,因为当我尝试这样做时,我得到以下错误
prod.nls<-nls(Y~A*(K^Beta_1)*(L^Beta_2), start =list(A = 2, Beta_1= 2, Beta_2=2))
Error in numericDeriv(form[[3L]], names(ind), env) :
Missing value or an infinity produced when evaluating the model
我的另一个问题是上面的模型可以用日志重写,log(Y)= log(A)+b_1 log(K)+b_2 log(L)
我省略了错误项,因为它变得不敬而 A 只是一个标量,所以我也将其省略了。但是,当我使用 mle 函数将该模型放入 R 时,出现如下错误,
prod.mle<-mle(log(Y)~log(K)+log(L))
Error in minuslogl() : could not find function "minuslogl"
In addition: Warning messages:
1: In formals(fun) : argument is not a function
2: In formals(fun) : argument is not a function
下面提供了一个来自数据集的值的小表,以便能够重现这些错误。感谢您提前提供的帮助。
是 | ķ | 大号 |
---|---|---|
26971.71 | 32.46371 | 3013256.014 |
330252.5 | 28.42238 | 135261574.9 |
127345.3 | 5.199048 | 39168414.92 |
3626843 | 327.807 | 1118363069 |
37192.73 | 16.01538 | 9621912.503 |
解决方案
1)尝试删除 A 并仅使用其他 2 个参数进行优化。然后使用结果作为起始值进行重新优化。在 nls 的第二个应用中,我们使用 plinear 算法,它不需要线性输入的参数的起始值。使用时plinear
,右侧应该是一个矩阵,使得线性参数与每列相乘。在这种情况下,我们只有一列。
fo <- Y ~ cbind(A = (K^Beta_1) * (L^Beta_2))
st <- list(Beta_1 = 2, Beta_2 = 2)
fm0 <- nls(fo, DF, start = st)
fm1 <- nls(fo, DF, start = coef(fm0), alg = "plinear"); fm1
给予:
Nonlinear regression model
model: Y ~ cbind(A = (K^Beta_1) * (L^Beta_2))
data: DF
Beta_1 Beta_2 .lin.A
0.25399 0.81422 0.03572
residual sum-of-squares: 2.468e+09
Number of iterations to convergence: 7
Achieved convergence tolerance: 6.56e-06
2)如果我们取两边的对数,那么假设我们使用 log(A) 而不是 A 作为一个参数,那么公式在所有参数中都是线性的,因此我们可以使用lm
。请注意,这不是与原始问题完全等效的问题,尽管它可能对您来说足够接近。下面我们使用它作为获取起始值的替代方法。
fm2 <- lm(log(Y) ~ log(K) + log(L), DF)
co2 <- coef(fm2)
st2 <- list(Beta_1 = co2[[2]], Beta_2 = co2[[3]])
fm3 <- nls(fo, DF, start = st2, alg = "plinear"); fm3
给予:
Nonlinear regression model
model: Y ~ cbind(A = (K^Beta_1) * (L^Beta_2))
data: DF
Beta_1 Beta_2 .lin.A
0.2540 0.8143 0.0357
residual sum-of-squares: 2.468e+09
Number of iterations to convergence: 6
Achieved convergence tolerance: 3.744e-06
笔记
可重现形式的输入DF
是:
DF <- structure(list(Y = c(26971.71, 330252.5, 127345.3, 3626843, 37192.73
), K = c(32.46371, 28.42238, 5.199048, 327.807, 16.01538), L = c(3013256.014,
135261574.9, 39168414.92, 1118363069, 9621912.503)),
class = "data.frame", row.names = c(NA, -5L))
推荐阅读
- openstack - Openstack heat 模板将安全组附加到现有端口
- django - 为什么 docker 允许删除其他人的容器/图像?
- amazon-web-services - 如何将 AWS Glue 中经过训练的 FindMatch ML 转换从 UAT 复制到 AWS 中的 PROD 环境
- python - django:上传文件神秘问题
- c - c错误:赋值从指针生成整数而没有强制转换[-Werror=int-conversion]
- r - dplyr 变异()。变异一个变量的问题,取决于匹配由matches()选择的特定名称的其他列中的非缺失值
- python - Python-从包含数字的文件名中导入变量
- amazon-web-services - 如何解决错误加载状态:AccessDenied: Access Denied status code: 403 when trying to use s3 for terraform backend?
- python - OpenCV(4.2.0) 错误:(-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
- rust - 相互交织的范围如何创建“数据竞赛”?