如何在几年前拟合的logistic回归中使用R中的预测函数? [英] How can I use the predict function in R in a logistic regression fitted years ago?

查看:144
本文介绍了如何在几年前拟合的logistic回归中使用R中的预测函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个无法解决的问题,但没有成功.经过两天多的搜索,我一无所获.很抱歉,答案在那里,但我没有找到.

I have a problem that I am trying to resolve with no success. More than two days searching and I didn’t get a single clue. Sorry if the answer is out there and I didn’t find it.

假设您有几年前估计的旧模型的逻辑方程回归(二进制模型).因此,您知道参数βk(k = 1,2,...,p),因为它们是过去估计的.但是您没有用于拟合模型的数据.

Suppose that you have a logistic equation regression (binary model) from an old model that you estimated some years ago. Therefore you know the parameters βk (k = 1, 2, ..., p) because they were estimated in the past. But you don’t have the data that were used to fit the model.

我的问题是:我可以在R中引入这个旧的估计逻辑模型作为对象(对应于逻辑回归模型)吗?

My question is: can I introduce this old estimated logistic model in R as an object (corresponding to a logistic regression model)?

我想使用预测"功能通过一组新数据(当前数据)证明这种逻辑回归,然后检查经受时间考验的旧模型的有效性.要使用此功能,您需要逻辑回归模型的对象.

I would like to use the "predict" function to prove this logistic regression with a new set of data (present data) and then check the validity of this old model standing the test of time. And to use this function you need the object of the logistic regression model.

非常感谢您.

推荐答案

根据我的评论,我认为您可以直接从系数中直接计算出预测.这是一个将predict.glm的输出与直接在数据上计算出的预测概率进行比较的示例:

Per my comment, I think you could start by just calculating predictions directly from the coefficients. Here's an example that compares the output from predict.glm to predicted probabilities calculated directly on the data:

# construct some data and model it
# y ~ x1 + x2
set.seed(1)
x1 <- runif(100)
x2 <- runif(100)
y <- rbinom(100,1,(x1+x2)/2)
data1 <- data.frame(x1=x1,x2=x2,y=y)
x3 <- runif(100)
x4 <- runif(100)
y2 <- rbinom(100,1,(x3+x4)/2)
data2 <- data.frame(x1=x3,x2=x4,y=y2)
glm1 <- glm(y~x1+x2,data=data1,family=binomial)

# extract coefs
#summary(glm1)
coef1 <- coef(glm1)

# calculate predicted probabilities for current data
tmp1 <- coef1[1] + (data1$x1*coef1[2]) + (data1$x2*coef1[3])
pr1 <- 1/(1+(1/exp(tmp1)))
# these match those from `predict`:
all.equal(pr1,predict(glm1,data1,type='response'))

# now apply to new data:
tmp2 <- coef1[1] + (data2$x1*coef1[2]) + (data2$x2*coef1[3])
pr2 <- 1/(1+(1/exp(tmp2)))
pr2

这显然不是通用解决方案,也不能正确处理不确定性,但是我认为这是比黑客入侵predict更好的方法.

This is obviously not a general solution, nor does it properly handle uncertainty, but I think it's a better approach than hacking predict.

这篇关于如何在几年前拟合的logistic回归中使用R中的预测函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