【问题标题】:Plot two curves in logistic regression in R在 R 中绘制逻辑回归中的两条曲线
【发布时间】:2012-02-13 10:12:38
【问题描述】:

我在 R (glm) 中运行逻辑回归。然后我设法绘制结果。我的代码如下:

 temperature.glm = glm(Response~Temperature, data=mydata,family=binomial)

 plot(mydata$Temperature,mydata$Response, ,xlab="Temperature",ylab="Probability of Response")
 curve(predict(temperature.glm,data.frame(Temperature=x),type="resp"),add=TRUE, col="red")
 points(mydata$Temperature,fitted(temperature.glm),pch=20)
 title(main="Response-Temperature with Fitted GLM Logistic Regression Line") 

我的问题是:

  1. 如何在一张图中绘制两条逻辑回归曲线?
  2. 我从其他统计软件中获得了这两个系数。如何创建随机数据,插入这两组 coef(Set 1 和 Set 2),然后生成两条逻辑回归曲线?

模型:

                   SET 1
 (Intercept)     -88.4505
 Temperature       2.9677

                  SET 2
 (Intercept)    -88.585533
 Temperature      2.972168

mydata 有 2 列和约 700 行。

Response Temperature 
1 29.33 
1 30.37 
1 29.52 
1 29.66 
1 29.57 
1 30.04 
1 30.58 
1 30.41 
1 29.61 
1 30.51 
1 30.91 
1 30.74 
1 29.91 
1 29.99 
1 29.99 
1 29.99 
1 29.99 
1 29.99 
1 29.99 
1 30.71 
0 29.56 
0 29.56 
0 29.56 
0 29.56 
0 29.56 
0 29.57 
0 29.51

【问题讨论】:

  • 你为什么不能只调用两次curve(或lines),并使用不同曲线的值?
  • 此外,如果您提供可重现的数据集,回答您的问题会容易得多。在这种情况下,我们无法访问 mydata,这让事情变得更加困难。
  • 最后,删除了您的签名。如果您想让人们知道您是 Eddie,请在您的个人资料中填写您的姓名。欢迎来到 SO,顺便说一句。
  • 如果您想并排绘制它们,请在您的plot 调用之前使用par(mfrow=c(1, 2))。否则,Richie 建议调用curve 两次应该适用于覆盖两条曲线。
  • 我不确定您是否要生成两个随机数据集,每个数据集都符合您的两个模型之一,但如果是这样,您可以尝试rbinom(1000, 1, (1/(1+exp(-88.4505 + 2.9677*x)))(对于第一个模型,类似对于第二个,x 是温度)。

标签: r regression


【解决方案1】:
  1. 要绘制曲线,您只需定义响应和预测变量之间的关系,并指定您希望绘制曲线的预测变量值的范围。例如:

    dat <- structure(list(Response = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
      1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L), Temperature = c(29.33, 30.37, 29.52, 29.66, 29.57, 30.04, 
      30.58, 30.41, 29.61, 30.51, 30.91, 30.74, 29.91, 29.99, 29.99, 
      29.99, 29.99, 29.99, 29.99, 30.71, 29.56, 29.56, 29.56, 29.56, 
      29.56, 29.57, 29.51)), .Names = c("Response", "Temperature"), 
      class = "data.frame", row.names = c(NA, -27L))
    
    temperature.glm <- glm(Response ~ Temperature, data=dat, family=binomial)
    
    plot(dat$Temperature, dat$Response, xlab="Temperature", 
         ylab="Probability of Response")
    curve(predict(temperature.glm, data.frame(Temperature=x), type="resp"), 
          add=TRUE, col="red")
    # To add an additional curve, e.g. that which corresponds to 'Set 1':
    curve(plogis(-88.4505 + 2.9677*x), min(dat$Temperature), 
          max(dat$Temperature), add=TRUE, lwd=2, lty=3)
    legend('bottomright', c('temp.glm', 'Set 1'), lty=c(1, 3), 
           col=2:1, lwd=1:2, bty='n', cex=0.8)
    

    在上面的第二个curve 调用中,我们说逻辑函数定义了xy 之间的关系。 plogis(z) 的结果等同于评估1/(1+exp(-z)) 时获得的结果。 min(dat$Temperature)max(dat$Temperature) 参数定义了x 的范围,y 应该被评估。我们不需要告诉函数x 指的是温度;当我们指定应针对该范围的预测值评估响应时,这是隐含的。

  2. 如您所见,curve 函数允许您绘制曲线,而无需模拟预测变量(例如温度)数据。如果您仍然需要这样做,例如要绘制符合特定模型的伯努利试验的一些模拟结果,您可以尝试以下操作:

    n <- 100 # size of random sample
    
    # generate random temperature data (n draws, uniform b/w 27 and 33)
    temp <- runif(n, 27, 33)
    
    # Define a function to perform a Bernoulli trial for each value of temp, 
    #   with probability of success for each trial determined by the logistic
    #   model with intercept = alpha and coef for temperature = beta.
    # The function also plots the outcomes of these Bernoulli trials against the 
    #   random temp data, and overlays the curve that corresponds to the model
    #   used to simulate the response data.
    sim.response <- function(alpha, beta) {
      y <- sapply(temp, function(x) rbinom(1, 1, plogis(alpha + beta*x)))  
      plot(y ~ temp, pch=20, xlab='Temperature', ylab='Response')
      curve(plogis(alpha + beta*x), min(temp), max(temp), add=TRUE, lwd=2)    
      return(y)
    }
    

    例子:

    # Simulate response data for your model 'Set 1'
    y <- sim.response(-88.4505, 2.9677)
    
    # Simulate response data for your model 'Set 2'
    y <- sim.response(-88.585533, 2.972168)
    
    # Simulate response data for your model temperature.glm
    # Here, coef(temperature.glm)[1] and coef(temperature.glm)[2] refer to
    #   the intercept and slope, respectively
    y <- sim.response(coef(temperature.glm)[1], coef(temperature.glm)[2])
    

    下图显示了上面第一个示例生成的图,即温度随机向量的每个值的单个伯努利试验的结果,以及描述模拟数据的模型的曲线。

【讨论】:

  • 这正是我想要的。你救了我! :)
猜你喜欢
  • 2016-08-09
  • 2015-02-19
  • 2018-10-13
  • 1970-01-01
  • 2014-04-22
  • 2014-03-16
  • 1970-01-01
  • 2013-08-29
  • 2021-11-23
相关资源
最近更新 更多