GARCH Optimization in R - r

I'm trying to fit a GARCH(1,1) model using rugarch library in R to predict S&P500 returns for the next 25 days. I have hard coded the ARIMA p and q to be 4 and 4 respectively for this example. However, each time I run this, it comes up as an optimization error.
Here is the code:
getSymbols("^GSPC", from="2016-01-01")
asset = diff(log(Cl(GSPC)))
returns = as.numeric(asset)
spec = ugarchspec(
variance.model=list(garchOrder=c(1,1)),
mean.model=list(armaOrder=c(
4,4
), include.mean=T),
distribution.model="sged"
)
fit = ugarchfit(
spec, returns, solver = 'hybrid')
and here is the error:
Error in optim(init[mask], armaCSS, method = optim.method, hessian =
TRUE, :
initial value in 'vmmin' is not finite

Related

How to correctly interpret glmmTMB models with large z statistics/conflicting error messages?

I am using glmmTMB to run a zero-inflated two-component hurdle model to determine how certain covariates might influence (1) whether or not a fish has food in its stomach and (2) if the stomach contains food, which covariates effect the number of prey items found in its stomach.
My data consists of the year a fish was caught, the season it was caught, sex, condition, place of origin, gross sea age (1SW = one year at sea, MSW = multiple years at sea), its genotype at two different loci, and fork length residuals. Data are available at my GitHub here.
Model interpretation
When I run the model (see code below), I get the following warning message about unusually large z-statistics.
library(glmmTMB)
library(DHARMa)
library(performance)
set.seed(111)
feast_or_famine_all_prey <- glmmTMB(num_prey ~ autumn_winter+
fishing_season + sex+ condition_scaled +
place_of_origin+
sea_age/(gene1+gene2+fork_length_residuals) + (1|location),
data = data_5,
family= nbinom2,
ziformula = ~ .,
dispformula = ~ fishing_season + place_of_origin,
control = glmmTMBControl(optCtrl = list(iter.max = 100000,
eval.max = 100000),
profile = TRUE, collect = FALSE))
summary(feast_or_famine_all_prey_df)
diagnose(feast_or_famine_all_prey_df)
Since the data does display imbalance for the offending variables (e.g. mean number of prey items in autumn = 85.33, mean number of prey items in winter = 10.61), I think the associated model parameters are near the edge of their range, hence, the extreme probabilities suggested by the z-statistics. Since this is an actual reflection of the underlying data structure (please correct me if I'm wrong!) and not a failure of the model itself, is the model output safe to interpret and use?
Conflicting error messages
Using the diagnose() function as well as exploring model diagnostics using the DHARMa package seem to suggest the model is okay.
diagnose(feast_or_famine_all_prey_df)
ff_all_prey_residuals_df<- simulateResiduals(feast_or_famine_all_prey_df, n = 1000)
testUniformity(ff_all_prey_residuals_df)
testOutliers(ff_all_prey_residuals_df, type = "bootstrap")
testDispersion(ff_all_prey_residuals_df)
testQuantiles(ff_all_prey_residuals_df)
testZeroInflation(ff_all_prey_residuals_df)
However, if I run the code performance::r2_nakagawa(feast_or_famine_all_prey_df) then I get the following error messages:
> R2 for Mixed Models
Conditional R2: 0.333
Marginal R2: 0.251
Warning messages:
1: In (function (start, objective, gradient = NULL, hessian = NULL, :
NA/NaN function evaluation
2: In (function (start, objective, gradient = NULL, hessian = NULL, :
NA/NaN function evaluation
3: In (function (start, objective, gradient = NULL, hessian = NULL, :
NA/NaN function evaluation
4: In fitTMB(TMBStruc) :
Model convergence problem; non-positive-definite Hessian matrix. See vignette('troubleshooting')
5: In fitTMB(TMBStruc) :
Model convergence problem; false convergence (8). See vignette('troubleshooting')"
None of these appeared using diagnose() nor were they (to the best of my knowledge) hinted at by the DHARMa diagnostics. Should these errors be believed?
Short answer: when you run performance::r2_nakagawa it refits the model with the fixed effects components removed. It's possible that your R^2 estimates are unreliable, but this shouldn't affect any of the other model results.
(update after much digging):
The code descends through these functions:
performance::r2_nakagawa
performance:::.compute_random_vars
insight::get_variance
insight:::.compute_variances
insight:::.compute_variance_distribution
insight:::.variance_distributional
insight:::null_model
insight:::.null_model_mixed
at which point it tries to run a null model with no fixed effects (num_prey ~ (1 | location)). This is where the warnings are coming from.
When I run your code I get R^2 values of 0.308/0.237, which does suggest that this is a somewhat unstable calculation (not that these differences would really change the conclusion much).

Durbin Watson test on the residuals of a mixed model

I am trying to calculate the temporal autocorrelation of a poison distributed mixed model, and was wondering how to do so. I get an error that says "$ operator not defined for this S4 class" I can successfully run the the dwtest on a linear model, with a poisson distribution, but not the one I really want.
Successful model and code:
temp.nem.cuc.glm<-glm(AllDat$nem.cuc~ AllDat$year.collected, family=poisson(link="log"))
summary(temp.nem.cuc.lm)
time<-AllDat$year.collected
dwnem.cuc<-dwtest(temp.nem.cuc.lm, order.by = time, alternative = "two.sided", iterations = 50, exact = FALSE, tol = 1e-10)
dwnem.cuc
Unsuccessful model and code
#the model I am really interested in
nem.cuc.pois=glmer(nem.cuc~ I(year.collected-1930)+I(standard.length..mm./100) + (1|sites1), family = "poisson", data=AllDat)
time<-AllDat$year.collected
dwnemresid.cuc<-dwtest(nem.cuc.pois, order.by = time, alternative = "two.sided", iterations = 50, exact = FALSE, tol = 1e-10)
dwnem.cuc
For future reference, I found the "check_autocorrelation" function from the performance package, but it returns only whether it detected autocorrelation or not.
I would also be interested in finding a DW test that returns a statistic, if anyone knows one - or in a way to solve the S4 class error, since I encounter it repeatedly.

Error with fitting a Generalized Extreme Value (GEV) using `extRemes` in R?

I have some data and I want to fit a Generalized Extreme Value (GEV) distribution using extRemes package in R. However, an error occurs:
library(extRemes)
Mydata = c(6,3,3,3,5,5,4,3,5,5,4,3,4,4,6,5,5,4,5,2,6,4,6,5,3,3,8,3,4,4,6,6,6,6,6,5,6,6,5,5)
fit_gev <- fevd(x=Mydata, method = "MLE", type="GEV", period.basis = "year")
summary(fit_gev)
Error in diag(cov.theta) : invalid 'nrow' value (too large or NA)
In addition: Warning message:
In diag(cov.theta) : NAs introduced by coercion
I wonder how can I fix this error? Thanks for any help.
You could adjust using the EnvStats package as follows:
library(EnvStats)
# Data
Mydata =
c(6,3,3,3,5,5,4,3,5,5,4,3,4,4,6,5,5,4,5,2,6,4,6,5,3,3,8,3,4,4,6,6,6,6,6,5,6,6,5,5)
# Generalized Extreme Value (EnvStats)
egevd(Mydata, method = "pwme")# (Method: probability-weighted moments)
Results of Distribution Parameter Estimation
--------------------------------------------
Assumed Distribution: Generalized Extreme Value
Estimated Parameter(s): location = 4.268896
scale = 1.314489
shape = 0.353434
Estimation Method: Unbiased pwme
Data: Mydata
Sample Size: 40

Forecasting Holt-Winters with and without trend -> Strange Results

I am currently encountering a problem with the HoltWinters method implemented in the forecast package. If I use the method for some of my time series I have a larger error (SSE) when I use the Holt Winters with trend than without trend (Single Exponential Smoothing).
As I understand, Holt Winters with trend should at least be as good as Holt Winters without trend.
Can anybody explain this? I added an example below.
Best wishes,
Chris
library(forecast)
x = c(50,50,70,50,90,70,90,80,70,40,60,20,60,60,40,40,40,50,50,30,60,40,40,40,50,10,20,60,70, 60,60,80,70,80,90,80,70,30,30,80, 100,80,80,20,40,30,40,50,60,30,80, 100)
mSES = HoltWinters(x, alpha = TRUE, beta = FALSE, gamma = FALSE)
mHW = HoltWinters(x, alpha = TRUE, beta = TRUE, gamma = FALSE)
mSES$SSE
mHW$SSE
Don't set alpha or beta to TRUE. If beta is set to FALSE then it will do exponential smoothing but otherwise the input is taken as the specific parameter value. So TRUE gets coerced to be 1. So instead of letting the function choose the parameter that minimizes the SSE you are explicitly setting them to be 1.

Fixing random slopes and random intercepts values in lme4

I'm currently trying to calculate the effect size (Cohen's f^2) of a given effect but need to run a null and partial model with the random effects pre-specified in order to do so (according to Selya et al., 2012 for dealing with continuous predictors). Selya et al. outline the code necessary to do so in SAS, but I'm trying to figure out how one would do it in R.
When I went to use the last piece of code from a prior similar question, I kept getting the error that the thetas do not match ("3!=4"). I think the issue arises because my original model has a cross-level interaction term while the prior post has only random intercepts. I'm not just trying to hold constant the random effects on the intercept but also the random effects of a slope. How can I amend the code to make this run? I called the getME function with "theta" and saw indeed that my mod3b model has 4 theta values listed, so I imagine I will need to add another parameter term to the code. I just cannot figure out how to get the variance of the fourth theta term to show in my original output. I appreciate any and all help!
Here's my original cross-classified model with random slopes & random intercepts:
mod3b <- lmer(FitBelong~Condition*Gender +
(1+Condition|ResponseID) + (1|Stimuli),
data=LFS1Ensemble, REML=TRUE)
summary(mod3b)
I've adapted code from the answer on the link to be as follows:
#Effect size of interaction#
buildMM <- function(theta) {
dd <- as.function(mod3b)
ff <- dd(theta)
opt <- list(par=c(0,0,0),fval=ff,conv=0)
mm <- mkMerMod(environment(dd), opt, lmod$reTrms, fr = lmod$fr,
mc = quote(hacked_lmer()))
return(mm)
}
objfun <- function(x,target=c(3.92244,0.08805,0.09683)) {
mm <- buildMM(sqrt(x))
return(sum((unlist(VarCorr(mm))-target)^2))
}
s0 <- c(3.92244,0.08805,0.09683)/sigma(mod3b)^2
opt <- optim(fn=objfun,par=s0)
mm_final <- buildMM(sqrt(opt$par))
summary(mm_final)
The error it throws is: "Error: theta size mismatch"
When I do the traceback it gives me:
6 stop(structure(list(message = "theta size mismatch", call = NULL,
cppstack = NULL), .Names = c("message", "call", "cppstack"
), class = c("std::invalid_argument", "C++Error", "error", "condition"
)))
5 dd(theta)
4 buildMM(sqrt(x))
3 fn(par, ...)
2 (function (par)
fn(par, ...))(c(2.1571475240413, 0.0484231344499436, 0.0532516991344468
))
1 optim(fn = objfun, par = s0)
Any and all help much much appreciated!

Resources