Evaluating NLS fit on each node in Sachs data and comparing residuals to Normal quantiles
devtools::load_all("R/optimization.R")
devtools::load_all("R/modelfitting.R")
data(tcells, package = "bninfo")
int_levels <- c("observational", "PKC", "PKA", "PIP2", "Mek", "Akt")
tcells_int <- tcells$processed$interventions %>% # read in intervention array
factor(levels = int_levels) %>% # make it a factor
{data.frame(int_ = .)} %>% # covert to design matrix and add to other variables
model.matrix( ~ int_, .) %>%
.[, - 1] %>% # (removes intercept)
{cbind(tcells$processed$.data, .)} %>%
apply(2, function(col) { # Standardize to between 0 and 1
col <- as.numeric(col)
if(min(col) != 0) col <- (col - min(col))/(max(col) - min(col))
col
}) %>%
data.frame
data(tcell_examples, package = "bninfo") #prepare to make intervention nodes
interventions <- c("int_PKC", "int_PKA", "int_PIP2", "int_Mek", "int_Akt")
tcell_input_net <- tcell_examples$net %>%
{bnlearn::as.graphNEL(.)} %>%
igraph.from.graphNEL %>%
{. + igraph::vertices(interventions)} %>%
{. + igraph::edges("int_PKC", "PKC",
"int_PKA", "PKA",
"int_PIP2", "PIP2",
"int_Mek", "Mek",
"int_Akt", "Akt")}
validated_net <- initializeGraph(tcell_input_net, data = tcells_int, fixed = interventions)
g <- validated_net %>%
induced.subgraph(V(g)[!is.bias])
lapply(V(g), function(v){
parents <- iparents(g, v)
g_data <- recover_design(g)
params <- paste("w", 1:length(parents), sep = "")
browser()
nls_fit <- params %>%
paste(., V(g)[parents]$name, sep = " * ") %>%
paste(collapse = " + ") %>%
{paste("w0 + ", .)} %>%
{paste("logistic(", ., ")")} %>%
{paste(V(g)[v]$name, " ~ ", .)} %>%
as.formula %>%
nls(data = g_data,
start = as.list(structure(c(1, E(g)[to(v)]$weight), names = c("w0", params))))
qqnorm(g_data[, V(g)[v]$name] - fitted(nls_fit))
})