【问题标题】:How to retrieve the data frame used in a GEE model fit?如何检索 GEE 模型拟合中使用的数据框?
【发布时间】:2019-12-23 12:31:31
【问题描述】:

我有一个纵向数据框,每个 id 有多行。

    > data("dietox")
    > head(dietox, 5)
   Pig    Evit    Cu Litter Start   Weight      Feed Time
1 4601 Evit000 Cu000      1  26.5 26.50000        NA    1
2 4601 Evit000 Cu000      1  26.5 27.59999  5.200005    2
3 4601 Evit000 Cu000      1  26.5 36.50000 17.600000    3
4 4601 Evit000 Cu000      1  26.5 40.29999 28.500000    4
5 4601 Evit000 Cu000      1  26.5 49.09998 45.200001    5

我正在尝试拟合 GEE 模型来预测数据框每一行的 Weight

    library(gee)
    library(dplyr)

    > model1 <- gee(Weight ~ Start + Feed, id=Pig, data=dietox, corstr="exchangeable")
    > model1

 GEE:  GENERALIZED LINEAR MODELS FOR DEPENDENT DATA
 gee S-function, version 4.13 modified 98/01/27 (1998) 

Model:
 Link:                      Identity 
 Variance to Mean Relation: Gaussian 
 Correlation Structure:     Exchangeable 

Call:
gee(formula = Weight ~ Start + Feed, id = Pig, data = dietox, 
    corstr = "exchangeable")

Number of observations :  789 

Maximum cluster size   :  11 


Coefficients:
(Intercept)       Start        Feed 
  5.1539561   0.9384232   0.4294209 

我现在希望能够向数据框添加一个新列-prediction,其中包含每行数据的预测权重值。这个想法是,我将能够在Time 变量的不同点将原始Weight 变量与prediction 变量进行比较。

当我尝试使用 mutatepredict 函数执行此操作时,我收到一条错误消息,指出模型拟合中使用的观测数 (789) 与原始数据框中的观测数不同 ( 861)。

> new_df <- dietox %>%
+   mutate(prediction = predict(model1))
Error: Column `prediction` must be length 861 (the number of rows) or one, not 789

我的问题是: 1. 如何提取 789 个观测值的数据框 被用于模型拟合? 2.为什么是观察次数 模型中使用的拟合与观察总数不同 在原始数据框中?

【问题讨论】:

    标签: r panel-data


    【解决方案1】:

    模型拟合中使用的 789 个观测值是没有NA 的观测值。 NAFeed 列中有 72 个观察结果

    sum(is.na(dietox$Feed))
    #[1] 72
    

    789 + 72 为您提供完整的 861 观察。要获得您可以做的所有预测值

    dietox$Prediction <- NA
    dietox$Prediction[!is.na(dietox$Feed)] <- predict(model1)
    
    head(dietox)
    #    Weight      Feed Time  Pig Evit Cu Litter Prediction
    #1 26.50000        NA    1 4601    1  1      1         NA
    #2 27.59999  5.200005    2 4601    1  1      1   31.43603
    #3 36.50000 17.600000    3 4601    1  1      1   36.76708
    #4 40.29999 28.500000    4 4601    1  1      1   41.45324
    #5 49.09998 45.200001    5 4601    1  1      1   48.63296
    #6 55.39999 56.900002    6 4601    1  1      1   53.66306
    

    模型中使用的值也存在于model1$y中。

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 2021-10-18
      • 2020-10-26
      • 2013-08-24
      • 1970-01-01
      • 2017-12-20
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多