Hols84
Hols84

Reputation: 13

Calling a previous saved value within a loop in R

I am pretty new using R and I would really appreciate some input. I've created several subsets (one for each iteration) using a loop. then I ran regressions using each subset/iteration and saved the coefficients (they are included in each subset/iteration as variables: single value for all rows in a column). what i would like to do is to save the coefficient obtained using the iteration i-1 in the iteration i (as i want to do predictions using the coefficients of the previous subset and the variables of the current subset)

Can someone help me with this.
Thanks.

here a reproducible example:

Data <- data.frame(
    X = sample(1:10),
    Y = sample(1:10)
)
Data$obs<-as.numeric(row.names(Data))
temp=NULL
obs=unique(Data$obs)
for (i in 2:length(obs)) {
    temp<-subset(Data, obs<=obs[i])
    ols <- lm(Y~X, data=temp)
    temp$intercept <-coef(summary(ols))["(Intercept)","Estimate"]
    temp$coefX <-coef(summary(ols))["X","Estimate"]

    #this is what i am trying to do
    temp$coefXprevious <- temp$coefX [i-1]

    write.csv(temp, paste(obs[i], ".csv", sep=""))
}

so for the last subset/iteration, I am getting:

X   Y   obs intercept   coefx   coefxprevious
8   5   1   9.8    -0.781818182 -0.781818182 
2   10  2   9.8    -0.781818182 -0.781818182 
10  1   3   9.8    -0.781818182 -0.781818182 
3   8   4   9.8    -0.781818182 -0.781818182 
9   2   5   9.8    -0.781818182 -0.781818182
5   4   6   9.8    -0.781818182 -0.781818182 
4   6   7   9.8    -0.781818182 -0.781818182 
7   3   8   9.8    -0.781818182 -0.781818182
6   9   9   9.8    -0.781818182 -0.781818182
1   7   10  9.8    -0.781818182 -0.781818182

this output is fine but the last column. I would appreciate any help. Thanks

Upvotes: 1

Views: 1559

Answers (2)

Hols84
Hols84

Reputation: 13

got it!!!

Data <- data.frame(X = sample(1:10),Y = sample(1:10))
Data$obs<-as.numeric(row.names(Data))

temp=NULL
temp2=NULL
obs=unique(Data$obs)

for (i in 8:length(obs)){
    temp1<-subset(Data, obs<=obs[i-1])
    temp2<-subset(Data, obs<=obs[i])
    ols1 <- lm(Y~X, data=temp1) 
    ols2 <- lm(Y~X, data=temp2) 

    temp2$coefx <-coef(summary(ols2))["X","Estimate"]
    temp2$coefxprev <- coef(summary(ols1))["X","Estimate"]
    temp2$predict <-predict(ols1,temp2)

write.csv(temp2, paste(obs[i], ".csv", sep=""))
}

Upvotes: 0

PeterK
PeterK

Reputation: 1243

You are creating a totally new temp on each iteration of the loop. I think you want this:

Data <- data.frame(
  X = sample(1:10),
  Y = sample(1:10),
  intercept = rep(NA,10), coefX = rep(NA,10), coefXprevious = rep(NA,10)
)
Data$obs<-as.numeric(row.names(Data))

for (i in 2:length(obs)) {
  ols <- lm(Y~X, data=Data[Data$obs<=i,])
  Data$intercept[i] <-coef(summary(ols))["(Intercept)","Estimate"]
  Data$coefX[i] <-coef(summary(ols))["X","Estimate"]

  Data$coefXprevious[i] <- Data$coefX [i-1]

  write.csv(Data[Data$obs<-i,], paste(obs[i], ".csv", sep=""))
}

Which will make, as the last output (but why do you need the other outputs?):

    X  Y intercept      coefX coefXprevious obs

1   6  6        NA         NA            NA   1
2   9  4 10.000000 -0.6666667            NA   2
3   3  5  6.000000 -0.1666667    -0.6666667   3
4   8  9  5.071429  0.1428571    -0.1666667   4
5   2  2  2.580645  0.4677419     0.1428571   5
6   4  8  3.813559  0.3474576     0.4677419   6
7   1  1  2.363402  0.5592784     0.3474576   7
8   7  3  2.500000  0.4500000     0.5592784   8
9  10  7  2.500000  0.4500000     0.4500000   9
10  5 10  3.200000  0.4181818     0.4500000  10

Upvotes: 1

Related Questions