[Solved] R create a matrix of occurence [closed]


Ok, still not perfectly clear, but I THINK that presence is now an adjacency matrix, where the columns represent users and the rows represent events, so presence[i,j] indicates that user i attended event j.

If I’m interpreting it correctly then counts seems to be the co-occurrence matrix, correct? count[i,j] should record the number of events users i and j attended together?

If you look into the literature on 2-mode social network analysis, and specifically into affiliation networks, there is a direct calculation for this:

count = t(presence)%*%presence

Now I would check that with your own data to make sure it’s accurate: just check a couple cells randomly in count and compare them to presence.

Also, as an aside, with these kind of matrices rows are traditionally users and events are columns.

EDIT: if instead you want count to be the number of events EITHER user attended (as opposed to the number of events they BOTH attended) then it’s a union rather than an intersection. So it would be the number of events user i attended + the number of events user j attended – the intersection

x = t(presence)%*%presence
numEvents = diag(x)
counts = matrix(rep(numEvents,dim(x)[1]),nrow=dim(x)[1],byrow=TRUE)+matrix(rep(numEvents,dim(x)[1]),nrow=dim(x)[1],byrow=FALSE)-x

It’s not the most elegant solution, but it should work

1

solved R create a matrix of occurence [closed]