Question

I have the following example (which is in similar format to my data):

set.seed(1)
RandData <- runif(8760*2)
Locations <- rep(c('UK','France'),each=8760)

Date = seq(from=as.POSIXct("1991-01-01 00:00"), 
           to=as.POSIXct("1991-12-31 23:00"), length=8760)

Final <- data.frame(Loc = Locations,
                    Doy = as.numeric(format(Date,format = "%j")),
                    Tod = as.numeric(format(Date,format = "%H")),
                    Temp = RandData)
require(mgcv)
mod1 <- gam(Temp ~ Loc + s(Doy) + s(Doy,by = Loc) +
  s(Tod) + s(Tod,by = Loc),data = Final)
plot(mod1,pages = 1, scale = 0)

From this model, the outcome which is plotted firstly shows the mean temperature variation as a function of day of year and then the amount that the temperature at each location varies from this mean. The the same is shown for the Time of day.

The problem with this is that the yaxis do not show the correct range of values i.e. the first plot does not show the mean temperature for these locations. How can the plots be altered to show (1) the mean temperature for both locations and (2) the temperature at each location compared to the mean instead of some arbitrary number.

If I haven't been clear in expressing my intentions let me know and I will try to provide a better example.

Was it helpful?

Solution

The plots of a gam model display the effect of the smoother itself. If you want the combined effect of several part of the model, then create a data.frame with the relevant values for each value, calculcate prediction for those values with predict(model, newdata) and create the plot you want.

dataset <- expand.grid(Loc = unique(Final$Loc), 
    Doy = pretty(Final$Doy), Tod = pretty(Final$Tod))
dataset$Temp <- predict(mod1, newdata = dataset)
library(ggplot2)
ggplot(dataset, aes(x = Doy, y = Temp, linetype = Loc)) + geom_line() + 
    facet_wrap(~Tod)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top