Skip to content

Instantly share code, notes, and snippets.

@aronlindberg
Last active December 3, 2015 04:00
Show Gist options
  • Save aronlindberg/a62c8cef45e912fb0298 to your computer and use it in GitHub Desktop.
Save aronlindberg/a62c8cef45e912fb0298 to your computer and use it in GitHub Desktop.
sequential_variation.R
library(plyr)
# turn the data into event-graphs
graph_transformation <- function(dat){
dat2 <- ddply(dat, .(id), function(d){
data.frame(
event = d$event[-1],
from = d$event[-NROW(d)],
to = d$event[-1],
time = paste(d$time[-NROW(d)], d$time[-1], sep = "-")
)
})
rc1_edge_list <- cbind(dat2["from"], dat2["to"])
network_data_object <- try_default(as.network(rc1_edge_list[, 1:2], hyper=FALSE, loops=TRUE, multiple=TRUE), default = NULL)
(network_data_object)
}
# This function extracts the degrees for each event (i.e. the number of sequential transitions it has with other event types)
extract_degrees <- function(x){
list_rc <- split(x, x$id)
sna_list <- lapply(list_rc, graph_transformation)
edgecount_list <- list()
for (i in 1:length(sna_list)){
if(is.null(sna_list[[i]])) {
edgecount_list[i] <- list(NULL)
} else {
edgecount_list[i] <- network.edgecount(sna_list[[i]])
}
}
for (i in 1:length(edgecount_list)){
if(is.null(edgecount_list[[i]])) {
edgecount_list[[i]] <- 0
} else {
edgecount_list[[i]] <- edgecount_list[[i]]
}
}
(unlist(edgecount_list))
}
sequential_variation <- extract_degrees(data)
sequential_variation_normalized <- sequential_variation/seqlength(data.seq)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment