【问题标题】:How to determine the probability that the predicted value reaches a certain value in R如何确定预测值在R中达到某个值的概率
【发布时间】:2022-01-14 08:20:57
【问题描述】:

根据 2003 年到 2018 年的历史数据,我使用应用贝叶斯计量经济学方法预测了 2019 年到 2030 年的数据。现在我要判断2030年的预测数据达到0.8的概率。我想知道R中使用了什么方法。谢谢!

【问题讨论】:

  • 从 15 年的数据预测未来 11 年是……愚蠢的。
  • 这更像是一个方法问题而不是编程问题。 (至少在你分享一些代码之前。)在 stats.stackexchange 上可能比在 Stack Overflow 上更自在。
  • 同意,并且还认为这不是关于 R 中的特定方法,而是更多关于一般的具体方法它们的实现,所以投票转向 stats.stackexchange .
  • 如果你已经拟合了贝叶斯模型,这应该是一个非常简单的后验分布模拟。

标签: r probability


【解决方案1】:

请注意,为了执行您想要的操作,您可以使用 predict 函数为数据集中的每个观察提取线性预测。然后,您可以简单地使用适当的概率分布函数来获得预测概率。例如,在逻辑回归的情况下,使用 plogis。换句话说,如果 mod 是你的模型适合 glm:

plogis(predict(mod))

将返回数据集中每个观察的预测概率,假设您估计了一个逻辑模型。如果您需要计算不在数据集中的点的预测概率,请参阅预测的新数据选项。请注意,预测还可以提供每个点的标准误差。查看 predict.glm 的文档以获取更多信息(使用 help(predict)):

predict(mod, type="response")

改编自杰森摩根:predict probability using R

此外,对于逻辑回归,函数用法如下:

predict(object, newdata = NULL,
            type = c("link", "response", "terms"),
            se.fit = FALSE, dispersion = NULL, terms = NULL,
            na.action = na.pass, ...)

您可以尝试将可用的 R 实现为:

predict(budworm.lg, data.frame(ldose = ld,
   sex = factor(rep("M", length(ld)), levels = levels(sex))),
   type = "response")

对于数据库Venables 和 Ripley (2002, pp. 190-2.)。具体可以使用如下代码:

require(graphics)

## example from Venables and Ripley (2002, pp. 190-2.)
ldose <- rep(0:5, 2)
numdead <- c(1, 4, 9, 13, 18, 20, 0, 2, 6, 10, 12, 16)
sex <- factor(rep(c("M", "F"), c(6, 6)))
SF <- cbind(numdead, numalive = 20-numdead)
budworm.lg
budworm.lg <- glm(SF ~ sex*ldose, family = binomial)
summary(budworm.lg)

plot(c(1,32), c(0,1), type = "n", xlab = "dose",
     ylab = "prob", log = "x")
text(2^ldose, numdead/20, as.character(sex))
ld <- seq(0, 5, 0.1)
lines(2^ld, predict(budworm.lg, data.frame(ldose = ld,
                                           sex = factor(rep("M", length(ld)), levels = levels(sex))),
                    type = "response"))
lines(2^ld, predict(budworm.lg, data.frame(ldose = ld,
                                           sex = factor(rep("F", length(ld)), levels = levels(sex))),
                    type = "response"))

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 2017-01-07
    • 2020-04-05
    • 2015-02-09
    • 2018-10-29
    • 2022-08-19
    • 2021-07-31
    • 2019-09-03
    相关资源
    最近更新 更多