Daily interpolation of growth data in R using grouping by variables

Asked

Viewed 26 times

0

Good morning, I am trying to perform linear interpolation of tree growth, however I would like to interpolate using some variables such as group_by in dplyr.

These variables are: Block (n=3), Genotype (n=22) and Individual (n=4), totaling 264 interpolations.

Measurements range from 06/01/2018 to 06/04/2019, at irregular intervals.

The code below is that I am using:

    b4 <- read.csv("Interpolation_arrumado.csv", h=T, sep=";")

#Informações sobre as 100 primeiras linhas dos dados:
    dput(b4[1:100,] )

# structure(list(Date = structure(c(76L, 76L, 76L, 76L, 76L, 76L, 
# 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 
# 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 
# 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 
# 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 
# 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 
# 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 
# 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 76L, 
# 76L, 76L, 76L), .Label = c("01/01/2019", "01/02/2018", "01/02/2019", 
# "01/03/2018", "01/03/2019", "01/04/2018", "01/04/2019", "01/05/2018", 
# "01/06/2018", "01/07/2018", "01/08/2018", "01/09/2018", "01/10/2018", 
# "01/11/2018", "01/12/2018", "02/01/2019", "02/02/2018", "02/02/2019", 
# "02/03/2018", "02/03/2019", "02/04/2018", "02/04/2019", "02/05/2018", 
# "02/06/2018", "02/07/2018", "02/08/2018", "02/09/2018", "02/10/2018", 
# "02/11/2018", "02/12/2018", "03/01/2019", "03/02/2018", "03/02/2019", 
# "03/03/2018", "03/03/2019", "03/04/2018", "03/04/2019", "03/05/2018", 
# "03/06/2018", "03/07/2018", "03/08/2018", "03/09/2018", "03/10/2018", 
# "03/11/2018", "03/12/2018", "04/01/2019", "04/02/2018", "04/02/2019", 
# "04/03/2018", "04/03/2019", "04/04/2018", "04/04/2019", "04/05/2018", 
# "04/06/2018", "04/07/2018", "04/08/2018", "04/09/2018", "04/10/2018", 
# "04/11/2018", "04/12/2018", "05/01/2019", "05/02/2018", "05/02/2019", 
# "05/03/2018", "05/03/2019", "05/04/2018", "05/04/2019", "05/05/2018", 
# "05/06/2018", "05/07/2018", "05/08/2018", "05/09/2018", "05/10/2018", 
# "05/11/2018", "05/12/2018", "06/01/2018", "06/01/2019", "06/02/2018", 
# "06/02/2019", "06/03/2018", "06/03/2019", "06/04/2018", "06/04/2019", 
# "06/05/2018", "06/06/2018", "06/07/2018", "06/08/2018", "06/09/2018", 
# "06/10/2018", "06/11/2018", "06/12/2018", "07/01/2018", "07/01/2019", 
# "07/02/2018", "07/02/2019", "07/03/2018", "07/03/2019", "07/04/2018", 
# "07/05/2018", "07/06/2018", "07/07/2018", "07/08/2018", "07/09/2018", 
# "07/10/2018", "07/11/2018", "07/12/2018", "08/01/2018", "08/01/2019", 
# "08/02/2018", "08/02/2019", "08/03/2018", "08/03/2019", "08/04/2018", 
# "08/05/2018", "08/06/2018", "08/07/2018", "08/08/2018", "08/09/2018", 
# "08/10/2018", "08/11/2018", "08/12/2018", "09/01/2018", "09/01/2019", 
# "09/02/2018", "09/02/2019", "09/03/2018", "09/03/2019", "09/04/2018", 
# "09/05/2018", "09/06/2018", "09/07/2018", "09/08/2018", "09/09/2018", 
# "09/10/2018", "09/11/2018", "09/12/2018", "10/01/2018", "10/01/2019", 
# "10/02/2018", "10/02/2019", "10/03/2018", "10/03/2019", "10/04/2018", 
# "10/05/2018", "10/06/2018", "10/07/2018", "10/08/2018", "10/09/2018", 
# "10/10/2018", "10/11/2018", "10/12/2018", "11/01/2018", "11/01/2019", 
# "11/02/2018", "11/02/2019", "11/03/2018", "11/03/2019", "11/04/2018", 
# "11/05/2018", "11/06/2018", "11/07/2018", "11/08/2018", "11/09/2018", 
# "11/10/2018", "11/11/2018", "11/12/2018", "12/01/2018", "12/01/2019", 
# "12/02/2018", "12/02/2019", "12/03/2018", "12/03/2019", "12/04/2018", 
# "12/05/2018", "12/06/2018", "12/07/2018", "12/08/2018", "12/09/2018", 
# "12/10/2018", "12/11/2018", "12/12/2018", "13/01/2018", "13/01/2019", 
# "13/02/2018", "13/02/2019", "13/03/2018", "13/03/2019", "13/04/2018", 
# "13/05/2018", "13/06/2018", "13/07/2018", "13/08/2018", "13/09/2018", 
# "13/10/2018", "13/11/2018", "13/12/2018", "14/01/2018", "14/01/2019", 
# "14/02/2018", "14/02/2019", "14/03/2018", "14/03/2019", "14/04/2018", 
# "14/05/2018", "14/06/2018", "14/07/2018", "14/08/2018", "14/09/2018", 
# "14/10/2018", "14/11/2018", "14/12/2018", "15/01/2018", "15/01/2019", 
# "15/02/2018", "15/02/2019", "15/03/2018", "15/03/2019", "15/04/2018", 
# "15/05/2018", "15/06/2018", "15/07/2018", "15/08/2018", "15/09/2018", 
# "15/10/2018", "15/11/2018", "15/12/2018", "16/01/2018", "16/01/2019", 
# "16/02/2018", "16/02/2019", "16/03/2018", "16/03/2019", "16/04/2018", 
# "16/05/2018", "16/06/2018", "16/07/2018", "16/08/2018", "16/09/2018", 
# "16/10/2018", "16/11/2018", "16/12/2018", "17/01/2018", "17/01/2019", 
# "17/02/2018", "17/02/2019", "17/03/2018", "17/03/2019", "17/04/2018", 
# "17/05/2018", "17/06/2018", "17/07/2018", "17/08/2018", "17/09/2018", 
# "17/10/2018", "17/11/2018", "17/12/2018", "18/01/2018", "18/01/2019", 
# "18/02/2018", "18/02/2019", "18/03/2018", "18/03/2019", "18/04/2018", 
# "18/05/2018", "18/06/2018", "18/07/2018", "18/08/2018", "18/09/2018", 
# "18/10/2018", "18/11/2018", "18/12/2018", "19/01/2018", "19/01/2019", 
#"19/02/2018", "19/02/2019", "19/03/2018", "19/03/2019", "19/04/2018", 
#"19/05/2018", "19/06/2018", "19/07/2018", "19/08/2018", "19/09/2018", 
#"19/10/2018", "19/11/2018", "19/12/2018", "20/01/2018", "20/01/2019", 
#"20/02/2018", "20/02/2019", "20/03/2018", "20/03/2019", "20/04/2018", 
#"20/05/2018", "20/06/2018", "20/07/2018", "20/08/2018", "20/09/2018", 
#"20/10/2018", "20/11/2018", "20/12/2018", "21/01/2018", "21/01/2019", 
#"21/02/2018", "21/02/2019", "21/03/2018", "21/03/2019", "21/04/2018", 
#"21/05/2018", "21/06/2018", "21/07/2018", "21/08/2018", "21/09/2018", 
#"21/10/2018", "21/11/2018", "21/12/2018", "22/01/2018", "22/01/2019", 
#"22/02/2018", "22/02/2019", "22/03/2018", "22/03/2019", "22/04/2018", 
#"22/05/2018", "22/06/2018", "22/07/2018", "22/08/2018", "22/09/2018", 
#"22/10/2018", "22/11/2018", "22/12/2018", "23/01/2018", "23/01/2019", 
#"23/02/2018", "23/02/2019", "23/03/2018", "23/03/2019", "23/04/2018", 
#"23/05/2018", "23/06/2018", "23/07/2018", "23/08/2018", "23/09/2018", 
#"23/10/2018", "23/11/2018", "23/12/2018", "24/01/2018", "24/01/2019", 
#"24/02/2018", "24/02/2019", "24/03/2018", "24/03/2019", "24/04/2018", 
#"24/05/2018", "24/06/2018", "24/07/2018", "24/08/2018", "24/09/2018", 
#"24/10/2018", "24/11/2018", "24/12/2018", "25/01/2018", "25/01/2019", 
#"25/02/2018", "25/02/2019", "25/03/2018", "25/03/2019", "25/04/2018", 
#"25/05/2018", "25/06/2018", "25/07/2018", "25/08/2018", "25/09/2018", 
#"25/10/2018", "25/11/2018", "25/12/2018", "26/01/2018", "26/01/2019", 
#"26/02/2018", "26/02/2019", "26/03/2018", "26/03/2019", "26/04/2018", 
#"26/05/2018", "26/06/2018", "26/07/2018", "26/08/2018", "26/09/2018", 
#"26/10/2018", "26/11/2018", "26/12/2018", "27/01/2018", "27/01/2019", 
#"27/02/2018", "27/02/2019", "27/03/2018", "27/03/2019", "27/04/2018", 
#"27/05/2018", "27/06/2018", "27/07/2018", "27/08/2018", "27/09/2018", 
#"27/10/2018", "27/11/2018", "27/12/2018", "28/01/2018", "28/01/2019", 
#"28/02/2018", "28/02/2019", "28/03/2018", "28/03/2019", "28/04/2018", 
#"28/05/2018", "28/06/2018", "28/07/2018", "28/08/2018", "28/09/2018", 
#"28/10/2018", "28/11/2018", "28/12/2018", "29/01/2018", "29/01/2019", 
#"29/03/2018", "29/03/2019", "29/04/2018", "29/05/2018", "29/06/2018", 
#"29/07/2018", "29/08/2018", "29/09/2018", "29/10/2018", "29/11/2018", 
#"29/12/2018", "30/01/2018", "30/01/2019", "30/03/2018", "30/03/2019", 
#"30/04/2018", "30/05/2018", "30/06/2018", "30/07/2018", "30/08/2018", 
#"30/09/2018", "30/10/2018", "30/11/2018", "30/12/2018", "31/01/2018", 
#"31/01/2019", "31/03/2018", "31/03/2019", "31/05/2018", "31/07/2018", 
#"31/08/2018", "31/10/2018", "31/12/2018"), class = "factor"), 
#    Block = c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
#    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
#    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
#    1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 
#    1L, 2L, 3L, 1L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
#    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 
#    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L), Gen = structure(c(1L, 
#    1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 
#    6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 
#    11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L, 14L, 
#    15L, 15L, 15L, 16L, 16L, 16L, 17L, 17L, 17L, 18L, 18L, 18L, 
#    19L, 19L, 19L, 20L, 20L, 20L, 21L, 21L, 22L, 22L, 22L, 1L, 
#    1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 
#    6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 
#    11L, 11L, 11L, 12L, 12L), .Label = c("G1", "G10", "G11", 
#    "G12", "G13", "G14", "G15", "G16", "G17", "G18", "G19", "G2", 
#    "G20", "G21", "G22", "G3", "G4", "G5", "G6", "G7", "G8", 
#    "G9"), class = "factor"), Tree = c(1L, 1L, 1L, 1L, 1L, 1L, 
#    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
 #   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
 #   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
 #   1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
 #   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
 #   2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
 #   2L, 2L, 2L, 2L), Volume = c(12.01525028, 11.88425197, 20.17471495, 
 #   13.32679626, 13.76410084, 8.008377467, 16.60606293, 20.10688407, 
 #   9.012805931, 5.06545153, 12.98761697, 7.215095791, 16.11152331, 
 #   28.13968031, 9.217642429, 6.521312815, 8.088493546, 17.00897414, 
 #   14.52575954, 15.59076128, 22.50477072, 11.84376032, 7.276038667, 
 #   14.86227807, 12.5744528, 9.24930351, 9.505297232, 5.643898267, 
 #   7.913097203, 8.684094463, 8.842618097, 22.74637184, 9.694342067, 
 #   32.78613635, 26.34365045, 21.80658736, 10.37204932, 0.699789764, 
 #   3.279007487, 4.205211637, 14.02768702, 6.656816995, 9.063071052, 
 #   13.41924315, 7.53526153, 14.03467248, 8.736854205, 17.72732933, 
  #  7.524139538, 7.829633537, 7.013214479, 12.55516141, 16.61908005, 
  #  13.67170864, 14.4784947, 12.36648824, 5.460956117, 13.54051179, 
   # 6.448122063, 8.820013332, 20.25484088, 14.29157308, 6.403400832, 
    # 1.2829479, 9.428919585, 8.835444993, 18.4750254, 4.354103572, 
    # 13.54497438, 9.888974594, 9.76785239, 16.36704792, 14.99247084, 
    # 11.31044173, 12.28478338, 4.378817814, 11.68171286, 10.01270857, 
    # 21.38691877, 16.052297, 12.59138875, 6.963452429, 18.79736075, 
    # 24.38803715, 13.78583035, 9.326797969, 7.58972539, 16.10188358, 
    # 11.73195673, 16.47054345, 17.18585689, 8.671224551, 7.371410422, 
    # 21.06658767, 9.692401317, 6.245290074, 8.286406532, 13.82058036, 
    # 28.46599617, 17.93357759)), row.names = c(NA, 100L), class = "data.frame")

I entered daily values between 01/06/2018 until 04/06/2019, where growth values where they were not measured were "NA". After that I ran the following code:

b5 <- b4 %>%
  group_by(Block, Gen, Tree) %>%
  nest(data = c(Date, Volume)) %>%
  mutate(ApproxFun = map(data, approxfun),
         LinearFit = map2(ApproxFun, data, ~.x(.y$Date))) %>%
  unnest(c(data, LinearFit))

The code has run, however the B5 data.frame has errors. Some of them are some interpolations are with NA and various interpolations at different dates decrease over time, which makes no sense for growth data that is always ascending.

Could someone help me if there is some error in my code, so these interpolation values are not correct?

Thank you.

  • 5

    The date is not a date is a "factor". Start the pipe with b4 %>% mutate(Date = as.Date(Date, "%d/%m/%Y")) %>% arrange(Date).

  • 2

    Thanks @Rui Barradas. It worked.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.