Hey Jean, This appears to have worked!! Thank you so much, this is an immense help to me.
Best On Tue, Mar 26, 2013 at 11:33 AM, Adams, Jean <jvad...@usgs.gov> wrote: > Thanks. That helps. > > Using the example data you sent, I came up with this loop. See if it does > the trick for you. > > niter <- 2 > nspp <- 11 > ntrees <- 22 > lnbm <- matrix(NA, niter, ntrees) > for(i in 1:niter) { > for(j in 1:ntrees) { > k <- j1data$its==i & j1data$spp==predictdata$spp[j] > lnbm[i, j] <- j1data$b0[k] + j1data$b1[k]*predictdata$lndbh[j] > } > } > > Jean > > P.S. You should cc rhelp on all correspondence, so other folks can see > the messages unfold. > > > > On Tue, Mar 26, 2013 at 8:41 AM, Curtis Burkhalter < > curtisburkhal...@gmail.com> wrote: > >> Hey Jean, >> >> Sorry for not including this before, here is the first 22 rows of j1data: >> >> its spp b0 b1 >> 1 1 1 -1.707 2.124 >> 2 1 2 -2.679 2.478 >> 3 1 3 -1.103 1.984 >> 4 1 4 -1.895 2.258 >> 5 1 5 -1.372 2.127 >> 6 1 6 -2.486 2.372 >> 7 1 7 -2.261 2.336 >> 8 1 8 -1.855 2.147 >> 9 1 9 -1.923 2.264 >> 10 1 10 -1.772 2.250 >> 11 1 11 -2.540 2.272 >> 12 2 1 -1.862 2.163 >> 13 2 2 -2.189 2.329 >> 14 2 3 -1.253 2.029 >> 15 2 4 -2.533 2.443 >> 16 2 5 -1.262 2.085 >> 17 2 6 -2.800 2.483 >> 18 2 7 -2.547 2.426 >> 19 2 8 -1.853 2.155 >> 20 2 9 -2.026 2.297 >> 21 2 10 -1.966 2.297 >> 22 2 11 -2.850 2.345 >> > >> and here is the first 22 rows of predictdata: >> >> spp dbh height bm lndbh dbh2 ln_h h2 logbm >> logdbh >> 1 1 16.3 9.4 66.64 2.791165 265.69 2.240710 88.36 1.823735 >> 1.212188 >> 2 1 18.3 10.9 85.75 2.906901 334.89 2.388763 118.81 1.933234 >> 1.262451 >> 3 1 19.3 7.1 61.25 2.960105 372.49 1.960095 50.41 1.787106 >> 1.285557 >> 4 1 19.6 13.0 106.82 2.975530 384.16 2.564949 169.00 2.028653 >> 1.292256 >> 5 1 21.6 13.0 121.52 3.072693 466.56 2.564949 169.00 2.084648 >> 1.334454 >> 6 1 22.9 11.7 127.40 3.131137 524.41 2.459589 136.89 2.105169 >> 1.359835 >> 7 1 24.6 13.6 150.43 3.202746 605.16 2.610070 184.96 2.177334 >> 1.390935 >> 8 1 24.9 13.8 162.19 3.214868 620.01 2.624669 190.44 2.210024 >> 1.396199 >> 9 1 25.9 19.4 269.01 3.254243 670.81 2.965273 376.36 2.429768 >> 1.413300 >> 10 1 26.2 16.8 237.65 3.265759 686.44 2.821379 282.24 2.375938 >> 1.418301 >> 11 1 26.2 19.2 299.39 3.265759 686.44 2.954910 368.64 2.476237 >> 1.418301 >> 12 1 27.7 11.3 203.84 3.321432 767.29 2.424803 127.69 2.309289 >> 1.442480 >> 13 1 27.9 15.6 255.78 3.328627 778.41 2.747271 243.36 2.407867 >> 1.445604 >> 14 1 27.9 17.7 235.20 3.328627 778.41 2.873565 313.29 2.371437 >> 1.445604 >> 15 1 28.7 18.8 279.79 3.356897 823.69 2.933857 353.44 2.446832 >> 1.457882 >> 16 1 28.7 20.2 326.83 3.356897 823.69 3.005683 408.04 2.514322 >> 1.457882 >> 17 1 29.0 14.4 240.10 3.367296 841.00 2.667228 207.36 2.380392 >> 1.462398 >> 18 1 29.2 16.9 305.27 3.374169 852.64 2.827314 285.61 2.484684 >> 1.465383 >> 19 1 30.0 12.9 211.19 3.401197 900.00 2.557227 166.41 2.324673 >> 1.477121 >> 20 1 30.7 21.8 385.14 3.424263 942.49 3.081910 475.24 2.585619 >> 1.487138 >> 21 1 31.0 15.0 282.24 3.433987 961.00 2.708050 225.00 2.450619 >> 1.491362 >> 22 1 31.2 20.0 388.57 3.440418 973.44 2.995732 400.00 2.589469 >> 1.494155 >> >> Also here is a little further clarification of what is happening when I >> run the code and some abbreviated ouput: >> >> [,1] [,2] [,3] [,4] [,5] >> [1,] 44.76950 58.23457 65.71707 68.06092 84.87327 >> [2,] 44.76950 58.23457 65.71707 68.06092 84.87327 >> [3,] 44.76950 58.23457 65.71707 68.06092 84.87327 >> [4,] 44.76950 58.23457 65.71707 68.06092 84.87327 >> [5,] 44.76950 58.23457 65.71707 68.06092 84.87327 >> [6,] 44.76950 58.23457 65.71707 68.06092 84.87327 >> [7,] 44.76950 58.23457 65.71707 68.06092 84.87327 >> [8,] 44.76950 58.23457 65.71707 68.06092 84.87327 >> [9,] 44.76950 58.23457 65.71707 68.06092 84.87327 >> [10,] 44.76950 58.23457 65.71707 68.06092 84.87327 >> [11,] 44.76950 58.23457 65.71707 68.06092 84.87327 >> [12,] 88.23264 114.99591 129.88904 134.55690 168.07243 >> [13,] 88.23264 114.99591 129.88904 134.55690 168.07243 >> [14,] 88.23264 114.99591 129.88904 134.55690 168.07243 >> [15,] 88.23264 114.99591 129.88904 134.55690 168.07243 >> [16,] 88.23264 114.99591 129.88904 134.55690 168.07243 >> [17,] 88.23264 114.99591 129.88904 134.55690 168.07243 >> [18,] 88.23264 114.99591 129.88904 134.55690 168.07243 >> [19,] 88.23264 114.99591 129.88904 134.55690 168.07243 >> [20,] 88.23264 114.99591 129.88904 134.55690 168.07243 >> [21,] 88.23264 114.99591 129.88904 134.55690 168.07243 >> [22,] 88.23264 114.99591 129.88904 134.55690 168.07243 >> >> So it appears that it is calculating the same value for biomass 11x using >> each iteration of the regression coefficients and then moves on. For the >> predicted biomass located in [1,1] I know this is correct b/c I calculated >> it by hand just to see if the code was working correctly. Moving beyond >> that though the values contained within [1,2:5] are incorrect for the >> corresponding trees and the subsequent values contained in [12:22,] are >> incorrect so I know that it's not then using the 2nd iteration of the >> regression coefficients for each tree. So to sum that up, it seems to work >> correctly in predicting biomass for the first tree using the first >> iteration of the regression coefficients, but beyond that something goes >> wrong. I hope this helps in making my point clearer. >> >> Thank you >> >> >> On Tue, Mar 26, 2013 at 8:33 AM, Adams, Jean <jvad...@usgs.gov> wrote: >> >>> It would be helpful to see what your data frames look like ... perhaps >>> you could share the first few rows of each with us. >>> >>> dput(head(j1data)) >>> >>> dput(head(predictdata)) >>> >>> Jean >>> >>> >>> On Mon, Mar 25, 2013 at 9:29 PM, Curtis Burkhalter < >>> curtisburkhal...@gmail.com> wrote: >>> >>>> Hello, >>>> >>>> I'm working on a problem using nested for-loops and I don't know if >>>> it's a >>>> problem with the order of the loops or something within the loop so any >>>> help with the problem would be appreciated. To briefly set up the >>>> problem. >>>> I have 259 trees (from 11 different species, of unequal count for each >>>> species) of which I am trying to predict biomass. For each tree >>>> species I >>>> have 10000 iterations for the regression coefficients, which were >>>> estimated >>>> previously in WinBUGS, and what I'm trying to do is for each tree I >>>> want to >>>> predict the biomass using each species-specific iteration of the >>>> regression >>>> coefficients, so that ultimately each tree has 10000 estimates of >>>> biomass, >>>> organized into a 10000x259 matrix. The input data used in the model >>>> equation is stored in two separate files and I don't think this is >>>> creating >>>> problems, but I thought it might be worth mentioning. I've pasted the >>>> code >>>> below and if any additional info is needed please write back and I will >>>> post it. >>>> >>>> #read in the model output data from Jenkins eq. 1 under "data" >>>> #read in the prediction data set under "predict data" >>>> >>>> j1data=read.delim("reduced_j1_forR.txt",header=T) >>>> predictdata=read.delim("predictset_forR.txt",header=T) >>>> j1data=j1data[,2:4] >>>> its=c(rep(1:10000,each=11)) >>>> j1data=cbind(its,j1data) >>>> >>>> #set up a matrix full of zeros "lnbm" where prediction results are >>>> placed >>>> #set up for loop that first loops over iteration, then species >>>> #(total iterations=10000it/spp*11spp=110000 iterations)and then over >>>> each >>>> tree >>>> #(total # of trees=259) >>>> >>>> niter=10000 >>>> nspp=11 >>>> ntrees=259 >>>> >>>> lnbm=matrix(0,10000,259) >>>> k=numeric() >>>> for (i in 1:ntrees) >>>> { >>>> for (j in 1:nspp) >>>> { >>>> for (m in 1:niter) >>>> { >>>> k=((j1data$its[m]-1)*1000)+(j1data$spp[j]) >>>> #print(k) >>>> lnbm[m,i]=j1data$b0[k]+j1data$b1[k]*predictdata$lndbh[i] >>>> } >>>> } >>>> } >>>> >>>> >>>> >>>> Thanks >>>> >>>> [[alternative HTML version deleted]] >>>> >>>> ______________________________________________ >>>> 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. >>>> >>> >>> >> >> >> -- >> Curtis Burkhalter >> > > -- Curtis Burkhalter [[alternative HTML version deleted]] ______________________________________________ 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.