2017-02-07 7 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データフレームを作る:

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

最後に、ソートnewdata x軸変数によって(ない場合は、我々はジグザグの線を取得しますすべてのプロットを超える)、そしてプロット:

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

出力:

enter image description here

関連する問題