Suppose I have a big county that has 10 farms. In the county, the planting of a crop could happen between March to April. I collected for a given year, the planting date of a crop across the 10 farms and the date of harvesting and the total rainfall between planting date and harvest date.
set.seed(123) df <- data.frame(year = rep(2002:2008, each = 10), farm.id = c(1:10), plant.date = sample(151:210, 70, replace = T), harvest.date = sample(220:270, 70, replace = T), rainfall = sample(200:600, 70, replace = T))
I do not know the yield of each farm for each year. Instead I know the average yield across all the farms in each year
farm.yield <- data.frame(year = 2002:2008, avg.yield = sample(1800:4000, 7))
I am interested in writing a simple model that predicts average farm yield as a function of total rainfall. To do this, I calculate the average rainfall across all farms.
df.sum <- df %>% dplyr::group_by(year) %>% dplyr:::summarise(avg.rain = mean(rainfall), cv.rain = sd(rainfall)/avg.rain * 100) final.dat <- farm.yield %>% dplyr::left_join(df.sum)
and then I regress the avg.yield against avg. rainfall
lm(avg.yield ~ avg.rain, data = final.dat)
However, I am also interested in knowing the coefficient of variation around each prediction i.e. I expect years with high variability in rainfall among the 10 farms to also result in higher variability in avg yield. Since I do not know each farm's yield, how I can model such an effect? i.e. what tools I can use to model the variation around the
avg.yield as a function of the variation around the
Also note that the data here will not result in any model since they are just made up data.