【问题标题】:Visualizing categorial data in a logistic regression plot在逻辑回归图中可视化分类数据
【发布时间】:2013-08-27 15:30:14
【问题描述】:

我正在尝试基于二进制数据作为因变量(直接离开 = 0 或 1)创建逻辑回归图。自变量是连续数据(危险提示的持续时间)、计数数据(危险提示出现的时间)和分类数据(处理:蔗糖或章鱼胺):

AnimalID       Time     Duration     Treatment       Daytime     DirectLeave
       1     1039.6          1.1       sucrose      mornings               1
       2     1116.5          7.6            OA      mornings               0
       3      359.9          2.4       sucrose     afternoon               0
       4      594.2         27.3            OA     afternoon               1
       5      951.4         10.5            OA      mornings               1
       6      612.4          3.8       sucrose     afternoon               0

到目前为止,我能够为整个数据集创建两个带有一条拟合线的图表(下图):

library(car)
data_animal <- read.table("DirLeave_DurSorted.txt",header=T)

# Plot for relationship between immediate leave of animal and the time of danger cue presentation

pufftimegraph<-glm(DirLea ~ Time , family=binomial(link=logit), data=data_animal)
summary(pufftimegraph)
Anova(pufftimegraph)
data_animal$fitted<-pufftimegraph$fitted

dummy<-(data_animal$Time)
dummy<-sort(dummy)
print(dummy)

plot(data_animal$DirLea~data_animal$Time, xlab ="Time of the presentation of the danger cue", ylab="Proportion of wasps leaving the patch")
lines(data_animal$Time,(1/(1+(1/exp(0.0011188*data_Maxi$Time+-0.0174130)))), col="black")

# Plot for relationship between immediate leave of animal and duration of danger cue

durgraph<-glm(DirLea ~ Dur , family=binomial(link=logit), data=data_animal)
summary(durgraph)
Anova(durgraph)
data_animal$fitteddur<-durgraph$fitted
print(data_animal$fitteddur)

plot(data_animal$DirLea~data_animal$Dur, xlab ="Duration of the danger cue [s]", ylab="Proportion of wasps leaving the patch")
lines(data_animal$Dur,(1/(1+(1/exp(0.15020*data_animal$Dur+-1.00618)))), col="black")

但是,我研究的目的是展示两种治疗方法之间的差异。我知道我需要两个类别的斜率和截距值,即蔗糖和章鱼胺,但 Anova() 只为整个数据集提供一个值。 所以,我想用两条拟合线创建两个图表:每个处理一个。是否有可能做到这一点,如果可以,怎么做?

【问题讨论】:

    标签: r plot regression categorical-data


    【解决方案1】:

    您在此处的模型不依赖于治疗类型,因此无论如何您都不会从同一模型中得到不同的曲线。要在治疗之间进行比较,您必须在模型中包含依赖于治疗的术语,或者将模型拟合到您的数据子集。

    将模型 DirLea ~ Time 与模型 DirLea ~ Dur 进行比较也很困难,因为它们没有嵌套。这两个模型可能捕捉到不同的效果或相同的效果,具体取决于您的实验设计以及两个变量是否具有任何相关性

    假设您从一个模型开始,其中包含您要比较的所有内容:

    model <- glm(DirLea ~ (Time + Dur)*treatment, 
                 data=data_animal, family = binomial(link=logit))
    

    您可以通过将人工数据输入predict 来构建任意拟合线,并使用subset 提取数据的相关子集。在这里,将蔗糖处理的数据和固定为某个值的危险提示绘制成图表:

    sucrose.line <- expand.grid(treatment = "sucrose", 
                                Time = seq(100, 1200, length=50)
                                Dur=mean(animal_data$Dur))
    sucrose.line$fitted <- predict(model, sucrose.line)
    lines(fitted ~ Time, data = sucrose.line,
          xlab ="Time of the presentation of the danger cue",    
          ylab="Proportion of wasps leaving the patch")
    

    【讨论】:

    • 我真的需要将时间和持续时间都包含在我的模型中以构建任意拟合线吗?无论如何,拟合线不会出现在情节中,我想知道为什么,因为我使用了此处发布的确切线。
    • 因为上面发布的代码不起作用(拟合线没有出现在图中),我使用了ggplot(data_animal, aes(x = Time, y = DirLea, shape = Treat)) + geom_point() + geom_smooth(method = "glm"),结果是in this graph。即使不是线性回归也可以使用吗?
    • 我无法检查上述方法是否有效,因为您没有足够的数据集供我实际运行代码。至于第一个问题,我不清楚您对拟合线有什么统计或分析目的。
    • 我再次测试了上面的代码,终于成功了!但是,行结束并开始outside the graph,这很奇怪。人工数据是否也考虑到我在这里处理的是生物数据?我能够告诉 ggplot 它应该根据数据的适当误差分布来绘制它:+ geom_smooth(method = "glm", family="binomial", se=FALSE) 顺便说一句,this is the whole data set
    • 我需要拟合线的原因是因为我想可视化治疗对动物行为的影响:尽管我提出了事实,但它是离开还是留在补丁上某个时间的危险提示?我的假设是,与只喂食蔗糖溶液的对照组相比,当喂食章鱼胺时,动物会在贴片上停留更长时间。说实话,我的分析结果是,治疗并没有操纵黄蜂在斑块上停留更长时间或更早离开的意愿。
    猜你喜欢
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 2021-09-28
    • 2019-02-15
    • 2012-11-19
    • 2016-02-01
    相关资源
    最近更新 更多