I'm using tsDyn package to predict time series data in R. there is a function in this package called nnetTs. However when I try to predict, it just gives me 1 output and does not provide x steps ahead forecast. See eblow for the code:
library("tsDyn")
set.seed(1234)
mod.nnet <- nnetTs(log(lynx), m=2, size=3,steps=12)
mod.nnet
predict(mod.nnet,steps=12)
here is the output (as noted above, I'm just getting 1 single output and not 12 steps ahead prediction). I'm not sure what the issue is, I read the documentation, I'm stuck.
Time Series:
Start = 1935
End = 1935
Frequency = 1
[1] 7.80263
Any help would be greatly appreicated
You should run
predict(mod.nnet,n.ahead=12)
at the last line. The argument for selecting forecast horizon is n.ahead not steps.
Related
I am using R and I have weekly data (all in all 660 obeservations) and I want to use X-13 Arima-Seats from the seasonal package to seasonally adjust my data. I store my data in a ts object:
library(lubridate)
x <- ts(data, freq=365.25/7, start=decimal_date(ymd("2004-02-01")))
library(seasonal)
x_sa <- seas(x)
However, I get the error:
Error: X-13 run failed
Errors:
- Seasonal period too large. See Section 2.7 of the Reference Manual on program limits
- Expected argument name or "}" but found ".1785714285714"
- Time series could not be read due to previously found errors
- Expected specification name but found "}"
- Specify series before user-defined adjustments
- Need to specify a series to identify outliers
I also tried a shorter period of time, but the error is still the same.
I would average your weekly data by month and run the following ts object:
ts(data, freq=12, start=c(2004,2))
You'll lose some data granularity converting to months instead of weeks, but then the seasonal package will at least be able to process your data.
Try STL (Seasonal and Trend decomposition using Loess). You can use it with any type of seasonality, not only monthly and quarterly.
It has automatic decomposition mstl(). So for your data the formula is:
x_sa <- mstl(x)
There are tuning parameters for the function t.window and s.window with help of with you are able to control how rapidly the trend-cycle and seasonal components can change.
More details you can get from book of Rob J Hyndman and George Athanasopoulos "Forecasting: Principles and Practice". In section "Time series decomposition".
I have a problem with parameter estimation and forecast for a GARCH model.
I have a time series of volatilities, starting in 1996 and ending in 2009.
I tried to estimate the parameters with the ugarchspec and ugarchfit function:
garch1.1 <- ugarchspec(variance.model=list(model="sGARCH", garchOrder=c(1,1)),mean.model=list(armaOrder=c(0,0)),distribution="std")
garch1.1fit <- ugarchfit(spec=garch1.1,data=RV)
The results seemed to be okay, so I went on with the forecast.
I wanted to use the ugarchforecast or ugarchroll function. But when I tried to do it, I recognized that they work with the wrong date. For example, If I try to do a simple forecast like
forecast <- ugarchforecast(garch1.1fit,n.ahead=2)
I get the following results:
0-roll forecast [T0=1979-04-05 01:00:00]:
Series Sigma
T+1 5.373e-05 3.733e-05
T+2 5.373e-05 3.762e-05
So my problem is: why does R say that T0=1979? This cant be correct as my data starts in 1996 and ends in 2009.
When I had a look at the residuals from garch1.1fit, the date is also wrong.
What's the problem here?
I am not sure what object do you use as RV, but I assume it is a numeric vector. Package rugarch works better with xts objects supported by xts package.
Following code should do the job:
require(xts)
time <- #put here time vector from your data
RV.xts <- na.omit(xts(x = RV, order.by = time))
and then your code with changed object RV for new one RV.xts:
garch1.1 <- ugarchspec(variance.model=list(model="sGARCH", garchOrder=c(1,1)),
mean.model=list(armaOrder=c(0,0)),
distribution="std")
garch1.1fit <- ugarchfit(spec=garch1.1,data=RV.xts)
forecast <- ugarchforecast(garch1.1fit,n.ahead=2)
The code i provided does two things: first it makes an xts object using time. This object will tell your ugarchfit() function what is the time of this model. Second, it omits possible NA data, which function ugarchfit() do not handle.
Make sure if object xts connected dates correctly by checking:
head(RV.xts)
tail(RV.xts)
I think you did not specify date for your ugarch model. Note that R "Date" class is coded in the number of days from the day 1970-01-01.
Following code may help to understand the concept:
as.Date("1970-01-01")
as.numeric(as.Date("1970-01-01"))
as.Date("1970-01-10")
as.numeric(as.Date("1970-01-10"))
As the date is not specified for ugarch model, your data seems to have the number of observations to fill the 1970-1979 years (probably weekends are excluded), and the prediction starts after that period.
I am having trouble relating how forecasts are calculated in the R packages forecast::croston and tsintermittent::crost. I understand the concept of croston, such as in the example posted here (www.robjhyndman.com/papers/MASE.xls), but the output from the R packages produces very different results.
I used the values from the Excel example (by R. Hyndman) in the following code:
library (tsintermittent)
library (forecast)
x=c(0,1,0,11,0,0,0,0,2,0,6,3,0,0,0,0,0,7,0,0,0,0) # from Hyndman Excel example
x_crost = crost(x,h=5, w=0.1, init = c(1,1) ) # from the tsintermittent package
x_croston=croston(x,h=5, alpha = 0.1) # from the forecast package
x_croston$fitted
y=data.frame(x,x_crost$frc.in,x_croston$fitted)
y
plot(x_croston)
lines(x_croston$fitted, col="blue")
lines(x_crost$frc.in,col="red")
x_crost$initial
x_crost$frc.out # forecast
x_croston$mean # forecast
The forecast from the Excel example is 1.36, crost gives 1.58 and croston gives 1.15. Why are they not the same? Also note that the in-sample (fitted) values are very different.
For crost in the tsintermittent package you need a second flag to not optimise the initial values: init.opt=FALSE, so the command should be:
crost(x,w=0.1,init=c(2,2),init.opt=FALSE)
Setting only init=c(2,2) will only set the initial values for the optimiser to work from.
Also note that the time series that Rob Hyndman has in his example has two additional values in the beggining (see column B), so x should be:
x=c(0,2,0,1,0,11,0,0,0,0,2,0,6,3,0,0,0,0,0,7,0,0,0,0)
Running these two commands produces the same values as in the excel example.
I am using R for sometime now and some days ago I found a very interesting function which made a prediction on a given time series. It just took the data from the known time series and applied it on a given period, but it kept the pattern. The problem is that I lost it. I am sure it was a sort of HoltWinters. I am trying two days to find something, but till now without success. Could someone please give me a hand on this!
Just use predict:
# Assuming you have some timeseries data in myts
hw <- HoltWinters(myts)
predict(hw, 10) # predict 10 periods ahead
You can use forecast.HoltWinters
#Model creation
fit <- HoltWinters(ts.data,gamma=FALSE)
#Load forecast package
require(forecast)
#Apply model into forecast
forecast(fit)
I'm doing some survival analysis in R, and looking to tidy up/simplify my code.
At the moment I'm doing several steps in my data analysis:
make a Surv object (time variable with indication as to whether each observation was censored);
fit this Surv object according to a categorical predictor, for plotting/estimation of median survival time processes; and
calculate a log-rank test to ask whether there is evidence of "significant" differences in survival between the groups.
As an example, here is a mock-up using the lung dataset in the survival package from R. So the following code is similar enough to what I want to do, but much simplified in terms of the predictor set (which is why I want to simplify the code, so I don't make inconsistent calls across models).
library(survival)
# Step 1: Make a survival object with time-to-event and censoring indicator.
# Following works with defaults as status = 2 = dead in this dataset.
# Create survival object
lung.Surv <- with(lung, Surv(time=time, event=status))
# Step 2: Fit survival curves to object based on patient sex, plot this.
lung.survfit <- survfit(lung.Surv ~ lung$sex)
print(lung.survfit)
plot(lung.survfit)
# Step 3: Calculate log-rank test for difference in survival objects
lung.survdiff <- survdiff(lung.Surv ~ lung$sex)
print(lung.survdiff)
Now this is all fine and dandy, and I can live with this but would like to do better.
So my question is around step 3. What I would like to do here is to be able to use information in the formula from the lung.survfit object to feed into the calculation of the differences in survival curves: i.e. in the call to survdiff. And this is where my domitable [sic] programming skills hit a wall. Below is my current attempt to do this: I'd appreciate any help that you can give! Once I can get this sorted out I should be able to wrap a solution up in a function.
lung.survdiff <- survdiff(parse(text=(lung.survfit$call$formula)))
## Which returns following:
# Error in survdiff(parse(text = (lung.survfit$call$formula))) :
# The 'formula' argument is not a formula
As I commented above, I actually sorted out the answer to this shortly after having written this question.
So step 3 above could be replaced by:
lung.survdiff <- survdiff(formula(lung.survfit$call$formula))
But as Ben Barnes points out in the comment to the question, the formula from the survfit object can be more directly extracted with
lung.survdiff <- survdiff(formula(lung.survfit))
Which is exactly what I wanted and hoped would be available -- thanks Ben!