I have a dataset of mean annual temperature values at different latitudes in different years. I want to use this to predict the latitude at which a given temperature could be found in a given year; i.e., "in 1980, at what latitude would the mean annual temperature have been 20C?"
I need to use year-specific models for this, because the relationship between latitude and temperature has changed over time (although not in the sample data below, which is randomly generated). This will involve:
- Fit GAMs to a dataset grouped/split by year.
- For each different GAM (that is, for each year), use
predict.gam
to calculate a predicted value for every element in a list of temperatures. - Recombine these to get a dataframe with columns representing
year
,newdata_value
(the temperature value used for prediction), andpredicted_value
(latitude from feeding eachnewdata_value
into the year-specific GAM).
Here's a toy dataset:
years <- seq(1968, 2018, 1)
lat <- seq(34.5, 44.5, 1)
dat <- expand.grid(years, lat)
names(dat) <- c("years","lat")
dat$temp <- runif(dim(dat)[1], 5, 20) # add random temperature data points
newdata_values <- seq(2, 16, 2) # temperature values to use for prediction
I've tried various purrr
and split-apply-combine
solutions and haven't figured anything out. Any suggestions?