我尝试使用stats4和bbmle包估计R中具有复合误差项的线性模型的参数。
对于型号规格,请单击
Linear model specification和Log Likelihood function specification
到目前为止我的代码:
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估算器生成的估算),并且我得到上面显示的警告信息。我的头现在搞砸了。 非常感谢您的帮助。
问候。