2

I am trying to extract the value 10.02798 (Mode 1 Forecasts)from the following results but all variations of p$ that I tried didn't work. I am using the predict function from the arfima package in R.

>library(arfima)
>set.seed(82365)
>sim <- arfima.sim(1000, model = list(dfrac = 0.4, theta=0.9, dint = 1))
>fit <- arfima(sim, order = c(0, 1, 1))
>p<- predict(fit, n.ahead = 5)
> p
$`Mode 1`
$`Mode 1`$`Forecasts and SDs`
                   1        2        3        4        5
Forecasts   10.02798 10.05937 10.08246 10.10474 10.12719
Exact SD     1.03035  1.14641  1.22365  1.28863  1.34813
Limiting SD  1.03028  1.14627  1.22343  1.28834  1.34774
Fred
  • 223
  • 3
  • 14
  • 1
    Looks like you should be able to do `p$\`Mode 1\`$\`Forecasts and SDs\`[1, ]`. – Alex A. May 06 '15 at 18:38
  • 2
    Also, please consider adding a [minimum reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). While what you have is helpful, it would be more helpful if you could provide some sample data and the model definition passed to `predict`. – Alex A. May 06 '15 at 18:42
  • @AlexA. Thanks for the suggestion. I edited the question and it can be replicated now but your suggestion still gives NULL – Fred May 06 '15 at 18:55
  • It's because the first item in `p` doesn't actually have a name. (You can check this with `names(p)`.) That's why the `$` syntax is failing. I posted an answer that should get you what you need though. – Alex A. May 06 '15 at 19:12
  • @AlexA.Thank you. I changed the question title according to the answer. – Fred May 06 '15 at 19:15
  • 1
    Glad I could help. I added a line to the top of my answer that directly addresses the title (but I've kept all other content intact). – Alex A. May 06 '15 at 19:23

1 Answers1

1

When list items are not named, you can refer to them by their index in the list using double bracket syntax. For example, the first item has index 1, so you would extract this element like list[[1]].


In your case, the object p is a list, and its first item is an unnamed list which contains the table as shown in your question. You can extract the forecasts like so:

p[[1]]$Forecast

The first element of this vector is 10.02798, which is what you're after. So you can do

p[[1]]$Forecast[1]

The first two list items in p are not named for whatever reason, thus the double bracket syntax is required to get the first item in p. But the list items within that item are named, so you can use the $ syntax for those.

If you want to remain consistent with your extraction syntax, you can instead do

p[[1]][["Forecast"]][1]

Or even

p[[1]][[1]][1]

I think p[[1]]$Forecast[1] is just fine though.


In general, to view the structure of an object, you can use the str() function. In this case:

str(p)
#List of 9
# $         :List of 9
#  ..$ Forecast : num [1:5] 10 10.1 10.1 10.1 10.1
#  ..$ exactVar : num [1:5] 1.06 1.31 1.5 1.66 1.82
#  ..$ exactSD  : num [1:5] 1.03 1.15 1.22 1.29 1.35
#  ..$ uppernp  : num [1:5] 12 12.2 12.4 12.7 12.9
#  ..$ lowernp  : num [1:5] 7.59 7.33 7 7.03 6.73
#  ..$ meanvalnp: num [1:5] 9.86 9.83 9.88 9.82 9.83
#  ..$ limitVar : num [1:5] 1.06 1.31 1.5 1.66 1.82
#  ..$ limitSD  : num [1:5] 1.03 1.15 1.22 1.29 1.35
#  ..$ sigma2   : num 1.06
# $         :List of 9
#  ..$ Forecast : num [1:5] 10 10.1 10.1 10.1 10.1
#  ..$ exactVar : num [1:5] 1.07 1.43 1.7 1.93 2.14
#  ..$ exactSD  : num [1:5] 1.04 1.2 1.3 1.39 1.46
#  ..$ uppernp  : num [1:5] 12 14.2 15.7 17.7 20.2
#  ..$ lowernp  : num [1:5] 5.96 4.02 1.89 -0.29 -2.69
#  ..$ meanvalnp: num [1:5] 9.03 9.04 9.01 8.91 8.97
#  ..$ limitVar : num [1:5] 1.07 1.43 1.7 1.93 2.14
#  ..$ limitSD  : num [1:5] 1.04 1.2 1.3 1.39 1.46
#  ..$ sigma2   : num 1.07
# $ z       : Time-Series [1:1000] from 1 to 1000: -0.829 0.1149 1.261 -0.0427 0.1901 ...
# $ seed    : logi NA
# $ limiting: logi TRUE
# $ bootpred: logi TRUE
# $ B       : num 1000
# $ predint : num 0.95
# $ name    : chr "fit"
# - attr(*, "class")= chr "predarfima"

This shows that p is a list with 9 items, the first of which is itself a list with 9 items and doesn't have a name. This has an item called Forecast which is the vector you want.

Alex A.
  • 5,466
  • 4
  • 26
  • 56