简体   繁体   中英

multiple values of a categorical variable , GLM , model.matrix

I have a data that looks like this

    id var1 var2 var3 response var4
1   1    0 cat1    E        1   T4
2   1    0 cat1    E        1   T2
3   2    0 cat2    B        1 <NA>
4   3    0 cat3    C        0 <NA>
5   4    0 cat4    D        0   T3
6   4    0 cat4    D        0   T1
7   5    1 cat1    A        1   T4
8   5    1 cat1    A        1   T3
9   6    1 cat3    C        1   T3
10  7    0 cat1    C        0   T1

I want to run a glm for the dependent variable "response"

as you can see the ID's are not unique, that's because each ID can take more than one value for "var4" ,

to that end I tried model.matrix but it doesn't preserve the Id's, making it difficult to check the results, I am sure there is a simple way to do this, can someone help?

to generate the above data you can use

# GENERATING THE FIRST DATA FRAME

set.seed(1984)

df1 <- data.frame(id = 1:15, 
                  var1 = sample(0:1, 15, replace = T, prob = c(.7,.3)), 
                  var2 = sample(c('cat1','cat2', 'cat3','cat4'),15, replace = T), 
                  var3 = sample(LETTERS[1:5],15, replace = T ), 
                  response = sample(0:1, 15, replace = T)
                  )

# GENERATING THE 2ND DATA FRAME
set.seed(1984)

df2 <- data.frame(id = sample(1:15, 20, replace = T), 
                  var4 = sample(c('T1','T2','T3','T4'), 20, replace = T))

df2 <- unique(df2[order(df2$id), ])
row.names(df2) <- NULL

# MERGING THE TWO
df3 <- merge(df1, df2, by = 'id', all = T )

df3

I would reshape the data using dcast from the reshape2 package. This will reshape your data so each id is on one row and one-hot-encode var4 prior to feeding the dataframe into model.matix. Using your minimum working example this would be something like:

library(reashpe2)

newDF <- dcast(df3, ...~var4,function(x) length(x))
model.matrix(response ~., newDF)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM