How to save estimated parameters from nigfit() in a variable
-
28-05-2021 - |
题
I want to automatically fit time series returns into a NIG distribution. With nigfit() from the package fBasics I estimate the mu, alpha, beta and delta of the distribution.
> nigFit(histDailyReturns,doplot=FALSE,trace=FALSE)
Title:
Normal Inverse Gaussian Parameter Estimation
Call:
.nigFit.mle(x = x, alpha = alpha, beta = beta, delta = delta,
mu = mu, scale = scale, doplot = doplot, span = span, trace = trace,
title = title, description = description)
Model:
Normal Inverse Gaussian Distribution
Estimated Parameter(s):
alpha beta delta mu
48.379735861 -1.648483055 0.012361539 0.001125734
This works fine, which means that nigfit plots my parameters. However I would like to use the estimated parameters and save them in variables. So I could use them later.
> variable = nigfit(histDailyReturns,doplot=FALSE,trace=FALSE)
This doesn't work out. 'variable' is an S4 object of class structure fDISTFIT. Calling the variable replots the output of nigfit above. I tried the following notations, to get just one parameter:
> variable$alpha
> variable.alpha
> variable[1]
I couldn't find an answer in the documentation of nigfit. Is it possible to save the estimated parameters in variables? How does it work?
解决方案
access the output compenents using @
. variable
has different slots
. Get their names using slotNames()
. Using the example from the documentation:
set.seed(1953)
s <- rnig(n = 1000, alpha = 1.5, beta = 0.3, delta = 0.5, mu = -1.0)
a <- nigFit(s, alpha = 1, beta = 0, delta = 1, mu = mean(s), doplot = TRUE)
slotNames(a)
[1] "call" "model" "data" "fit" "title"
[6] "description"
# `fit` is a list with all the goodies. You're looking for the vector, `estimate`:
a@fit$estimate
alpha beta delta mu
1.6959724 0.3597794 0.5601027 -1.0446402
其他提示
Examine the structure of the output object using str(variable)
:
> variable@fit$par[["alpha"]]
[1] 48.379735861
> variable@fit$par[["beta"]]
[1] -1.648483055
> variable@fit$par[["delta"]]
[1] 0.012361539
> variable@fit$par[["mu"]]
[1] 0.001125734