简体   繁体   English

使用 GAM 分析时空模式

[英]Analysing spatio-temporal patterns using a GAM

I am trying to analyse the spatio-temporal risk from disease using the following model;我正在尝试使用以下 model 分析疾病的时空风险;

mod2 = gam(TB ~ offset(log(Population)) + 
       s(Indigenous, k = 10, bs = "cr") + 
       s(Urbanisation, k = 10, bs = "cr") +
       s(Density, k = 10, bs = "cr") + Poverty + 
       s(Poor_Sanitation, k = 10, bs = "cr") + 
       Unemployment + Timeliness + Year + 
              Region + s(lon, lat), 
              data = TBdata, 
              family = nb(link = 'log'))

My issue is that I also want to analyze the difference is years (2012-2014) which i am not confident that my model does at the moment.我的问题是我还想分析年(2012-2014)的差异,我不相信我的 model 目前确实如此。 How would I be able to do this?我怎么能做到这一点? I've tried using 'Years' in an interaction with 'Population' as it makes some sort of sense but wasn't able to do this successfully.我曾尝试在与“人口”的互动中使用“年”,因为它有某种意义,但未能成功地做到这一点。

Data if needed;数据(如果需要);

TBdata
# A tibble: 1,671 × 14
Indigenous Illiteracy Urbanisation Density Poverty Poor_Sanitation Unemployment Timeliness  Year    TB Population Region   lon    lat
<dbl>      <dbl>        <dbl>   <dbl>   <dbl>           <dbl>        <dbl>      <dbl> <int> <int>      <int>  <int> <dbl>  <dbl>
 1      0.335       6.35         84.1   0.714    31.3            15.3         5.41       59.2  2012   323     559543  11001 -60.7 -12.1 
 2      6.45        8.49         71.4   0.743    48.6            29.4         5.92       58.1  2012    15      73193  11002 -64.0  -9.43

Added information;添加信息;

After editing my model i am now trying to plot my predictions.在编辑了我的 model 之后,我现在正在尝试 plot 我的预测。

I have used the current code which produces the following plot, although I think i might be missing my predictions so im just currently plotting the smooth function;我使用了产生以下 plot 的当前代码,尽管我认为我可能会错过我的预测,所以我目前正在绘制平滑的 function;

mod2 = gam(TB ~ offset(log(Population)) + s(Indigenous, k = 10, bs = "cr") + s(Urbanisation, k = 10, bs = "cr") +
              s(Density, k = 10, bs = "cr") + Poverty + s(Poor_Sanitation, k = 10, bs = "cr") + Unemployment + Timeliness +
              as.factor(Year) + Region + s(lon, lat), 
              data = TBdata, 
              family = nb(link = 'log'))

model summary model总结

在此处输入图像描述

#plot
plot(mod2, contour.col = 'white', too.far = 0.10, scheme = 2, rug = T)

在此处输入图像描述

Although i think this shows the spatial trends I now need this for the different years, so i think what i want is a plot showing my predictions for each of the years (2012, 2013 and 2014)虽然我认为这显示了我现在需要的不同年份的空间趋势,所以我认为我想要的是 plot 显示我对每一年(2012、2013 和 2014 年)的预测

When you plot() your model, you are just visualising the estimated smooth functions.当您plot() model 时,您只是在可视化估计的平滑函数。 As you only have a single spatial smooth, you get a single plot.由于您只有一个空间平滑,因此您得到一个 plot。 There's no interaction between factor(Year) and s(lon,lat) . factor(Year)s(lon,lat)之间没有相互作用。

Do yourself a favour and either convert Year to a factor in the data or create a new variable fYear that is factor(Year) .帮自己一个忙,或者将Year转换为数据中的一个因子,或者创建一个新变量fYear ,即factor(Year) I'm going to assume you did the former in the below我假设你在下面做了前者

If you want an interaction you need to use something like this Year + s(lon, lat, by = Year) in your model.如果你想要一个交互,你需要在你的 model 中使用像 this Year + s(lon, lat, by = Year)这样的东西。

Then you'll get nlevels(factor(Year)) spatial smooths when you plot() your GAM.然后,当您plot() GAM 时,您将获得nlevels(factor(Year))空间平滑。

But even then those aren't predictions ;但即便如此,这些也不是预测 you'll notice that the scale is centred about 0 (the grand mean of the data in the reference year in this case).您会注意到刻度以 0 为中心(在本例中为参考年数据的总平均值)。 To get plots of predictions you either need to use predict() with newdata set to a data frame containing all the combinations of the predictors you want to predict for/at, or use vis.gam() .要获得预测图,您需要使用predict()并将newdata设置为包含要预测的预测变量的所有组合的数据框,或者使用vis.gam() For the latter you'll want:对于后者,您需要:

layout(matrix(c(1:3), nrow = 1))
vis.gam(mod2, view = c("lon", "lat"), cond = list(Year = "2012"),
        plot.type = "contour")
vis.gam(mod2, view = c("lon", "lat"), cond = list(Year = "2013"), 
        plot.type = "contour")
vis.gam(mod2, view = c("lon", "lat"), cond = list(Year = "2014"), 
        plot.type = "contour")
layout(1)

anything that you don't include in the list passed to cond will use a value that is either the observation closest to the median of the each respective continuous covariate or the modal level for factor variables.您未包含在传递给cond的列表中的任何内容都将使用一个值,该值是最接近每个相应连续协变量的中位数的观察值或因子变量的模态水平。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM