[R-sig-Finance] fSeries prob
Spencer Graves
spencer.graves at pdf.com
Sat Jun 24 06:01:48 CEST 2006
Dear Øyvind Foshaug and Diethelm Wuertz:
ØYVIND:
Thanks for reporting this. Could you please try the revised function
below? I changed three lines over the previous version
In case you are interested, I will outline here what I did to resolve
this issue. First, I looked on the 'armaFit' help page for an example.
Then I modified it in a way that successfully reproduced the error you
reported:
xmpSeries("\nStart: Simulate an ARMA(2, 1) process > ")
x = armaSim(model = list(ar = c(0.5, -0.5), ma = 0.1), n = 1000)
Continue = xmpSeries("Press any key > ")
# Estimate the parameters:
fit = armaFit(x ~ arma(2, 1))
predict(fit, doplot=FALSE)
Error in predict.fARMA(fit, doplot = FALSE) :
object "npred" not found
If you list 'predict', you will see it primarily consists of a call
to 'UseMethod'. The help page for 'UseMethod' says, "see also:
methods". When I tried 'methods("predict")', I got a character vector
of length 23, all beginning "predict.". This list included in
particular, "predict.fARMA". I listed that, made a local copy, found
where 'npred' is designed and used, and made two minor changes in
'predict.fARMA'. After sourcing that, I got a different error, and I
found an apparent patch for that.
Thanks again for reporting this problem.
DIETHELM:
Below please find a modification to your 'predict.fARMA' from fSeries
221.10065. I made three changes marked by comments. Please let me know
how you would like me to handle issues like this. Until I hear from you
I will send them to you like this -- unless I find a caret "^", which
might be eaten by certain email software.
You might consider adding a 'predict(fit, doplot=FALSE)' to the help
page example section.
Best Wishes,
Spencer Graves
##################################
sessionInfo()
Version 2.3.1 (2006-06-01)
i386-pc-mingw32
attached base packages:
[1] "methods" "stats" "graphics" "grDevices" "utils" "datasets"
[7] "base"
other attached packages:
fSeries fCalendar fBasics
"221.10065" "221.10065" "221.10065"
Best Wishes,
####################################
predict.fARMA <-
function (object, n.ahead = 10, n.back = 50, conf = c(80, 95),
doplot = TRUE, doprint = TRUE, ...)
{
object = object at fit
class(object) = object$class
TS = "stats"
if (object$tsmodel == "fracdiff") {
warning("Prediction for FRACDIFF not yet implemented")
return(NA)
}
options(warn = -1)
pred = .predictTS(object = object, n.ahead = n.ahead, se.fit = TRUE)
nint = length(conf)
upper = lower = matrix(NA, ncol = nint, nrow = length(pred$pred))
for (i in 1:nint) {
qq = qnorm(0.5 * (1 + conf[i]/100))
lower[, i] = pred$pred - qq * pred$se
upper[, i] = pred$pred + qq * pred$se
}
colnames(lower) = colnames(upper) = paste(conf, "%", sep = "")
shadecols = switch(1 + (length(conf) > 1), 7, length(conf):1)
shadepalette = heat.colors(length(conf))
col = 1
data = as.ts(object$x)
freq = frequency(data)
start = start(data)
n = length(data)
pred.mean = pred$pred
npred = length(pred.mean)
# The preceeding 2 lines (pred.mean and npred)
# were moved from after if(doplot) to before.
if (doplot) {
ylim = range(c(data[(n - n.back + 1):n], pred.mean),
na.rm = TRUE)
ylim = range(ylim, lower, upper, na.rm = TRUE)
ylab = paste("Series: ", object$series)
plot(ts(c(data[(n - n.back + 1):n], pred.mean[1], rep(NA,
npred - 1)), end = tsp(data)[2] + npred/freq, f = freq),
ylim = ylim, ylab = ylab)
title(main = paste(object$tstitle))
}
xx = tsp(data)[2] + (1:npred)/freq
idx = rev(order(conf))
if (nint > 1)
palette(shadepalette)
# "if(doplot)" was added before 'for(i in 1:nint)'
if(doplot)for (i in 1:nint) {
polygon(c(xx, rev(xx)), c(lower[, idx[i]], rev(upper[,
idx[i]])), col = shadecols[i], border = FALSE)
}
palette("default")
lines(ts(pred.mean, start = tsp(data)[2] + 1/freq, f = freq),
lty = 1, col = 4)
nconf = length(conf)
out = pred.mean
upper = as.matrix(upper)
lower = as.matrix(lower)
names = "Forecast"
for (i in nconf:1) {
out = cbind(out, lower[, i])
names = c(names, paste("Low", conf[i]))
}
out = cbind(out, pred.mean)
names = c(names, "Forecast")
for (i in 1:nconf) {
out = cbind(out, upper[, i])
names = c(names, paste("High", conf[i]))
}
out = round(out, digits = 4)[, 2:(2 * nconf + 2)]
colnames(out) = names[2:(2 * nconf + 2)]
if (doprint)
print(out)
options(warn = 0)
result = list(pred = pred$pred, se = pred$se)
invisible(result)
}
####################################
Øyvind Foshaug wrote:
> Hi,
> I seem to get an error when I apply predict with the
option doplot=FALSE in function armaFit. It says npred not found.
>
> Thanks,
> Oyvind Foshaug
>
>
> ---------------------------------
>
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
More information about the R-SIG-Finance
mailing list