user1471980
user1471980

Reputation: 10626

how do you append to a list in R

I am trying to pick the best span argument to the loess model. I need to store i and also the standard error to a list called error. After I run from 0.10 to 1, I can compare the least error and corresponding i to use for the model. I have tried this but does not seem to be working. any ideas?

z<-1
for(i in seq(from=0, to=1, by=0.10)){
  tryCatch({
      mdl <- loess(data=final.train, mCpu ~ mTrans_A,control=loess.control(surface="direct"), span=i)
      error[[z]]<-i
      error[[ z ]] <- mdl$s
      z=z+1
  }, error = function(err) {

  })
}

Upvotes: 0

Views: 67

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 145755

It's weird (and potentially buggy) to use z and i both as loop indices. Just pick one. Also you need to initialize your results list.

error = list()
my_seq = seq(from=0, to=1, by=0.10)
for(i in seq_along(my_seq)){
  tryCatch({
      mdl <- loess(data=final.train, mCpu ~ mTrans_A,control=loess.control(surface="direct"), span=my_seq[i])
      error[[i]] <- mdl$s
  }, error = function(err) return(NA))
}

Upvotes: 2

Related Questions