[R] how to prevent duplications of data within a loop

Juliet Hannah juliet.hannah at gmail.com
Sat Jan 24 06:30:27 CET 2009


Hi All,

I had posted a question on a similar topic, but I think it was not
focused. I am posting a modification that I think better accomplishes
this.
I hope this is ok, and I apologize if it is not. :)

I am looping through variables and running several regressions. I have
reason to believe that the data is being duplicated because I have
been
monitoring the memory use on unix.

How can I avoid this? Here is an example of how I am going about this.
For lm, I also tried model=FALSE, but this did not seem to do the job.
Any ideas?
Thanks for your time.

Regards,

Juliet

# create data
set.seed(1)
response <- rnorm(50)
var1 <- rnorm(50)
var2 <- rnorm(50)
var3 <- rnorm(50)
myData <- data.frame(response,var1,var2,var3)
var.names <- names(myData)[2:4]


numVars <- length(var.names)
betas <- rep(-1,numVars)
names(betas) <- var.names

#run regression on var1 through var3.

for (Var_num in 1:numVars)
{
  col.name <- var.names[Var_num]
  mylm <- lm(response ~ get(col.name),data=myData,model=FALSE)
  betas[Var_num] <- coef(mylm)[2]
}




More information about the R-help mailing list