Checking residuals (from ETS+STL method) with checkresiduals() function - r

I have one ts object which contain one column with weekly data (freqency = 52) for the period 2016-2019(only one week from 2019).
#>TEST_1
#>Time Series:
#>Start = c(2016, 1)
#>End = c(2019, 1)
#>Frequency = 52
So I am performing forecast with this ts object with, function forcast() from forecast package.This function give me selection of best model ETS (Exponential smoothing) for my series.
Forecast method: STL + ETS(M,A,N)
Model Information:
ETS(M,A,N)
Call:
ets(y = x, model = etsmodel, allow.multiplicative.trend = allow.multiplicative.trend)
Smoothing parameters:
alpha = 0.0044
beta = 0.0044
Initial states:
l = 496.0001
b = -0.7495
sigma: 0.2538
AIC AICc BIC
2328.009 2328.406 2343.290
But here arise a new problem for me. Namely I trying to perform residual diagnostics for residuals from this model with function checkresiduals() but I receive this message.
#> Warning message:
#> In modeldf.default(object) :
#> Could not find appropriate degrees of freedom for this model.
So can anybody help me how to find appropriate degrees of freedom for this model
with checkresiduals() function? Below is data from residuals.
residuals<-structure(c(103.861587225712, 232.922530738897, -177.501044573567,
-32.3310448885088, 51.8658720663952, -127.669525632371, -21.3736988850188,
31.8283388622758, 134.388167819753, -202.279672375648, -150.211885150427,
59.7872220312138, 7.21928088178879, -31.0067512774922, 240.664063232754,
-259.693899860492, 51.2068097649542, 133.051059120384, 153.754774108432,
-245.448120335887, -41.7151580882252, 329.736089553496, -176.574681226445,
-5.49877539363433, -57.9440644242901, -141.920372666123, 59.631632197218,
30.3566233456523, -19.5674149569647, 49.8299466802158, 8.08039437858747,
-179.219757481181, 61.6262480548803, 14.2886335749734, 147.521659709062,
-203.114556948222, 232.39658682842, 17.0359701527633, 122.671792930753,
1.17404214154658, -21.3604900851155, 43.6067134825538, 56.6694972222097,
-74.206099457236, 22.2154797604099, -42.6209506582884, -69.0881062270763,
44.9935627424999, -65.4843011281191, 45.9859871219855, 38.48475732006,
217.607886572158, -81.752879329815, -62.3165846738133, 91.3280029935076,
13.8065979268541, -27.5160607993942, -2.45614326754531, 8.82428074173083,
-21.9816546447523, 58.6350169306539, 2.99591624137327, 25.4548944489055,
-7.80971451574547, -33.741824891111, 148.727324165574, -103.887619405031,
13.6976122890256, -6.22642628362576, -89.0151943344358, 151.68500527824,
113.373271376477, 165.103295852743, -295.039665234726, 213.698114407198,
-76.4034402042766, -9.34573346398901, -71.4103830503603, 122.800589573655,
-55.724016585403, 63.7939569095491, 44.9784699409192, 151.519180259845,
-58.4408170188741, -74.3037359893916, -47.7713298497972, 163.367074626196,
-249.379445021869, -112.112655284116, -43.5458433646284, -53.5666005867634,
281.491207440336, -121.212142480196, -33.9138735682901, -31.1438180301793,
-31.2555698825003, 20.3181357200996, -46.2564548372715, 19.2769399131227,
82.0903051423776, -53.9874588993755, -81.7381076026692, -109.42037514781,
-128.567530337503, 239.606771386708, -163.928615298084, 88.3650587021525,
22.3840519205474, -19.7936259061341, 133.392615761316, 14.8789465334592,
-7.35384302392632, -193.309220279654, 199.807229000058, 124.081926626315,
-52.3795507957004, 26.248230162833, -123.352126375918, -136.687848362162,
242.06397333675, -49.2896526387001, -47.0413692896267, -315.639803224046,
122.111855110991, -135.453045844048, -34.9514109509343, -51.0671430546247,
75.2304903204274, 58.5168476811577, 205.900859581612, -195.231017102347,
17.0666471041718, -55.7835085816988, -105.931678098968, -173.52733115843,
229.313605012801, 4.76417288414814, 24.9291766474627, -324.904858037879,
449.500524512662, -126.709163220759, 18.7291455153395, -76.1328146141673,
-298.217791616455, 137.973841964018, -16.2916958267025, -31.8650948708939,
99.4876416447454, -49.4760819558044, 84.1071094148195, 44.155870901787,
-133.53348599245, 117.30321085781, 35.0222913102854, 71.5981819455558,
-87.2032279610021, -272.900607282635), .Tsp = c(2016, 2019, 52
), class = "ts")

