问题描述
我正在 R 中进行非线性回归,并希望向我的模型添加一个移动平均项以消除残差中的自相关.
I'm doing a non-linear regression in R and want to add one moving-average term to my model to eliminate the autocorrelations in residuals.
基本上,这是模型:
y[n] = a + log((x1[n])^g + (x2[n])^g) + c*e[n-1] + e[n]
其中 [e]
是移动平均项.
where [e]
is the moving average term.
我计划使用 ARIMA(0, 0, 1)
对残差进行建模.但是,我不知道我应该在 R 中使用哪个函数将非线性外生部分添加到 ARIMA 模型中.
I plan to use ARIMA(0, 0, 1)
to model residuals. However, I do not know which function I should use in R to add non-linear exogenous part to ARIMA model.
更多信息:我知道如何使用nls
命令来估计a
和g
,但不知道如何处理e[n]
.
More information: I know how to use nls
command to estimate a
and g
, but do not know how to deal with e[n]
.
我知道arima
中的xreg
可以处理带有线性外生变量的ARIMA 模型.是否有类似的函数来处理具有非线性外生变量的 ARIMA 模型?
I know that xreg
in arima
can handle ARIMA model with linear exogenous variables. Is there a similar function to handle ARIMA model with nonlinear exogenous variables?
提前感谢您的帮助!
推荐答案
nlme
具有这样的能力,因为它可以拟合非线性混合模型.通过允许随机效应和相关误差,您可以将其视为 nls
(仅限固定效应的非线性回归)的扩展.
nlme
has such capability, as it is fitting non-linear mixed models. You can think of it an extension to nls
(a fixed-effect only non-linear regression), by allowing random effect and correlated errors.
nlme
可以处理 ARMA 相关性,类似于 correlation = corARMA(0.2, ~ 1, p = 0, q = 1, fixed = TRUE)
.这意味着,残差是 MA(1) 过程,初始估计系数为 0.2,但要在模型拟合期间更新.~1
表明 MA(1)
处于拦截状态,没有进一步的分组结构.
nlme
can handle ARMA correlation, by something like correlation = corARMA(0.2, ~ 1, p = 0, q = 1, fixed = TRUE)
. This means, that residuals are MA(1) process, with initial guess of coefficient 0.2, but to be updated during model fitting. The ~ 1
suggests that MA(1)
is on intercept and there is no further grouping structure.
我不是 nlme
的专家,但我知道 nlme
是您所需要的.我生成了以下示例,但由于我不是专家,因此目前无法使用 nlme
工作.我把它贴在这里是为了提供一个开始/味道.
I am not an expert in nlme
, but I know nlme
is what you need. I produce the following example, but since I am not an expert, I can't get nlme
work at the moment. I post it here to give a start / flavour.
set.seed(0)
x1 <- runif(100)
x2 <- runif(100)
## MA(1) correlated error, with innovation standard deviation 0.1
e <- arima.sim(model = list(ma = 0.5), n = 100, sd = 0.1)
## a true model, with `a = 0.2, g = 0.5`
y0 <- 0.2 + log(x1 ^ 0.5 + x2 ^ 0.5)
## observations
y <- y0 + e
## no need to install; it comes with R; just `library()` it
library(nlme)
fit <- nlme(y ~ a + log(x1 ^ g + x2 ^ g), fixed = a + g ~ 1,
start = list(a = 0.5, g = 1),
correlation = corARMA(0.2, form = ~ 1, p = 0, q = 1, fixed = FALSE))
类似于nls
,我们有一个整体模型公式y ~ a + log(x1 ^ g + x2 ^ g)
,迭代过程需要起始值.我选择了 start = list(a = 0.5, g = 1)
.correlation
位已经在开头解释了.
Similar to nls
, we have an overall model formula y ~ a + log(x1 ^ g + x2 ^ g)
, and starting values are required for iteration process. I have chosen start = list(a = 0.5, g = 1)
. The correlation
bit has been explained in the beginning.
fixed
和 random
参数指定了在整个公式中应该被视为固定效应和随机效应的内容.由于我们没有随机效应,我们不指定它.我们想要 a
和 g
作为固定效果,所以我尝试了类似 fixed = a + g ~ 1
的东西.不幸的是,它并不完全有效,出于某种原因我不知道.我阅读了 ?nlme
,并认为这个公式意味着我们想要一个共同的 a
和 g
用于所有观察,但后来 nlme
报告错误,指出这不是有效的组公式.
fixed
and random
arguments in nlme
specify what should be seen as fixed effects and random effects in the overall formula. Since we have no random effect, we leave it unspecified. We want a
and g
as fixed effect, so I tried something like fixed = a + g ~ 1
. Unfortunately it does not quite work, for some reason I don't know. I read the ?nlme
, and thought this formula means that we want a common a
and g
for all observations, but later nlme
reports an error saying this is not a valid group formula.
我也在投资这个;正如我所说,以上为我们提供了一个开始.我们已经相当接近最终答案了.
I am also investing at this; as I said, the above gives us a start. We are already fairly close to the final answer.
感谢 user20650 指出我的尴尬错误.我应该使用 gnls
函数而不是 nlme
.根据 nlme
包的设计性质,函数 lme
和 nlme
必须采用 random
参数才能工作.幸运的是,nlme
包中还有其他几个用于扩展线性模型和非线性模型的例程.
Thanks to user20650 for point out my awkward error. I should use gnls
function rather than nlme
. By design nature of nlme
package, functions lme
and nlme
have to take a random
argument to work. Luckily, there are several other routines in nlme
package for extending linear models and non-linear models.
gls
和gnls
通过允许非对角方差函数扩展lm
和nls
.
gls
andgnls
extendlm
andnls
by allowing non-diagonal variance functions.
所以,我真的应该使用 gnls
代替:
So, I should really use gnls
instead:
## no `fixed` argument as `gnls` is a fixed-effect only
fit <- gnls(y ~ a + log(x1 ^ g + x2 ^ g), start = list(a = 0.5, g = 1),
correlation = corARMA(0.2, form = ~ 1, p = 0, q = 1, fixed = FALSE))
#Generalized nonlinear least squares fit
# Model: y ~ a + log(x1^g + x2^g)
# Data: NULL
# Log-likelihood: 92.44078
#
#Coefficients:
# a g
#0.1915396 0.5007640
#
#Correlation Structure: ARMA(0,1)
# Formula: ~1
# Parameter estimate(s):
# Theta1
#0.4184961
#Degrees of freedom: 100 total; 98 residual
#Residual standard error: 0.1050295
这篇关于R中具有非线性外生变量的ARIMA模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!