pce369
2011-Oct-22 19:41 UTC
[R] Help w/an old R program I've rediscovered and want to make work
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.
Barry Rowlingson
2011-Oct-22 20:28 UTC
[R] Help w/an old R program I've rediscovered and want to make work
On Sat, Oct 22, 2011 at 8:41 PM, pce369 <pce369 at hotmail.com> wrote:> 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.R will only look in the current working directory if you don't specify a full path to read.table. That's the directory you start R from, or any directory set with setwd("some/path") in an R session. Suggest you get hunting for your data set...> 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"You get that if you mistakenly try subsetting a function. I suspect this is because you try to call your data 'data' and that gets confused with the data function because the read.table fails. If you start a fresh R session and do: data$something you'll get that message. R is smart enough though, that if you do create a data frame with the name 'data' it will go for that if you do data$something... Barry