R: Forecasting Future Values of Time Series - r

I am working with the R programming language. In particular, I am using "Markov Switching Models" for the purpose modelling more complicated dataset with varying degrees of volatility. For this problem, I am using the "MSwM" package in R:
https://cran.r-project.org/web/packages/MSwM/MSwM.pdf
https://cran.r-project.org/web/packages/MSwM/vignettes/examples.pdf
I am following the first example from the "vignettes":
#load library
library(MSwM)
#load data
data(example)
#plot data
plot(ts(example))
#fit basic model
mod=lm(y~x,example)
#fit markov switching model:
mod.mswm=msmFit(mod,k=2,p=1,sw=c(TRUE,TRUE,TRUE,TRUE),control=list(parallel=FALSE))
The above code successfully creates the model - 2 "Regimes" have been identified:
Question: I checked the documentation of this package, and there does not seem to be any functions that allow you to predict future values of this time series. I did some research and it seems like the "Markov Switching Model" should allow you to predict future values of a time series, e.g. : https://stats.stackexchange.com/questions/90448/how-to-forecast-a-markov-switching-model
OR:
However, there does not seem to be a straightforward way to do this in R. Can someone please suggest how to resolve this problem?
Thanks

Related

Call R script from Anylogic and return recults

I need to call a forecast model from R within Anlylogic, and return the resulting outputs in R. It is a specific timeseries that I have built in R, and just copying the coefficients to Anylogic is not efficient. I have seen a couple of older posts on similar questions, but I am not sure I can follow. Any advice would be very appreciated.
I have a regression forecast model that uses predictors to provide a forecast along with Prediction Intervals. I need these outputs to be updated by the different values of the predictors and then used in Anylogic.

When predicting using R ARIMA object, how to declare the time series' history?

Suppose I fit AR(p) model using R arima function from stats package. I fit it using a sample x_1,...,x_n. In theory, when predicting x_{n+1} using this model, it needs an access x_n,...x_{n-p}.
How does the model know which observation I want to predict? What if I wanted to actually predict x_n based on x_{n-1},...,x_{n-p-1} and how my code would differ in this case? Can I make in-sample forecasts, similar to Python's functionality?
If my questions imply that I think about forecasting in a wrong way, please kindly correct my understanding of the subject.

How to run a dynamic linear regression in R?

I am new to using R as I usually use Stata. I want to estimate a state space model on some time series data with time varying coefficients. From what I have gathered this is not possible to do in Stata.
I have downloaded the dlm package in R and I am trying to run the dlmModReg command to regress my dependent variable on a single explanatory variable. I would like to allow the intercept and beta coefficient to vary over time.
If anyone could show me an example of the code I want to run I think that would be enough for me to work out how to do this. The examples I have found online are vague or use terminology that I am not familiar with as a new R user. Any help or comments are greatly appreciated.

Can MXNET fit a regression LSTM model in R?

I would like to fit an LSTM model using MXNET in R for the purpose of predicting a continuous response (i.e., regression) given several continuous predictors. However, the mx.lstm() function seems to be geared toward NLP as it requires arguments which don't seem applicable to a regression problem (such as those related to embedding).
Is MXNET capable of this sort of modeling and, if not, what is an example of an appropriate tool (preferably in R)? Are there any tutorials relevant to the problem I've described?
LSTM is used for working with temporal data: text, speech, time series. If you want to predict a continuous response, then I assume you want to do something similar to time series analysis.
If my assumption is correct, then, please, take a look here. It gives quite a good example on how to use MxNet with R for time series on CPU. The GPU version is also available here.

R Forecast package for anomaly detection

I'm trying to detect anomalies from training data.
First, I train a model according to a given time series using the forecast package:
train <- forecast(ts(sin((2*pi)*seq(from=0,to=10,by=0.01)),frequency=100))
Then, once I get new time series i try seeing how well they fit the trained data, and by that finding anomalies.
Currently I'm using the accuracy function which doesn't seem to be the right tool for the job:
test <- ts(sin((2*pi)*seq(from=0,to=20,by=0.01))+sin((3*pi)*seq(from=0,to=20,by=0.01)),frequency=100)accuracy(train,test)
accuracy(train,test)
I also thought of somehow analyzing the residuals of the new dataset according to the trained model.
Does anyone have any good ideas as to how to optimize this task?

Resources