Hi, Look at this line:
cross.val.error<-append(cross.val.error.temp,value.temp) at each iteration you're overwriting cross.val.error with cross.val.error.temp and value.temp You probably actually need something like cross.val.error.temp <- append(cross.val.error.temp,value.temp) print(cross.val.error.temp) But this is far from a small reproducible example, so you could need something else entirely. Sarah On Fri, Jun 1, 2012 at 2:23 PM, Benjamin Caldwell <btcaldw...@berkeley.edu> wrote: > Hello all, > * > * > I'm having some difficulty, and I think the problem is with how I'm using > append() nested inside a for loop. The data are: > > y,x > 237537.61,873 > 5007.148438,227 > 17705.77306,400 > 12396.64369,427 > 228703.4021,1173 > 350181.9752,1538 > 59967.79376,630 > 140322.7774,710 > 42650.07251,630 > 5382.858702,264 > 34405.82429,637 > 92261.34614,980 > 144927.1713,1094 > 362998.7355,1420 > 203313.6442,1070 > 168861.649,930 > 214306.5266,1040 > 201804.5193,1000 > 108426.1988,820 > 181651.8748,980 > 53675.45849,775 > 43068.82972,563 > 92393.35158,805 > 93305.0618,740 > 107308.4761,840 > 139056.5446,775 > 15411.81924,420 > 16104.80991,390 > 25629.07578,502 > 37121.23438,461 > 29711.42749,565 > 33400.49145,510 > > and the code is: > > xy<-read.csv(xy.csv) > x<-xy$x > y<-xy$y > cross.val.error.temp<-numeric(0) > b<-c(1:length(x)) > > for(i in b){ > frame.val.temp<-data.frame(x,y) > frame.val<-frame.val.temp[-i,] > yt<-frame.val$y > xt<-frame.val$x > temppow.val<-lm(log(yt)~log(xt)) > tempint.val<-summary(temppow.val)$coefficients[1,1] #intercept of power > function > tempslope.val<-summary(temppow.val)$coefficients[2,1] > nls.model.val<-nls(yt~exp(intercept)*((xt)^slope), > start=list(intercept=tempint.val, slope=tempslope.val)) > prediction.cross.val<-((exp(intercept.nls))*((x[i])^slope.nls)) > value.temp<-(prediction.cross.val-y[i])/y[i] > cross.val.error<-append(cross.val.error.temp,value.temp) > cross.val.error > print(cross.val.error) > } > cross.val.error > > print() shows the values that I'm looking for, but I must be doing > something wrong with append because the call to cross.val.error just > returns the last value that the for loop returns. Would anyone be willing > to show me how to correct my mistake? > > As an aside, if anyone cares to explain how to make this work using lapply > instead of a for loop, I'm very interested. > > Many thanks for looking. > > *Ben Caldwell* > -- Sarah Goslee http://www.functionaldiversity.org ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.