2016-08-17 79 views
2

我們正在處理一個包含兩個分類變量年齡組和性別的迴歸模型。R中兩個分類變量之間的相互作用

我們希望在兩個分類變量之間包含一個交互項,但所得到的模型僅顯示女性與所有年齡組之間的交互效果。

我們應該如何調整代碼,使「26-30歲」男性成爲參考水平,並顯示所有其他羣體在其輸出中的效果?

調整代碼

count_med_op3 <- glm(Count_OP ~ Gender * age_group + otherfactors, 
        data = med, family = 'poisson') 

結果通緝:

GenderMale:age_group"0-1" 
GenderMale:age_group"2-6" 
GenderMale:age_group"7-18" 
GenderMale:age_group"19-25" 
GenderMale:age_group"31-36" 
Genderfemale:age_group"0-1" 
Genderfemale:age_group"2-6" 
Genderfemale:age_group"7-18" 
Genderfemale:age_group"19-25" 
Genderfemale:age_group"26-30" 
other factors 

回答

2

使用relevel

# simulate some data 
df_foo = data_frame(
    age = as.factor(sample(seq(10, 90, 10), 100, replace = TRUE)), 
    y = rnorm(100), 
    gender = as.factor(sample(c("Male", "Female"), 100, replace = TRUE)) 
) 

# female as omitted level 
df_foo %>% 
    lm(y ~ age*gender, data = .) %>% 
    summary() 

# male as omitted level 
df_foo %>% 
    lm(y ~ age*relevel(gender, ref = "Male"), data = .) %>% 
    summary()