我正在尝试为R中的二元逻辑回归模型开发一个较低和较高的95%CI用于某些生物数据。响应是基于激素值的怀孕状态,因此个体怀孕(1)或不怀孕(0)。然后,我预测模型中的一系列未知值,以获得个体怀孕的概率。我需要为模型获得/开发95%的上限和下限CI包络并绘制它。我已经能够在Matlab中做到这一点,但无法让它在R中工作,使用bootstrap函数从一个向量/值数组开发1000个重复项,然后我可以使用上面的.975和更低的.025来开发我的独联体国家。任何帮助和反馈都会很棒。非常感谢。
R代码:
model5 <-glm(Preg~logP4, data = controls, family=binomial(link="logit"))
summary(model5)
range(controls$logP4)
xlogP4 <- seq(-1, 3, 0.01)
ylogP4 <- predict(model5, list(logP4=xlogP4, type ="response"))
plot(controls$logP4, controls$Preg, pch =16, xlab ="Log10(Progesterone)", ylab ="Probability of being pregnant")
curve(predict(model5, data.frame(logP4=x), type="resp"), add=TRUE)
Matlab代码:
data = GoMControlsFinal;
x = data(1:29,4)%logP4 value
X = table2array(x)
y = data(1:29,6)% pregnant binary response
Y = table2array(y)
x1 = (-1:0.001:3)'
[b,dev,stats] = glmfit(X,Y,'binomial', 'logit') % linear regression analysis
yfit = glmval(b, x1, 'logit') % linear regression analysis
%% not yet giving me a P of 0 to 1 for pregnancy, still working as linear model
for i=1:10000 %number of replicates
b2 = bootstrp(1,@glmBfit,X,Y); %generates bootstrap error envelop
yfitBoot(:,i) = glmval(b2', x1, 'logit');
%plot (x1, yfitBoot(:,i), '-','LineWidth',1)
end
s =sort(yfitBoot');
s_lo = s(250,:) %number of replicates * 0.025
s_hi = s(9750,:)%number of replicates * 0.975
s_lo3 = s_lo'
s_hi3 = s_hi'
figure
z1= plot(x1, s_lo, 'b:', 'linewidth',2) % CI low line
hold on
z2 = plot(x1, s_hi, 'b:', 'linewidth',2) %ci hi line
z3= plot (x1, yfit, 'k-', 'LineWidth',2) % Model line
z4=scatter(X,Y, 'r', 'filled')
legend([z1, z3, z4], {'95% CI','Logistic Model', 'GoM Control Samples'})
xlabel('Log10 progesterone concentration')
ylabel('Probability of being pregnant')