2017-06-04 68 views
2

我的數據是二進制與兩個線性獨立變量。對於兩個預測指標,隨着它們變大,還有更多積極的反應。我將這些數據繪製在一張熱點圖上,顯示了兩個變量的積極響應密度。右上角有最積極的反應,左下角有負面反應,沿兩個軸線可以看到梯度變化。陰謀logistic迴歸線在熱圖

我想在熱點上畫出一條線,顯示邏輯迴歸模型預測正面和負面反應的可能性相同。 (我的模型的形式爲response~predictor1*predictor2+(1|participant)。)

我的問題:如何根據此模型計算出積極響應率爲0.5的線?我嘗試使用predict(),但它的工作原理與此相反;我必須給它的價值觀,而不是給出我想要的迴應率。當我只有一個預測變量(function(x) (log(x/(1-x))-fixef(fit)[1])/fixef(fit)[2])時,我也嘗試過使用以前使用的函數,但我只能得到單個值,而不是一行,並且一次只能獲取一個預測變量的值。

+1

有趣的問題!例如,如果您提供示例數據,人們可以更輕鬆地獲得幫助。一個類似的邏輯迴歸模型適用於R中的示例數據集之一以及您的圖的代碼。我認爲你可以通過找到擬合線性預測變量(在對數賠率標度上)爲0的點來找到邊界線,所以你可以使用一些非常基本的代數來找出「predictor2」的值的方程,該方程將滿足給予'predictor1'的一些價值。 – Marius

+0

事實上,我剛剛發現有人在這裏寫下代數:https://stats.stackexchange.com/a/159977/5443 – Marius

回答

2

使用一個簡單的例子logistic迴歸模型擬合到mtcars數據集和代數描述here,我可以使用產生熱圖與決策邊界:

library(ggplot2) 
library(tidyverse) 
data("mtcars") 

m1 = glm(am ~ hp + wt, data = mtcars, family = binomial) 

# Generate combinations of hp and wt across their observed range. Only 
# generating 50 values of each here, which is not a lot but since each 
# combination is included, you get 50 x 50 rows 
pred_df = expand.grid(
    hp = seq(min(mtcars$hp), max(mtcars$hp), length.out = 50), 
    wt = seq(min(mtcars$wt), max(mtcars$wt), length.out = 50) 
) 
pred_df$pred_p = predict(m1, pred_df, type = "response") 


# For a given value of hp (predictor1), find the value of 
# wt (predictor2) that will give predicted p = 0.5 
find_boundary = function(hp_val, coefs) { 
    beta_0 = coefs['(Intercept)'] 
    beta_1 = coefs['hp'] 
    beta_2 = coefs['wt'] 
    boundary_wt = (-beta_0 - beta_1 * hp_val)/beta_2 
} 


# Find the boundary value of wt for each of the 50 values of hp 
# Using the algebra in the linked question you can instead find 
# the slope and intercept of the boundary, so you could potentially 
# skip this step 
boundary_df = pred_df %>% 
    select(hp) %>% 
    distinct %>% 
    mutate(wt = find_boundary(hp, coef(m1))) 


ggplot(pred_df, aes(x = hp, y = wt)) + 
    geom_tile(aes(fill = pred_p)) + 
    geom_line(data = boundary_df) 

生產:

enter image description here

請注意,這隻考慮了模型的固定效應,所以如果您想以某種方式考慮隨機效應,這可能會更復雜。