Skip to content

Commit

Permalink
added styling using styler package
Browse files Browse the repository at this point in the history
  • Loading branch information
brandmaier committed Mar 24, 2024
1 parent 6e90ff6 commit e6e5a63
Show file tree
Hide file tree
Showing 10 changed files with 950 additions and 917 deletions.
25 changes: 11 additions & 14 deletions R/evaluateTree.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#' Evaluate Tree -2LL
#'
#'
#' A helper function to evaluate the negative two log-likelihood (-2LL) of leaf (terminal) nodes for a
#' dataset. When given a \code{\link{semtree}} and a unique dataset, the model
#' estimates -2LL for the tree parameters and data subsets that fit the tree
#' branching criteria.
#'
#'
#'
#'
#' @param tree A fitted \code{\link{semtree}} object
#' @param test_set Dataset to fit to a fitted \code{\link{semtree}} object
#' @param data_type type of data ("raw", "cov", "cor")
Expand All @@ -26,32 +26,29 @@ evaluateTree <-
function(tree,
test_set,
data_type = "raw",
leaf_ids = NULL)
{
leaf_ids = NULL) {
# get a mapping of dataset rows to leaf ids
if (is.null(leaf_ids)) {
leaf_ids <- traverse(tree, test_set)
}

# for each leaf, calculate deviance of each data row
dev <- 0
for (leaf_id in unique(leaf_ids))
{
temp_set <- test_set[leaf_ids == leaf_id,]
temp_set <- test_set[leaf_ids == leaf_id, ]


leaf <- getNodeById(tree, leaf_id)

# add up log-likelihoods
dev <-
dev + evaluateDataLikelihood(leaf$model, temp_set[, , drop = F], data_type)
}

result <- list()
result$deviance <- dev
result$num_models <- length(unique(leaf_ids))

return(result)


}
49 changes: 24 additions & 25 deletions R/evaluateTreeFocus.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,59 @@
# computes the difference in log-likelihoods considering focus parameters
#
evaluateTreeFocus <-
function(tree, test_set, data_type="raw", leaf_ids=NULL)
{

function(tree, test_set, data_type = "raw", leaf_ids = NULL) {
# get a mapping of dataset rows to leaf ids
if (is.null(leaf_ids)) {
leaf_ids <- traverse(tree, test_set)
}

# for each leaf, calculate deviance of each data row
dev <- 0
for (leaf_id in unique(leaf_ids))
{
# get all data rows from current leaf
temp_set <- test_set[leaf_ids==leaf_id, ];
temp_set <- test_set[leaf_ids == leaf_id, ]

# get the leaf object
leaf <- getNodeById( tree, leaf_id)
leaf <- getNodeById(tree, leaf_id)

# test if node has a focus model
if (is.null(leaf$focus.model)) {
ui_warn("No focus model available!")
return(NA)
}

# evaluate log-likelihood from baseline and focus model
#baseline = evaluateDataLikelihood(leaf$model, temp_set[,,drop=F], data_type )
ll.focus = evaluateDataLikelihood(leaf$focus.model,
temp_set[,,drop=F], data_type )

# baseline = evaluateDataLikelihood(leaf$model, temp_set[,,drop=F], data_type )
ll.focus <- evaluateDataLikelihood(
leaf$focus.model,
temp_set[, , drop = F], data_type
)

# evaluate log-likelihood after permutation


# add up log-likelihoods
dev <- dev + ll.focus
}

result <- list()
result$deviance <- dev
result$num_models <- length(unique(leaf_ids))

return(result);


return(result)
}

# TODO: finish this block
# TODO: remove earlier computation of baseline ll
# compute influence of focus parameter before permutation
#ll.baseline <- eval.fun(tree, oob.data)$deviance
#ll.baseline <- fitSubmodels(tree$model, subset1, subset2,
# ll.baseline <- eval.fun(tree, oob.data)$deviance
# ll.baseline <- fitSubmodels(tree$model, subset1, subset2,
# control, invariance=constraints$focus.parameters)
# compute misfit of focus parameter after permutation
#ll.permuted <- eval.fun(tree, oob.data.permuted)$deviance
#ll.diff <- -ll.baseline + ll.permuted
#ui_warn("Unfinished implementation!")
# ll.permuted <- eval.fun(tree, oob.data.permuted)$deviance
# ll.diff <- -ll.baseline + ll.permuted
# ui_warn("Unfinished implementation!")


#}
# }
Loading

0 comments on commit e6e5a63

Please sign in to comment.