我有逻辑回归,我想从logit曲线生成模拟数据。我的代码如下:
#Begin Code
require(gld)
runs<-100
num.trees<-500
p<-0.5
trial.1<-rgl(num.trees,1859.75592, 0.02179, -0.09578, 0.24264, param = "fkml", lambda5 = NULL)
trial.1 <- floor(trial.1/10)*10+1
minDecade <- min(trial.1)
maxDecade <- max(trial.1)
allDecades <- seq(minDecade-100, 2001, by=10)
x<-1:length(allDecades)
y<-sample(trial.1, p*num.trees)
binTrees <- rep(0,length(allDecades))
for (i in 1:length(allDecades)) {
binTrees[i] <- length(which(y==allDecades[i]))
}
binTrees
binTrees<-cumsum(binTrees)/sum(binTrees)
fit<-glm(binTrees~x,family=binomial(link='logit'))
plot(binTrees)
lines(fitted.values(fit))
#End Code
基本上,从最后一点开始,如何从逻辑回归中生成模拟数据? 我采访过的人建议使用CDF功能来做到这一点,但我不知道从哪里开始。我的目标是根据拟合曲线重建完整的数据集。
提前感谢任何建议!
答案 0 :(得分:0)
如果您使用逻辑回归进行模拟实验,通常会从二项分布中绘制:
> set.seed(123)
> df1 <- data.frame(event=rbinom(n=20, size=1, prob=.4) )
> glm(event ~ . , df1, family="binomial")
Call: glm(formula = event ~ ., family = "binomial", data = df1)
Coefficients:
(Intercept)
-0.4055
Degrees of Freedom: 19 Total (i.e. Null); 19 Residual
Null Deviance: 26.92
Residual Deviance: 26.92 AIC: 28.92
> exp(-0.4055)/(1+exp(-0.4055))
[1] 0.3999916
> sum(df1$event)/length(df1$event)
[1] 0.4 # that degree of agreement with the simulated parameter is accidental.
您的rgl
功能结果(基于未命名的包)与其余代码之间的联系似乎模糊不清,因此如果您用自然语言描述您希望模拟的内容会更好。< / p>