helske / KFAS

KFAS: R Package for Exponential Family State Space Models
64 stars 17 forks source link

KFS function #67

Closed mariajesus1980 closed 3 years ago

mariajesus1980 commented 3 years ago

Hi,

I am estimating the following model:

bitcoin <- read.csv2("bitcoin.csv",header=T,dec=";") bitcoin$date <- as.Date(bitcoin$date, "%d-%m-%Y") bitcoin <- as_tbl_time(bitcoin, index = date) bitcoin <- as_period(bitcoin, '1 month')

y <- as.numeric(bitcoin$close) x <- as.numeric(bitcoin$volume)

model <- SSModel(log(y) ~ SSMtrend(1, Q = list(1)) + SSMseasonal(period = 12, sea.type = "trigonometric") + log(x) , data = bitcoin, H = 1)

model_fit <- fitSSM(model,inits = -15, method = "BFGS")$model

out<- KFS(model_fit$model, smoothing = c("state", "mean")) out ts.plot(out$model$y, fitted(out), lty = 1:2, col = 1:2, main = "Observations and smoothed signal with and without seasonal component") lines(signal(out, states = c("regression", "trend"))$signal, col = 4, lty = 1) legend("bottomleft", col = c(1, 2, 4), lty = c(1, 2, 1), legend = c("Observations", "Smoothed signal", "Smoothed level"))

However, I am getting this error:

out<- KFS(model_fit$model, smoothing = c("state", "mean")) Error in is.SSModel(model, na.check = TRUE, return.logical = FALSE) : Object is not of class 'SSModel'

So I checked:

class(model) [1] "SSModel" class(model_fit) [1] "SSModel" out<- KFS(model_fit$model, smoothing = c("state", "mean")) Error in is.SSModel(model, na.check = TRUE, return.logical = FALSE) : Object is not of class 'SSModel' is.SSModel(model_fit) [1] TRUE is.SSModel(model) [1] TRUE

How can I fix the code to run all again ? Can you help here please?

many thanks in advanced,

all the best, Maria Jesus

helske commented 3 years ago

You are checking that model_fit is valid SSModel object, but then passing model_fit$model to KFS, which is likely NULL.

mariajesus1980 commented 3 years ago

Hi, sorry but I didnt understand your response. I checked again:

model <- SSModel(log(y) ~ SSMtrend(1, Q = list(1)) +

  • SSMseasonal(period = 12, sea.type = "trigonometric") +
  • log(x) , data = bitcoin, H = 1)

class(model) [1] "SSModel" is.SSModel(model) [1] TRUE

model_fit <- fitSSM(model,inits = -15, method = "BFGS")$model out<- KFS(model_fit$model, smoothing = c("state", "mean")) Error in is.SSModel(model, na.check = TRUE, return.logical = FALSE) : Object is not of class 'SSModel'

can you help to fix this error, please ?

thanks

helske commented 3 years ago

The output of fitSSM is a list containing component model which is the estimated model (object of class SSModel), and which you extract and assign to a new object model_fit. But then when you are calling KFS you are bit using this as an input but instead try again to extract a component named model from model_fit which does not exist.

mariajesus1980 commented 3 years ago

Thanks, I changed the name, keeping the same code for the rest and was working.