2013-08-24 24 views
1

我想創建基於二進制數據作爲因變量(直接離開= 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") 

enter image description hereenter image description here

但是,我研究的目的是爲了顯示這兩種治療方法之間的差異。我知道我需要兩個類別的斜率和截距值,即蔗糖和章魚胺,但Anova()僅爲整個數據集提供一個值。 因此,我想創建兩個適合線條的圖形:每個處理一個。有沒有可能做到這一點,如果是的話如何?

回答

0

這裏的模型不依賴於治療的類型,所以無論如何你都不會得到不同的曲線。爲了在治療之間進行比較,您必須將依賴治療的術語包含在模型中,或者將模型適用於數據的子集。

將模型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") 
+0

我是否真的需要在我的模型中包含時間和持續時間以構建任意擬合線?無論如何,合適的線條不會出現在劇情中,我不知道爲什麼,因爲我使用了這裏發佈的確切線條。 – Maxi

+0

由於上面貼出的代碼不起作用(擬合線不出現在圖中),我使用了'ggplot(data_animal,aes(x = Time,y = DirLea,shape = Treat))+ geom_point()+ geom_smooth (method =「glm」)'導致[在此圖中](https://dl.dropboxusercontent.com/u/38035375/ggplot_Treatment.jpg)。即使它不是線性迴歸,也可以使用它嗎? – Maxi

+0

我無法檢查上述工作是否因爲您沒有足夠的數據集供我實際運行代碼。至於第一個問題,我不清楚你的合適線路有什麼統計或分析目的。 – crowding