[R] Help w/an old R program I've rediscovered and want to make work

pce369 pce369 at hotmail.com
Sat Oct 22 21:41:40 CEST 2011


I found an old R program I wrote som eyears back and I'd like to make work in
2.11.1 (or a more recent version), but am having two problems: 

1. I can't seem to access the datafile it requires. I'm not sure where the
default place that R looks to for files references within it is, or what
exactly the format is for pulling in the data, by my own #comments from a
few years ago aren't helpful.

2. Whether or not it's related to the problem locating the data file, the
error message I get is:

"object of type 'closure' is not subsettable"

In short, the goal of the program is to take a list of number from a file,
shuffle it in a bootstrap manner a number of times ("Loops Number"), divide
the resulting (long) list of numbers into a number of discrete periods
("WindowPeriod"), and then from that generate means, standard deviations,
quantiles, etc.

The code is below. Any help - I must have written this, and with help, years
ago, as my comments aren't terribly helpful - would be deeply appreciated.
Also, any more efficient way of writing what is probably cumbersome and
spagetti-ish would be helpful.

pce369

## code begins below ## 

# simulation parameters

LoopsNumber <- 10000          # number of iterations
WindowPeriod <- 20            # 20 unit returns

# data input area

data <- read.table("gspc.txt",header=T)  #gspc.txt is a column of returns
under gspc header

# main

PeriodsNumber <- trunc(length(data$gspc)/WindowPeriod) # above will lead to
losing last few elements, but makes life easier

for (i in 1:LoopsNumber) 
{
MCP <- sample(data$gspc, replace = FALSE)   # resampling here without
replacement = permutations, not bootstrap

# computing period returns

MCP <- MCP[1:(WindowPeriod*PeriodsNumber)]   #loses last few elements
MCPsplitlist <- 1:PeriodsNumber
MCPsplitlist <- rep(MCPsplitlist, each=WindowPeriod)
MCPfunction <- function(y) {cumprod(1+y)* c(rep(0,WindowPeriod-1),1)}
MCPcumprod <- tapply(MCP, MCPsplitlist, MCPfunction)
MCPcumprod <- unlist(MCPcumprod, use.names=FALSE)
MCPcumprod <- MCPcumprod[MCPcumprod != 0]    # makes life easier - It
deletes the very rare 0 x-day returns

if (i==1) result <- MCPcumprod
else result <- c(result, MCPcumprod)

}
result <- 100*(result-1)

# output area, displaying a few useful statistics and graphs

mean(result)
sd(result)
quantile(result, seq(0,0.05,0.001))
par(mfcol=c(1, 2))
plot(ecdf(result), do.points = FALSE, col.hor = 'red')
qqnorm(result)
qqline(result, col = 'green', lwd = 3)

### code ends here ###

--
View this message in context: http://r.789695.n4.nabble.com/Help-w-an-old-R-program-I-ve-rediscovered-and-want-to-make-work-tp3928890p3928890.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list