Q1
您在这里做错了两件事。首先是一件坏事;通常不要深入研究模型对象并提取组件。在这种情况下,请学习使用提取器功能resid()。在这种情况下,您会得到一些有用的信息,但是如果您有不同类型的模型对象,例如来自的GLM glm(),那么mod$residuals它将包含上次IRLS迭代中的工作残差,而您通常不希望这样做!
你做错的第二件事也是让我失望的。您提取的残差(如果使用也会提取resid()),它们是原始残差或响应残差。本质上,这是拟合值和响应观察值之间的差异,仅考虑固定效应项。这些值将包含与之相同的残差自相关,这是m1因为两个模型(~ time + x)中的固定效果(或者,如果愿意,线性预测变量)相同。
要获得包含您指定的相关项的残差,您需要标准化的残差。您可以通过以下方式获得这些:
resid(m1, type = "normalized")
在?residuals.gls以下内容中描述了此(以及其他可用类型的残差):
type: an optional character string specifying the type of residuals
to be used. If ‘"response"’, the "raw" residuals (observed -
fitted) are used; else, if ‘"pearson"’, the standardized
residuals (raw residuals divided by the corresponding
standard errors) are used; else, if ‘"normalized"’, the
normalized residuals (standardized residuals pre-multiplied
by the inverse square-root factor of the estimated error
correlation matrix) are used. Partial matching of arguments
is used, so only the first character needs to be provided.
Defaults to ‘"response"’.
通过比较,这是原始(响应)的ACF和标准化残差的ACF
layout(matrix(1:2))
acf(resid(m2))
acf(resid(m2, type = "normalized"))
layout(1)

要了解发生这种情况的原因以及原始残差不包括相关项的地方,请考虑您所拟合的模型
ÿ= β0+ β1个Ť 我中号Ë + β2x +ε
哪里
ε 〜Ñ(0 ,σ2Λ )
Λρ^ρ| d|d
原始残差(默认返回resid(m2)值)仅来自线性预测变量部分,因此来自此位
β0+ β1个Ť 我中号Ë + β2X
Λ
Q2
似乎您正在尝试使用的线性函数拟合非线性趋势,time并考虑到AR(1)(或其他结构)对“趋势”的拟合不足。如果您的数据与您在此处提供的示例数据类似,那么我将适合GAM以允许协变量的平滑函数。这个模型是
ÿ= β0+ f1个(Ť 我中号ë)+ ˚F2(x)+ ε
Λ = 我
library("mgcv")
m3 <- gam(y ~ s(time) + s(x), select = TRUE, method = "REML")
在此处select = TRUE应用一些额外的收缩,以允许模型从模型中删除任何一项。
这个模型给
> summary(m3)
Family: gaussian
Link function: identity
Formula:
y ~ s(time) + s(x)
Parametric coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 23.1532 0.7104 32.59 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Approximate significance of smooth terms:
edf Ref.df F p-value
s(time) 8.041 9 26.364 < 2e-16 ***
s(x) 1.922 9 9.749 1.09e-14 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
并具有如下平滑术语:

该模型的残差也表现得更好(原始残差)
acf(resid(m3))

现在提请注意;平滑时间序列存在一个问题,因为确定函数平滑或摆动程度的方法假定数据是独立的。实际上,这意味着时间(s(time))的平滑函数可以拟合真正是随机自相关误差的信息,而不仅是潜在趋势。因此,在将平滑器拟合到时间序列数据时,您应该非常小心。
有很多方法可以解决此问题,但是一种方法是切换到适合模型,通过gamm()它lme()内部进行调用,并允许correlation您使用用于gls()模型的参数。这是一个例子
mm1 <- gamm(y ~ s(time, k = 6, fx = TRUE) + s(x), select = TRUE,
method = "REML")
mm2 <- gamm(y ~ s(time, k = 6, fx = TRUE) + s(x), select = TRUE,
method = "REML", correlation = corAR1(form = ~ time))
s(time)s(time)ρ = 0s(time)ρ > > 0.5
具有AR(1)的模型与没有AR(1)的模型相比没有明显的改进:
> anova(mm1$lme, mm2$lme)
Model df AIC BIC logLik Test L.Ratio p-value
mm1$lme 1 9 301.5986 317.4494 -141.7993
mm2$lme 2 10 303.4168 321.0288 -141.7084 1 vs 2 0.1817652 0.6699
如果我们看一下$ \ hat {\ rho}}的估算值,
> intervals(mm2$lme)
....
Correlation structure:
lower est. upper
Phi -0.2696671 0.0756494 0.4037265
attr(,"label")
[1] "Correlation structure:"
Phiρρ