Tuesday 15 September 2015

dataset - Converting cross-sectional data into an adjacency matrix in R -



dataset - Converting cross-sectional data into an adjacency matrix in R -

i trying convert cross-sectional info adjacency matrix, want analyze how variables nowadays social network analysis. in case empirical examples help logic, it's analogous presenting 4 people selection of 3 objects; can take 0 3 of objects. i'd analyze how commonly different objects chosen , visualize network of preferences.

the info set cross-sectional data, below:

id1 <- c(1,0,0) id2 <- c(1,0,1) id3 <- c(1,1,1) id4 <- c(0,0,0) ids <- c("1","2","3","4") df <- data.frame(rbind(id1, id2, id3, id4)) df <- cbind(ids, df) colnames(df) <- c("id", "var1", "var2", "var3")

i'd create weighted adjacency matrix var1, var2 , var3, each cell containing total number of times 2 variables occur among observations.

so basic procedure thinking create separate matrix each row (each id number) 1 or 0 each cell indicating whether or not both variables nowadays id. , add together these matrices together, final matrix gives total number of joint appearances.

i've been looking around , haven't quite gotten right. thought of using outer, it'd need work each column in sequence. reply pretty close, wasn't sure how adding values. ended list of matrices, values didn't correspond initial data- convert categorical info in info frame weighted adjacency matrix. , reply close, although seemed have different type of data. gave me adjacency matrix based on ids- http://r.789695.n4.nabble.com/conversion-to-adjacency-matrix-td794102.html

here messy code manually create matrix 1 observation, sense i'm going (using vector representing first id observation)

id1 <- c(1,0,0) var1 <- id1[[1]] var2 <- id1[[2]] var3 <- id1[[3]] onetwo <- var1 * var2 onethree <- var1 * var3 twothree <- var2 * var3 oneone <- var1 * var1 twotwo <- var2 * var2 threethree <- var3 * var3 rows1 <- rbind(oneone, onetwo, onethree) rows2 <- rbind(onetwo, twotwo, twothree) rows3 <- rbind(onethree, twothree, threethree) df2 <- cbind(rows1, rows2, rows3)

this not ideal, actual dataset has 198 observations , 33 variables, looping or utilize of apply functions inefficient.

i can't tell if i'm making more hard needs be, or if i'm trying forcefulness info wasn't meant do. if has run sort of task before, please allow me know. there way create desired adjacency matrix directly? should transfer border list first, , there way that? there code create first step(creating matrix each row of dataframe) more efficient?

thanks help,

i'm not sure if understand question, want?

nc=33 nr=198 m3<-matrix(sample(0:1,nc*nr,replace=true),nrow=nr) df3<-data.frame(m3) m3b <-matrix(0,nrow=nc,ncol=nc) for(i in seq(1,nc)) { (j in seq(1,nc)) { t3<-table(df3[,i],df3[,j]) m3b[i,j] = t3[2,2] # t3[2,2] contains count of df3[,i] = df3[,j] = 1 # or # t3 = sum(df3[,i]==df3[,j] & df3[,i] == 1) # m3b[i,j] = t3 } }

or, if want sum of product, gives same result if 1 or 0

m3c <-matrix(0,nrow=nc,ncol=nc) for(i in seq(1,nc)) { (j in seq(1,nc)) { sv=0 (k in seq(1,nr)) { vi = df3[k,i] vj = df3[k,j] sv=sv+vi*vj } m3c[i,j] = sv } }

r dataset adjacency-matrix

No comments:

Post a Comment