woodland_creature
woodland_creature

Reputation: 301

Loess prediction for many groups of data

I'm using loess to interpolate data between measurement dates from individual plots. I would like to get daily resolution for all 48 plots in each year. Below is a sample from my dataset:

dput(vi.sample)

structure(list(year = c(2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L), julian = c(150L, 153L, 157L, 165L, 173L, 179L, 186L, 193L, 201L, 208L, 226L, 150L, 153L, 157L, 165L, 173L, 179L, 186L, 193L, 201L, 208L, 226L), jdx = c(2.573770492, 2.625245902, 2.693879781, 2.831147541, 2.968415301, 3.07136612, 3.19147541, 3.311584699, 3.448852459, 3.568961749, 3.877814208, 2.573770492, 2.625245902, 2.693879781, 2.831147541, 2.968415301, 3.07136612, 3.19147541, 3.311584699, 3.448852459, 3.568961749, 3.877814208), site = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "east", class = "factor"), type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "b", class = "factor"), trt = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "a", class = "factor"), plot = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), vi = c(0.41, 0.44, 0.52, 0.64, 0.66, 0.67, 0.64, 0.66, 0.61, 0.7, 0.7, 0.41, 0.45, 0.55, 0.61, 0.63, 0.66, 0.63, 0.64, 0.6, 0.7, 0.69)), .Names = c("year", "julian", "jdx", "site", "type", "trt", "plot", "vi"), class = "data.frame", row.names = c(NA, -22L))

I need to interpolate the data from each plot within each of three seasons (2012 - 2014) of data collected. I have successfully done so by using the subset function. Note that I am using julian dates, however I'm not particularly wedded to this date format.

lo <- loess(vi~julian, 
               subset(vi.sample, year=="2012" & site=="east" 
               & type=="b" & trt=="a" & plot=="1"),
               model=TRUE,
               na.action=na.exclude)

Question: Is there a way to automate the code to interpolate data from each plot/year combination without having to subset each one? I'm thinking along the lines of the nlsList function using the | year/site/type/trt/plot design.

My second question deals with the predict function. The following code successfully displays the fitted vi values for the plot specified above.

pred <- predict(lo, seq(from =150, to =226, 
            by = 1), se=FALSE)

However, the corresponding x-values are not the same as the original julian dates.
Question: How do I get the predicted values to represent the julian dates used in the loess model? Note that these dates may differ for each year/plot.

Upvotes: 0

Views: 874

Answers (1)

Rorschach
Rorschach

Reputation: 32456

For question 1, this is a standard data manipulation - split, apply, combine. Here using plyr

library(plyr)
fits <- dlply(vi.sample, .(year, site, type, trt, plot), .fun=function(samp)
    loess(vi ~ julian, model=T, na.action=na.exclude, data=samp))

You split the data into groups by year/site/type/trt/plot, apply the loess function, then combine the results back into a list.

For the second question, you choose the julian dates when you do the prediction, maybe I am missing something there.

Upvotes: 1

Related Questions