data(adjlg)
## See further usage in tests/test-adjacency-long.R
if (FALSE) {
# as produced by:
library(data.table) ## Included data produced using version 1.10.4.3
library(igraph) ## Included data produced using version 1.2.1
rsample <- function(N=100, ## size of implied adjacency matrix
month_max=10,seed) {
if (is.integer(seed)) set.seed(seed)
dt <- data.table(ID=factor(1:N))
dt$months <- sample(1:month_max,N,replace=T) ## # of liens for each level of ID
dt$GENDER <- sample(c("MALE","FEMALE"),N,replace=TRUE)
dt$AGE <- sample(18:99,N,replace=T)
dt$X1 <- sample(1000:9900,N,replace=T)
dt$X2 <- runif(N)
dt <- dt[, c(.SD, month=data.table(seq(from=1, to=months, by = 1))), by = ID]
dt[,BUY := 0]
dt[month.V1==months,BUY := sample(c(0,1),1),by=ID]
setnames(dt,"month.V1","month")
#### create adjacency matrix
Network <- data.table(OUT=sample(dt$ID,N*month_max*4/10))
Network$IN <- sample(dt$ID,N*month_max*4/10)
Network <- Network[IN != OUT]
Network <- unique(Network)
g <- graph.data.frame(Network,directed=F)
g <- add_vertices(g,sum(!unique(dt$ID) %in% V(g)),
name=unique(dt[!dt$ID %in% V(g),list(ID)])) # => improper names
Network <- as_adjacency_matrix(g,sparse = TRUE,type="both")
colnames(Network) <- rownames(Network) <- seq(nrow(Network)) # post-v3.8.0 names
return(list(data=dt,adjMatrix=Network))
}
RNGkind("Mersenne-Twister", "Inversion", "Rounding" )
set.seed(123)
adjlg_sam <- rsample(N=1000,seed=NULL)
RNGkind("Mersenne-Twister", "Inversion", "Rejection" )
#
adjlg <- as.data.frame(adjlg_sam$data)
adjlgMat <- adjlg_sam$adjMatrix
}
Run the code above in your browser using DataLab