- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想计算预测值和标准误差,但我不能简单地使用 predict(),因为我使用了 15 个乘法估算数据集(Amelia 包生成)。我在每个数据集上运行回归模型。之后,使用 Amelia 函数 mi.meld() 将结果合并为一组模型系数和标准误差,该函数使用鲁宾规则。
示例数据和代码:
dd<-list()
for (i in 1:15){
dd[[i]] <- data.frame(
Age=runif(50,20,90),
Cat=factor(sample(0:4, 50, replace=T)),
Outcome = sample(0:1, 50, replace=T)
)}
b.out<-NULL
se.out<-NULL
for(i in 1:15) {
ols.out<-glm(Outcome~Age+factor(Cat), data=dd[[i]],family="binomial")
b.out <- rbind(b.out, ols.out$coef)
se.out <- rbind(se.out, coef(summary(ols.out))[,2])}
mod0 <- mi.meld(q = b.out, se = se.out)
> mod0
$q.mi
(Intercept) Age factor(Cat)1 factor(Cat)2 factor(Cat)3 factor(Cat)4
[1,] 0.0466825 -0.00577106 0.5291908 -0.09760264 0.4058684 0.3125109
$se.mi
(Intercept) Age factor(Cat)1 factor(Cat)2 factor(Cat)3
factor(Cat)4
[1,] 1.863276 0.02596468 1.604759 1.398322 1.414589
1.332743
现在是有问题的部分。我想计算以下预测变量值集的预测值(在本例中为预测概率)和标准误差:
data.predict <- data.frame(Cat=as.factor(c(0:4)), Age=53.6)
print(data.predict)
Cat Age
1 0 53.6
2 1 53.6
3 2 53.6
4 3 53.6
5 4 53.6
如果我在 1 个数据集上拟合了一个模型,我会简单地这样做:
prediction<- predict(mod1, data.predict, type="response",se.fit=T)
但是,我没有模型对象,我只存储了系数。现在我研究了两种解决这个问题的方法,第一种是以这种方式手动计算预测:predict() with arbitrary coefficients in r但是我不知道如何得到标准错误。我的另一个想法是创建一个假模型对象,就像这个函数创建的:https://gist.github.com/MrFlick/ae299d8f3760f02de6bf并在 predict() 中使用它,但由于未使用模型的标准误差,因此也无法计算预测的标准误差。
有人对如何解决这个问题有建议吗?我已尝试通过示例代码清楚地解释我的问题,但如果我的问题不清楚,请告诉我,以便我提供更多信息。感谢您的帮助!
最佳答案
我敢肯定,近几年后您不需要这个答案,但我一直在研究类似的问题,我想我会把答案放在这里以供后代使用。
Andrew Heiss 将此解决方案放在 gisthub 上 - https://gist.github.com/andrewheiss/a3134085e92c6607db39c5b14e1b879e
我对它做了一点修改(部分原因是我认为自从他写这篇文章以来,'nest' 的默认行为可能已经在 tidyverse 中改变了?)
代码(艰苦的工作!)几乎完全来自 Andrew Heiss。这里的注释是我和他的混合。
这是使用 Amelia Africa 数据集,对于我的现实生活问题,我有一个不同的数据集(显然)并且前几个步骤有点不同,但一切都很好。
library(tidyverse)
library(Amelia)
library(broom)
# Use the africa dataset from Amelia
data(africa)
set.seed(1234)
imp_amelia <- amelia(x = africa, m = 5, cs = "country", ts = "year", logs = "gdp_pc", p2s = 0) # do the imputations -- for me, it was fine to do this bit in 'mice'
# Gather all the imputed datasets into one data frame and run a model on each
models_imputed_df <- bind_rows(unclass(imp_amelia$imputations), .id = "m") %>%
group_by(m) %>%
nest() %>%
mutate(model = data %>% map(~ lm(gdp_pc ~ trade + civlib, data = .)))
# again - for my real life problem the models looked very different to this, and used rms - and this was also totally fine.
models_imputed_df
#> # A tibble: 5 x 3
#> m data model
#> <chr> <list> <list>
#> 1 imp1 <tibble [120 × 7]> <S3: lm>
#> 2 imp2 <tibble [120 × 7]> <S3: lm>
#> 3 imp3 <tibble [120 × 7]> <S3: lm>
#> 4 imp4 <tibble [120 × 7]> <S3: lm>
#> 5 imp5 <tibble [120 × 7]> <S3: lm>
# We want to see how GDP per capita varies with changes in civil liberties, so
# we create a new data frame with values for each of the covariates in the
# model. We include the full range of civil liberties (from 0 to 1) and the mean
# of trade.
# ie. this is a 'skelton' data frame of all your variables that you want to make predictions over.
new_data <- data_frame(civlib = seq(0, 1, 0.1),
trade = mean(africa$trade, na.rm = TRUE))
new_data
#> # A tibble: 11 x 2
#> civlib trade
#> <dbl> <dbl>
#> 1 0. 62.6
#> 2 0.100 62.6
#> 3 0.200 62.6
#> 4 0.300 62.6
#> 5 0.400 62.6
#> 6 0.500 62.6
#> 7 0.600 62.6
#> 8 0.700 62.6
#> 9 0.800 62.6
#> 10 0.900 62.6
#> 11 1.00 62.6
# write a function to meld predictions
meld_predictions <- function(x) {
# x is a data frame with m rows and two columns:
#
# m .fitted .se.fit
# 1 1.05 0.34
# 2 1.09 0.28
# x ... ...
# Meld the fitted values using Rubin's rules
x_melded <- mi.meld(matrix(x$.fitted), matrix(x$.se.fit))
data_frame(.fitted = as.numeric(x_melded$q.mi),
.se.fit = as.numeric(x_melded$se.mi))
}
# We augment/predict using new_data in each of the imputed models, then we group
# by each of the values of civil liberties (so each value, like 0.1 and 0.2 has
# 5 values, 1 from each of the imputed models), and then we meld those 5
# predicted values into a single value with meld_predictions()
predict_melded <- data_frame(models = models_imputed_df$model) %>%
mutate(m = 1:n(),
fitted = models %>% map(~ augment(., newdata = new_data))) %>%
unnest(fitted) %>%
dplyr::select(-models) %>% #### I needed to add this row to make the code work, once you've used the models to get the fit you don't need them in the data object anymore. I took this line out because it was slowing everything down, then realised the code only works with this line... not sure why?
group_by(civlib) %>%
nest(data=c(m, .fitted, .se.fit)) %>% # needed to change this here from gisthub to make the nested 'data' have all the imputations in it, not just estimates from one of the imputations.
mutate(fitted_melded = data %>% map(~ meld_predictions(.))) %>%
unnest(fitted_melded) %>%
mutate(ymin = .fitted + (qnorm(0.025) * .se.fit),
ymax = .fitted + (qnorm(0.975) * .se.fit))
## NB. this is still on the link scale -- you'd need to write an extra few lines to exponentiate everything and get your predictions and se's on the response scale
# Plot!
ggplot(predict_melded, aes(x = civlib, y = .fitted)) +
geom_line(color = "blue") +
geom_ribbon(aes(ymin = ymin, ymax = ymax), alpha = 0.2, fill = "blue")
希望这至少对其他遇到此问题的人有所帮助。
关于r - 在 R 中进行多重插补后计算预测均值(或预测概率)和 SE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45082279/
我正在从 Stata 迁移到 R(plm 包),以便进行面板模型计量经济学。在 Stata 中,面板模型(例如随机效应)通常报告组内、组间和整体 R 平方。 I have found plm 随机效应
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 6年前关闭。 Improve this qu
我想要求用户输入整数值列表。用户可以输入单个值或一组多个值,如 1 2 3(spcae 或逗号分隔)然后使用输入的数据进行进一步计算。 我正在使用下面的代码 EXP <- as.integer(rea
当 R 使用分类变量执行回归时,它实际上是虚拟编码。也就是说,省略了一个级别作为基础或引用,并且回归公式包括所有其他级别的虚拟变量。但是,R 选择了哪一个作为引用,以及我如何影响这个选择? 具有四个级
这个问题基本上是我之前问过的问题的延伸:How to only print (adjusted) R-squared of regression model? 我想建立一个线性回归模型来预测具有 15
我在一台安装了多个软件包的 Linux 计算机上安装了 R。现在我正在另一台 Linux 计算机上设置 R。从他们的存储库安装 R 很容易,但我将不得不使用 安装许多包 install.package
我正在阅读 Hadley 的高级 R 编程,当它讨论字符的内存大小时,它说: R has a global string pool. This means that each unique strin
我们可以将 Shiny 代码写在两个单独的文件中,"ui.R"和 "server.R" , 或者我们可以将两个模块写入一个文件 "app.R"并调用函数shinyApp() 这两种方法中的任何一种在性
我正在使用 R 通过 RGP 包进行遗传编程。环境创造了解决问题的功能。我想将这些函数保存在它们自己的 .R 源文件中。我这辈子都想不通怎么办。我尝试过的一种方法是: bf_str = print(b
假设我创建了一个函数“function.r”,在编辑该函数后我必须通过 source('function.r') 重新加载到我的全局环境中。无论如何,每次我进行编辑时,我是否可以避免将其重新加载到我的
例如,test.R 是一个单行文件: $ cat test.R # print('Hello, world!') 我们可以通过Rscript test.R 或R CMD BATCH test.R 来
我知道我可以使用 Rmd 来构建包插图,但想知道是否可以更具体地使用 R Notebooks 来制作包插图。如果是这样,我需要将 R Notebooks 编写为包小插图有什么不同吗?我正在使用最新版本
我正在考虑使用 R 包的共享库进行 R 的站点安装。 多台计算机将访问该库,以便每个人共享相同的设置。 问题是我注意到有时您无法更新包,因为另一个 R 实例正在锁定库。我不能要求每个人都关闭它的 R
我知道如何从命令行启动 R 并执行表达式(例如, R -e 'print("hello")' )或从文件中获取输入(例如, R -f filename.r )。但是,在这两种情况下,R 都会运行文件中
我正在尝试使我当前的项目可重现,因此我正在创建一个主文档(最终是一个 .rmd 文件),用于调用和执行其他几个文档。这样我自己和其他调查员只需要打开和运行一个文件。 当前设置分为三层:主文件、2 个读
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?将问题更新为 on-topic对于堆栈溢出。 5年前关闭。 Improve this qu
我的 R 包中有以下描述文件 Package: blah Title: What the Package Does (one line, title case) Version: 0.0.0.9000
有没有办法更有效地编写以下语句?accel 是一个数据框。 accel[[2]]<- accel[[2]]-weighted.mean(accel[[2]]) accel[[3]]<- accel[[
例如,在尝试安装 R 包时 curl作为 usethis 的依赖项: * installing *source* package ‘curl’ ... ** package ‘curl’ succes
我想将一些软件作为一个包共享,但我的一些脚本似乎并不能很自然地作为函数运行。例如,考虑以下代码块,其中“raw.df”是一个包含离散和连续类型变量的数据框。函数“count.unique”和“squa
我是一名优秀的程序员,十分优秀!