2017-02-07 10 views
0

숫자 변수에서 상호 작용을 사용하여 이항 glm을 플로팅 할 수 있는지 알고 싶습니다. 내 경우 : behv과 조건이 모델숫자 변수에서 상호 작용을 사용하여 이항 glm 플로팅

#Plotting first for behv 
x<-d$behv ###Take behv values 
x2<-rep(mean(d$condition),length(d_p[,1])) ##Fixed mean condition 

# Points 
plot(d$mating~d$behv) 

#Curve 
curve(exp(model$coefficients[1]+model$coefficients[2]*x+model$coefficients[3]*x2) 
/(1+exp(model$coefficients[1]+model$coefficients[2]*x+model$coefficients[3]*x2))) 

에 중요하지만, 작동하지 않는 상황에서

##Data set artificial 
set.seed(20) 
d <- data.frame(
    mating=sample(0:1, 200, replace=T), 
    behv = scale(rpois(200,10)), 
    condition = scale(rnorm(200,5)) 
) 

#Binomial GLM ajusted 
model<-glm(mating ~ behv + condition, data=d, family=binomial) 
summary(model) 

! 다른 올바른 접근법이 있습니까? 그것은 당신의 원하는 출력처럼 보인다

감사

답변

1

조건부 수단 (또는 최적 라인)의 플롯이다. predict 기능을 사용하여 예측값을 계산하여이 작업을 수행 할 수 있습니다.

더 나은 결과를 얻으려면 예제를 조금 바꿀 것입니다. 이제

d$mating <- ifelse(d$behv > 0, rbinom(200, 1, .8), rbinom(200, 1, .2)) 
model <- glm(mating ~ behv + condition, data = d, family = binomial) 
summary(model) 

, 우리가 원하는 값으로 newdata dataframe합니다하지 않을 경우

newdata <- d 
newdata$condition <- mean(newdata$condition) 
newdata$yhat <- predict(model, newdata, type = "response") 

마지막으로, x 축 변수에 의해 우리 일종의 newdata가 (우리가 지그재그 라인을 얻을 것이다 모든) 플롯을 통해, 다음 줄거리 :

newdata <- newdata[order(newdata$behv), ] 
plot(newdata$mating ~ newdata$behv) 
lines(x = newdata$behv, y = newdata$yhat) 

출력 :

enter image description here