Hammered
Hammered

Reputation: 1

Bootstrapping in R Not Displaying Relevant Statistic

I have bootstrapped a project for a class I'm in and when attempting to display the specific statistic I want, I am met with errors. I am coding this with R programming language for statistics in R studio. Here is my code:

  boot = do(20)* 
  {
  horizon = 20
  totalwealth = 10000
  pweights = c(0.5, 0, 0, 0, 0.5)
  holdings = pweights * totalwealth
  wealthtracker = rep(0, horizon) # Set up a placeholder to track total wealth
  for(today in 1:horizon) {
    return.today = resample(myreturns, 1, orig.ids=FALSE)
    holdings = holdings*(1+return.today)
    totalwealth = sum(holdings)
    wealthtracker[today] = totalwealth
  }
  VaR = unname(quantile(wealthtracker, p=0.05)) 
  VaR
  }

  hist(boot$VaR)

The error I am encountering occurs when I try and take data or build a histogram from my sample statistic, in this case, VaR.

Whenever I type in 'boot$' the next thing to appear should be 'VaR' and the other statistics in my bootstrapped model, but the only thing that shows up is 'result'. When I enter commands to return anything with VaR in the context of the bootstrap (bootstrap$VaR) I return this error - 'x' must be numeric. The problem here is that I've already made sure 'x' is numeric by applying the unname function. Any help here is greatly appreciated.

Thanks.

Upvotes: 0

Views: 203

Answers (1)

David Robinson
David Robinson

Reputation: 78610

When you use the mosaic package's do() function, the results are always in a column called result. It doesn't matter what you called the variable within those brackets.

Instead of trying to access boot$VaR, just work with boot$result, such as

hist(boot$result)

It contains the data you want.

Upvotes: 1

Related Questions