0

I have a tree growth database containing the number of cells in every growth stage (enlarging, thickening, mature) for every DOY (Day of the year) for every tree and every year (6 different trees per year, 10 years, 60 trees in total). We went to the field to take samples every 1-2 weeks as you can see. The database looks like this (simplified):

print(xylo)

  Year Tree DOY Enlarging Thickening Mature
  2012  25  80    0         0          0
  2012  25  87    1         0          0
  2012  25  94    4         0          0
  2012  25  103   5         1          0
  2012  25  111   3         3          0
  2012  25  119   1         4          1
  2012  25  127   1         5          3
  2012  25  139   0         3          5
  2012  30  80    0         0          0
  2012  30  87    2         0          0
  2012  30  94    5         1          0
  2012  30  103   7         3          1
  2012  30  111   4         6          2
  2012  30  119   3         7          5
  2012  30  127   1         8          7
  2012  30  139   1         4          8
  2012  43  80    0         0          0
  2012  43  87    0         0          0
  2012  43  94    2         0          0
  etc.

I would like to apply a GAM function to obtain predictions about when does every growth stage start and finish for every tree, every year, and also understand the growth curves and pattern every stage follows.

First of all, in order to build the model, I just use a simple GAM between every growth phase number of cells (enlarging, thickening, mature) and the day of the year (DOY, from my database):

enlarging <- gam(Enlarging ~ s(DOY), data=xylo, quasipoisson, gamma=1, 
                  min.sp=0.01)  
thickening <- gam(Thickening ~ s(DOY), data=xylo, quasipoisson, gamma=1, 
                  min.sp=0.01)  
mature <- gam(Mature ~ s(DOY), data=xylo, quasipoisson, gamma=1, 
                  min.sp=0.01)

Now, I would like to predict for each day of the year (starting from the first the day of the year I go to the field to take samples until the last one) the number of cells in every growth stage using the GAM model built in the previous step. For that, I create a daily sequence called DOY starting the first day we took samples and ending the last day we took samples.

DOY <- seq(min(xylo$DOY), max(xylo$DOY), 1)

My plan is to apply then the predict function using the model and the sequence I created above:

enlarging_gam <- as.vector(predict(enlarging, data.frame(DOY), 
                           type="response"))
thickening_gam <- as.vector(predict(thickening, data.frame(DOY), 
                            type="response"))
mature_gam <- as.vector(predict(mature, data.frame(DOY), type="response"))
  
fit <- data.frame(cbind(DOY = DOY, enlarging_gam = enlarging_gam,
                  thickening_gam = thickening_gam, mature_gam = mature_gam))

Lastly, I would like to create a simple custom function to apply this predictions to every tree of every year I have on my database. Is it possible to do it this way? I have tried different things and I haven't been able to do it. I just want to use a GAM model to predict the daily number of cells of every growth stage for every Tree, every Year. Could someone help?

kjetil b halvorsen
  • 1,206
  • 2
  • 18
  • 28
  • Welcome to SO! I think this tutorial is relevant -- it walks through grouped predictions with dplyr: https://cran.r-project.org/web/packages/broom/vignettes/broom_and_dplyr.html – Jon Spring Feb 24 '23 at 19:55
  • 1
    If you'd like people to try answering your question with specifics, it's helpful to make it as easy as possible for them by including specific example data they can load. (This also helps avoid ambiguity so they can confirm their solutions work for your specific data structure.) This answer has some great tips. https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Jon Spring Feb 24 '23 at 19:58

0 Answers0