The degrees of freedom are: DF = (# lags to be tested) - (# parameters that you estimate).
You can manually set the degrees of freedom using:
checkresidual(..., df=DF)
Hope it works.
Goosse

Related

How to get the fitted values of the underlying model run in CADFtest in R?

I'm using the package CADFtest in R, and I've some issues retrieving the fitted values of the underlying regression model on which Hansen's ADF test is implemented. Here is an example from the help page:
install.packages("CADFtest")
require(CADFtest)
data(npext, package="urca")
npext$unemrate <- exp(npext$unemploy) # compute unemployment rate
L <- ts(npext, start=1860) # time series of levels
D <- diff(L) # time series of diffs
S <- window(ts.intersect(L,D), start=1909) # select same sample as Hansen's
CADFt <- CADFtest(L.gnpperca~D.unemrate, data=S, max.lag.y=3,
kernel="Parzen", prewhite=FALSE)
Running CADFt returns:
> CADFt
CADF test
data: L.gnpperca ~ D.unemrate
CADF(3,0,0) = -3.413, rho2 = 0.063515, p-value = 0.001729
alternative hypothesis: true delta is less than 0
sample estimates:
delta
-0.08720302
Which is a model with 3 lagged dependent variables. I'd like to extract the fitted values of this model. So I run
> CADFt$est.model$fitted.values
Time Series:
Start = 5
End = 80
Frequency = 1
[1] 0.0005662303 -0.0733646249 -0.0126602690 0.0697401891 0.0143332199 0.0857608955 -0.0086049916 -0.0558618079 -0.1410753680 0.1223887323 0.0880077675 -0.0201279048 0.0552825504 0.0245985124 -0.0157488507
[16] -0.0089621034 0.0251877185 -0.1101706369 -0.1229540835 -0.1562289335 0.0118467889 0.0942232745 0.0830000974 0.1186187635 0.0906191863 -0.0591099094 0.0709325962 0.0693442793 0.1384204645 0.1287152465
[31] 0.0814823568 0.0201579212 -0.0101185970 -0.0409656385 0.0056581976 -0.0016591031 -0.0236250458 0.0387140051 0.0515742055 0.0306409595 0.0187764792 -0.0455932936 0.0433190872 0.0115143178 0.0218779596
[46] -0.0395455081 0.0481404678 0.0139747978 0.0043631927 0.0447783346 0.0148757356 0.0371630123 0.0325870648 0.0340326292 0.0171292682 0.0220503307 0.0121438056 -0.0129218323 -0.0055278586 0.0180975211
[61] 0.0320353864 0.0026341644 -0.0411863408 0.0312942735 0.0272606802 0.0450317222 0.0238727288 -0.0078678757 0.0073786290 -0.0282459788 0.0291874998 0.0613006522 0.0330602986 0.0320649803 0.0375408342
[76] 0.0360327436
However, the initial dependent variable L.gnpperca has observations that are very different:
> S[,8]
Time Series:
Start = 1909
End = 1988
Frequency = 1
[1] 7.163172 7.170120 7.179308 7.219642 7.208600 7.144407 7.121252 7.183112 7.177019 7.293698 7.244942 7.181592 7.070724 7.204149 7.301148 7.279319 7.345365 7.388946 7.374002 7.367709 7.421177 7.306531 7.218177
[24] 7.050989 7.026427 7.106606 7.193686 7.317212 7.362645 7.302496 7.376508 7.450080 7.589336 7.699842 7.809947 7.867489 7.839132 7.701200 7.673223 7.699842 7.683404 7.758761 7.818028 7.830823 7.858254 7.826443
[47] 7.882315 7.883069 7.879291 7.851272 7.896553 7.900637 7.903596 7.951911 7.976595 8.015988 8.064951 8.116417 8.131236 8.166784 8.182280 8.165079 8.178548 8.216426 8.257559 8.243015 8.220508 8.258717 8.294266
[70] 8.335247 8.348691 8.335095 8.343870 8.307756 8.333102 8.389362 8.412748 8.430085 8.456477 8.490848
I wonder whether the fitted value refers to the first-differenced model. How to obtain the actual fitted values? Thank you.

ARIMA forecasts are way off

I am using ARIMA (auto.arima) to forecast for 52 weeks. The time series model fits the data well (see plot below, red line is the fitted value). The input data has a decreasing trend.
The forecasts (highlighted area) however seems to just taking off after the actual values end.
How can the forecasts be tamed?
dput of the input
> dput(baseTs)
structure(c(5.41951956469523, 5.49312499014084, 5.56299025716832,
5.64442852110163, 5.71385023974044, 5.77578632033402, 5.82985917237953,
5.86346591034374, 5.89626165157029, 5.92013286862512, 5.94200331713403,
5.93996840759539, 5.93917517855891, 5.90355191030718, 5.87180377346416,
5.83190030607801, 5.79624428055153, 5.75377043604686, 5.71445345904649,
5.70025269940165, 5.69789272204017, 5.73728731204876, 5.77015169357394,
5.78936321107329, 5.80113284575595, 5.79449448552444, 5.78193215198878,
5.74003482344406, 5.71694163930612, 5.66689345413153, 5.614357635737,
5.58578389962286, 5.55824727570498, 5.58495146060423, 5.61344117957187,
5.63637441850401, 5.65948408172102, 5.65558124383951, 5.64909390802285,
5.6664546352889, 5.68205689033408, 5.69991437586231, 5.72273650369514,
5.72006065065194, 5.71556512542993, 5.6717608006789, 5.64610326418084,
5.57193975508467, 5.49406607804055, 5.40126523530993, 5.31513540386482,
5.238437956722, 5.15362077920702, 5.11960611878249, 5.08498887979172,
5.08408134201562, 5.07361213981111, 5.04830559379816, 5.01401413448689,
5.0418662607737, 5.06947584464062, 5.08771495309317, 5.10587165060358,
5.1438369937098, 5.1815251206981, 5.2318657906363, 5.29385492077065,
5.29652029253008, 5.29998067741868, 5.28242409629194, 5.2722770646788,
5.24927444462166, 5.22226735874711, 5.16555064465208, 5.10956459841778,
5.09439240612378, 5.07617974794969, 5.04418337811006, 5.0075619037348,
4.99108423417745, 4.9874504485194, 4.99135285004736, 4.99217791657733,
4.94874445528885, 4.90320874819525, 4.84508278068469, 4.79086127023963,
4.75236840849279, 4.71431573721527, 4.71936529020481, 4.72422850167074,
4.72203091743033, 4.71732868614755, 4.71175323610448, 4.70566162766782,
4.71165837247331, 4.71767529028615, 4.75129316683193, 4.7863855803437,
4.85248191548789, 4.91865394024373, 4.9590849617955, 4.99960686851895,
5.02020678181827, 5.04201201976595, 5.02025906892952, 4.99735920720967,
4.92520279823639, 4.84822505567723, 4.81118504683572, 4.77330440072099,
4.72636395544651, 4.6861111959621, 4.64912520396312, 4.61348981514599,
4.58517820348434, 4.56378688913207, 4.549011597464, 4.52900600122321,
4.56028365470815, 4.60248987909752, 4.65628990381626, 4.70496326660038,
4.73779351647955, 4.76616725791407, 4.79569018347378, 4.83185281078024,
4.85177852259102, 4.87488251014986, 4.89468916229158, 4.9077984323135,
4.92375782591088, 4.96363767543938, 5.05416277704822, 5.1426680212522,
5.232495043331, 5.32153608753653, 5.41780853915163, 5.51131526881126,
5.62791210324026), .Tsp = c(2015.05769230769, 2017.73076923077,
52), class = "ts")
The code used
fc <- try(auto.arima(baseTs,ic='aic',approximation = F))
baseFc <- forecast(fc,h = weeks_forecasted)
baseVolume_forecast_new <- baseFc$mean
What could be the reason behind the forecasts exploding?

Estimating DIC for a zoib beta regression model

I have the data and code below and would appreciate your help to estimate DIC (or AIC) for a beta regression model with zoib:
library(zoib)
data("GasolineYield", package = "zoib")
re.md <- zoib(yield ~ temp | 1 | 1, data=GasolineYield,
joint = FALSE, random=1, EUID=GasolineYield$batch,
zero.inflation = FALSE, one.inflation = FALSE,
n.iter=3200, n.thin=15, n.burn=200)
sample2 <- re.md$coeff
summary(sample2)
Following the package's vignette and the article published by the authors, I implemented the code below to get DIC, but I get an error:
dic.samples(sample2, n.iter=201, thin = 1, type="pD")
Error in nchain(model) : Invalid JAGS model object in nchain
I ran the same code with an object from coda.samples {rjags} documentation and it worked:
data(LINE)
LINE$recompile()
LINE.out <- coda.samples(LINE, c("alpha","beta","sigma"), n.iter=1000)
summary(LINE.out)
dic.samples(LINE, n.iter=201, thin = 1, type="pD")
Then I checked the class of each of the objects above LINE, sample2, re.md, and LINE.out, and the output is:
class(LINE)
"jags"
class(re.md)
"list"
class(sample2)
"mcmc.list"
class(LINE.out)
"mcmc.list"
This suggests that my error is probably because the object sample2 is not of class jags.
Therefore, I would appreciate any ideas on how I can get sample2 in a form that can be accepted by dic.samples in order to be able to get DIC (or AIC) for my rd.md model.

How can I use TexReg (1.36.4) for a "relogit" model estimated using Zelig (v. 5.0-13)?

I know that Zelig is a wrapper... But still, it provides nice simulation capabilities (which I wouldn't be able to do on my own).
Lets say I have this data,
set.seed(123)
x1 = rnorm(5)
x2 = rnorm(5)
z = 1 + 2*x1 + 3*x2
pr = 1/(1+exp(-z))
y = rbinom(5,1,pr)
df = data.frame(y=y,x1=x1,x2=x2)
Now, we estimate the model,
library(Zelig)
relogit <- zelig(y ~ x1 + x2, model = "relogit", data = df)
And now, we (try to) make the table
library(texreg)
texreg(relogit)
... only to get this error.
Error in (function (classes, fdef, stable):
unable to find an inherited method for function ‘extract’ for
signature ‘"Zelig-relogit"’
I am aware of the $getvcov() and $getcoef() functions. But I wonder how I could make a straightforward table using texreg. Any advice will be greatly appreciated. Thanks!
texreg uses a generic function called extract to pull the relevant data from a model object and then processes the resulting texreg object to create a regression table. In order to extend the range of models texreg is applicable to, you can write your own methods for the extract function.
Zelig-relogit objects apparently store a glm object with the relevant data somewhere inside the object and attach a different class name to it. So it should be relatively straightforward to create a copy of this sub-object, fix its class name, and apply the existing extract.glm method to this object to extract the data. More specifically:
# extension for Zelig-relogit objects (Zelig package >= 5.0)
extract.Zeligrelogit <- function(model, include.aic = TRUE, include.bic = TRUE,
include.loglik = TRUE, include.deviance = TRUE, include.nobs = TRUE, ...) {
g <- model$zelig.out$z.out[[1]]
class(g) <- "glm"
e <- extract(g, include.aic = include.aic, include.bic = include.bic,
include.loglik = include.loglik, include.deviance = include.deviance,
include.nobs = include.nobs, ...)
return(e)
}
setMethod("extract", signature = className("Zelig-relogit", "Zelig"),
definition = extract.Zeligrelogit)
This code creates a Zelig-relogit method for the extract function. You can use it by typing something like screenreg(relogit), where relogit is the name of your Zelig-relogit object. The result should look like this:
==================================
Model 1
----------------------------------
(Intercept) -9446502571.59 ***
(62615.78)
x1 19409089045.70 ***
(141084.20)
x2 856836055.47 ***
(98175.65)
----------------------------------
AIC 6.00
BIC 4.83
Log Likelihood -0.00
Deviance 0.00
Num. obs. 5
==================================
*** p < 0.001, ** p < 0.01, * p < 0.05
More generally, if you want to make any Zelig model work with texreg, you should look at model$zelig.out$z.out[[1]] to find the relevant information. I will include the Zelig-relogit extract method in the next texreg release.

Different output from auto.arima when parallelization is enabled

The information outputted by auto.arima() when enabling parallelization seems to differ from that outputted when not using parallelization. Specifically, when parallelization is enabled, the output does not specify the values of p, d, and q nor report the AICc or BIC information criteria.
Example without parallelization:
> model <- auto.arima(ts,xreg=tax.ts,ic=c("bic"),stepwise=FALSE,approximation=FALSE)
Series: ts
ARIMA(0,1,1)
Coefficients:
ma1 tax
0.4589 -0.7035
s.e. 0.0806 0.6969
sigma^2 estimated as 0.6028: log likelihood=-138.36
AIC=282.72 AICc=282.92 BIC=291.08
With paralellization:
> model <- auto.arima(ts,xreg=tax.ts,ic=c("bic"),stepwise=FALSE,approximation=FALSE,parallel=TRUE,num.cores=12)
Call:
auto.arima(x = structure(list(rwhitebread = c(89.2749786376953, 89.2966079711914,
88.9023666381836, 88.6773300170898, 89.0641860961914, 89.6114883422852, 89.5789642333984,
89.3341522216797, 89.0179290771484, 88.4036026000977, 88.309211730957, 87.6592483520508,
87.3339614868164, 87.2614440917969, 87.7638092041016, 87.3240356445312, 88.6289749145508,
90.0923461914062, 90.2542266845703, 90.6454772949219, 90.7220764160156, 90.4558944702148,
90.8726654052734, 91.7483978271484, 92.7035140991211, 92.5734100341797, 92.4802780151367,
92.6397476196289, 93.3086471557617, 92.8956298828125, 92.6798477172852, 94.3108444213867,
96.7846298217773, 99.8294296264648, 100.150718688965, 103.013710021973, 103.426765441895,
102.606864929199, 102.679161071777, 103.586181640625, 106.069953918457, 105.177772521973,
104.857940673828, 104.737091064453, 104.414154052734, 103.719192504883, 101.936988830566,
101.443199157715, 101.572723388672, 101.843193054199, 101.829467773438, 101.567581176758,
102.02978515625, 101.891319274902, 101.730346679688, 101.729293823242, 101.258888244629,
100.753799438477, 100.468338012695, 100.157859802246, 99.6511154174805, 99.5886764526367,
98.7140121459961, 98.9120025634766, 99.6159439086914, 100.186874389648, 99.477912902832,
99.2161636352539, 99.1324768066406, 99.4329071044922, 99.8656692504883, 100.098243713379,
100.050888061523, 100.484085083008, 100.259925842285, 101.157867431641, 102.265960693359,
102.379020690918, 102.103042602539, 102.0927734375, 101.972709655762, 101.002868652344,
99.5803604125977, 99.2621994018555, 100.411376953125, 101.165588378906, 101.440376281738,
101.992752075195, 102.306083679199, 102.090476989746, 101.909233093262, 105.202362060547,
108.684272766113, 108.776985168457, 108.79345703125, 108.924339294434, 108.688865661621,
108.240608215332, 107.647567749023, 106.971420288086, 107.167625427246, 106.995262145996,
107.178039550781, 107.176040649414, 106.977165222168, 106.254096984863, 104.939926147461,
104.52613067627, 104.082489013672, 104.322189331055, 103.974395751953, 104.827789306641,
105.576438903809, 105.718490600586, 105.364852905273, 105.030937194824, 104.75341796875,
103.981002807617, 103.218376159668, 102.868370056152, 102.812316894531)), .Names = "rwhitebread", row.names = c(NA,
-121L), class = "data.frame"), ic = c("bic"), stepwise = FALSE, approximation = FALSE,
xreg = tax.ts, parallel = TRUE, num.cores = 12)
Coefficients:
ma1 tax
0.4589 -0.7035
s.e. 0.0806 0.6969
sigma^2 estimated as 0.6028: log likelihood = -138.36, aic = 282.72
Is there any way to get the same output from the parallelized command, or otherwise extract the parameters used in the model and the AICc/BIC?

Resources