【发布时间】: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 变量进行比较。
当我尝试使用 mutate 和 predict 函数执行此操作时,我收到一条错误消息,指出模型拟合中使用的观测数 (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