How do I change the underlying distribution in garch.sim? - r

I want to change the underlying distribution for my garch.sim function, but I get the following error : could not find function "rnd"
I would like to change it to either t or chi-squared distribution, as the default one is normal from a normal distribution. The below code is trying to mimic the normal distribution with sd = 4, but it gives the same error. The source code does not give additional hint in my opinion to solve the issue.
The code itself:
u_garch <-garch.sim(alpha = c(0.01,0.3), beta = 0.69,n = 251,rnd = rnorm(351,sd = 4),ntrans = 100)

Related

nlminb problem, convergence error code = 1 message = iteration limit reached without convergence (10)

I am trying to find a best model fitting on my data using library(nlme) and lme function in R. Here is my model when the slope is fixed:
FixedRopeLength <- lme(EnergyCost~ RopeLength,
data = data,
random=~1|Subject, method = "ML")
summary(FixedRopeLength)
To see whether a random slope provides a better model than a fixed slope, I let the slope to vary across Subject as follows:
RandomRopeLength <- lme(EnergyCost~RopeLength,
data = data,
random=~RopeLength|Subject, method = "ML")
summary(RandomRopeLength)
However, I got this error:
Error in lme.formula(EnergyCost ~ RopeLength, data = data, random =
~RopeLength | : nlminb problem, convergence error code = 1
message = iteration limit reached without convergence (10)
Any solution??
Thank you so much for your help. Your code worked. I only needed to justify your code based on lme function. Here is the code which can be used for aforementioned error:
RandomRopeLength<-lme(EnergyCost~RopeLength, data = data, random=~RopeLength|Subject, method = "ML", control =list(msMaxIter = 1000, msMaxEval = 1000))
summary(RandomRopeLength)
Thanks!
?lme shows that there is a control argument, which redirects you to ?lmerControl, which gives you
msMaxIter: maximum number of iterations for the optimization step
inside the ‘lme’ optimization. Default is ‘50’.
and
msMaxEval: maximum number of evaluations of the objective function
permitted for nlminb. Default is ‘200’.
These correspond to eval.max and iter.max from ?nlminb. Since I'm not sure which of these is the problem, I would re-run the model with
control = lmeControl(msMaxIter = 1000, msMaxEval = 1000)
However, I'll warn you that once you have a problem that experiences numerical problems with the default parameter settings, adjusting the parameter settings may just lead to other problems farther down the line ...

two functions from different packages not working together

So I am doing work on ruin probability. I am performing monte-carlo simulations through R. The package that works perfectly for this is 'ruin'. You simply specify inputs for different ruin models. I am trying to simulate a SparreAndersen model where there are two random variable generators from different distributions. Typical distributions such as exponential or gamma work fine, using rgamma or rexp.
The problem that I am having is that I require a mixed exponential function as one of the r.v. generators. I found a great package that achieves this, called 'gendist' where I use the rmixt function to generate r.v.s from a mixed exponential function.
Running rmixt with all parameters works fine, giving a correct and accurate numerical output.
Running rexp with all parameters works fine, giving a correct and accurate numerical output.
Running a SparreAndersen() function works fine when using any function that is NOT the rmixt function.
The problem: SparreAndersen does not like to use rmixt from 'gendist' package as a r.v. generator
When I try to run this function, I get this as an error
I have no idea why this isn't working???
See my entire code below:
library(ruin)
library(gendist)
rmixt(1, phi=1, spec1="exp", arg1=list(rate=3), spec2="exp",
arg2=list(rate=7) )
rexp(1, rate=1)
modelSA <- SparreAndersen(initial_capital = 1,
premium_rate = 5,
claim_interarrival_generator = rmixt,
claim_interarrival_parameters = list(1, phi=1, spec1="exp", arg1=list(rate=3), spec2="exp", arg2=list(rate=7)),
claim_size_generator = rexp,
claim_size_parameters = list(rate=1))
edit: I have found a working solution, not involving the 'gendist' package. Let me know if this is the best way to go about doing this.
My new working code:
# Function which generates multiple exponential
rmixedexp <- function(...) {
choice <- c(...)
return(rexp(n=1, sample(choice, 1, replace=TRUE)))
}
modelSA <- SparreAndersen(initial_capital = 5,
premium_rate = 5,
claim_interarrival_generator = rmixedexp,
claim_interarrival_parameters = list(1, 3, 7),
claim_size_generator = rexp,
claim_size_parameters = list(rate=1))
I had to make my own function which takes any number of parameters, for example, 2. Each parameter is the rate of the exponential, chosen randomly (probability half).

Controlling Step Size in 'optim' R function

I am using R to optimize a function using the 'optim' function. However, the true values of the variables I am optimizing over are spaced apart at least 10^-5 or so. But, as I understand it, the default step size (ie how much optim adds to each control variable to see how that changes the objective function) is of the order of 10^-8.
Is there any easy way to tell the 'optim' function to increase the step size to 10^5 or perhaps higher?
For reference my code is here:
Optimal <- optim(par = starting, fn =expectedSeats,
propensities = propsShocked, n = NumberofDistricts,
shockType = "normal", shockSD = 0.1,
method = "L-BFGS-B",
lower = rep(0,NumberofDistricts), upper = rep(1,NumberofDistricts),
control=list(factr = 1e12)
)
I have looked around and can't seem to figure this out. Thanks!
As I understand your question, I believe you can specify the step value within the ndeps argument of the control= option. According to the documentation, the default is 1e-3 (not 1e-8).

How to generate the actual results of an IRF() function within the vars package?

Somehow, I am unable to generate the actual underlying values of the IRFs. See code of a simple VAR model.
irf5<-irf(var2, impulse = "libor", response = "y", n.ahead = 10, ortho = TRUE, boot = TRUE, CI = 0.95, runs = 100)
I can generate the resulting IRF plots just fine with this code:
plot(irf5)
But, I can't generate the underlying values. I'd like to do so to have precise figures. Visually interpreting IRFs is not that accurate. Using the summary() did not provide me this information.
I think you need just to write irf5 in command line and push ctrl+enter. If you can plot irf, so there are no errors, you can easily get IRF's values. In other words, inirf5<-irf(var2,....) you just generate and save new variable, but not call to it
You should be able to get the values by using irf5$irf

Estimate parameters of Frechet distribution using mmedist or fitdist(with mme) error

I'm relatively new in R and I would appreciated if you could take a look at the following code. I'm trying to estimate the shape parameter of the Frechet distribution (or inverse weibull) using mmedist (I tried also the fitdist that calls for mmedist) but it seems that I get the following error :
Error in mmedist(data, distname, start = start, fix.arg = fix.arg, ...) :
the empirical moment function must be defined.
The code that I use is the below:
require(actuar)
library(fitdistrplus)
library(MASS)
#values
n=100
scale = 1
shape=3
# simulate a sample
data_fre = rinvweibull(n, shape, scale)
memp=minvweibull(c(1,2), shape=3, rate=1, scale=1)
# estimating the parameters
para_lm = mmedist(data_fre,"invweibull",start=c(shape=3,scale=1),order=c(1,2),memp = "memp")
Please note that I tried many times en-changing the code in order to see if my mistake was in syntax but I always get the same error.
I'm aware of the paradigm in the documentation. I've tried that as well but with no luck. Please note that in order for the method to work the order of the moment must be smaller than the shape parameter (i.e. shape).
The example is the following:
require(actuar)
#simulate a sample
x4 <- rpareto(1000, 6, 2)
#empirical raw moment
memp <- function(x, order)
ifelse(order == 1, mean(x), sum(x^order)/length(x))
#fit
mmedist(x4, "pareto", order=c(1, 2), memp="memp",
start=c(shape=10, scale=10), lower=1, upper=Inf)
Thank you in advance for any help.
You will need to make non-trivial changes to the source of mmedist -- I recommend that you copy out the code, and make your own function foo_mmedist.
The first change you need to make is on line 94 of mmedist:
if (!exists("memp", mode = "function"))
That line checks whether "memp" is a function that exists, as opposed to whether the argument that you have actually passed exists as a function.
if (!exists(as.character(expression(memp)), mode = "function"))
The second, as I have already noted, relates to the fact that the optim routine actually calls funobj which calls DIFF2, which calls (see line 112) the user-supplied memp function, minvweibull in your case with two arguments -- obs, which resolves to data and order, but since minvweibull does not take data as the first argument, this fails.
This is expected, as the help page tells you:
memp A function implementing empirical moments, raw or centered but
has to be consistent with distr argument. This function must have
two arguments : as a first one the numeric vector of the data and as a
second the order of the moment returned by the function.
How can you fix this? Pass the function moment from the moments package. Here is complete code (assuming that you have made the change above, and created a new function called foo_mmedist):
# values
n = 100
scale = 1
shape = 3
# simulate a sample
data_fre = rinvweibull(n, shape, scale)
# estimating the parameters
para_lm = foo_mmedist(data_fre, "invweibull",
start= c(shape=5,scale=2), order=c(1, 2), memp = moment)
You can check that optimization has occurred as expected:
> para_lm$estimate
shape scale
2.490816 1.004128
Note however, that this actually reduces to a crude way of doing overdetermined method of moments, and am not sure that this is theoretically appropriate.

Resources