我正在尝试使用 stats4 和 bbmle 包估计具有 R 中复合误差项的线性模型的参数。
到目前为止我的代码:
N <- 100
x <- runif(N)
y <- 5 * x + 3 + rnorm(N)
library("stats4")
LL <- function(beta0, beta1, sigma1, sigma2){
R = y - x*beta1 - beta0
V = suppressWarnings(dnorm(R, 0, sigma1))
U = suppressWarnings(dnorm(R, 0, sigma2))
R = V + U
std = sqrt(sigma1^2 + sigma2^2)
lampda = sigma2/sigma1
s = -1
z = length(y)
- z*log(std) + z*0.5*log(2/3.14159) + sum(dnorm((s*R*lampda/std), log =
TRUE) - (0.5*R^2/std^2))
}
library("bbmle")
fit <- mle2(LL, start = list(beta0 = 3, beta1 = 1, sigma1 = 0.1, sigma2 =
0.1))
summary(fit).
模型的输出:
Maximum likelihood estimation
Call:
mle2(minuslogl = LL, start = list(beta0 = 3, beta1 = 1, sigma1 = 0.1,
sigma2 = 0.1))
Coefficients:
Estimate Std. Error z value Pr(z)
beta0 -2.0313e+11 NA NA NA
beta1 -1.2108e+11 5.8350e-67 -2.0750e+77 < 2.2e-16 ***
sigma1 -2.1035e-09 4.4256e-51 -4.7531e+41 < 2.2e-16 ***
sigma2 5.0292e-08 4.4258e-51 1.1364e+43 < 2.2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
-2 log L: -8.009115e+90
Warning message:
In sqrt(diag(object@vcov)) : NaNs produced
我得到了微不足道的估计(估计应该至少更接近使用 OLS 估计器产生的估计),而且我得到了上面显示的警告消息。我的头现在乱了。对你的帮助表示感谢。
问候。