Skip to content

Commit

Permalink
bugfix with crossvalidation for OpenMx
Browse files Browse the repository at this point in the history
improved lavaan support
  • Loading branch information
brandmaier committed Apr 5, 2017
1 parent a4e9af3 commit 6ac518f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
5 changes: 3 additions & 2 deletions R/crossvalidatedSplit.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ crossvalidatedSplit <- function(model=NULL, mydata=NULL, control=NULL,



mvars <- length(model@manifestVars)
#mvars <- length(model@manifestVars)
n.comp <- 0

LL.btn <- c()
Expand All @@ -28,7 +28,8 @@ crossvalidatedSplit <- function(model=NULL, mydata=NULL, control=NULL,

LL.baseline <- cvLikelihood(model, mydata, NULL, fold_association, NULL, control, invariance)

for(c in (mvars+1):ncol(mydata)) {
#for(c in (mvars+1):ncol(mydata)) {
for (c in meta$covariate.ids) {
#case for factored covariates##############################
if(is.factor(mydata[,c])) {
#unordered factors#####################################
Expand Down
6 changes: 4 additions & 2 deletions R/cvLikelihood.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ cvLikelihood <- function( model, subset1, subset2, fold_association1,fold_associ
}
else {
#return(NA)
model1 <- mxModel(model,mxData(observed=training_subset1,type="raw"),name=paste("MODEL",fold,sep=" "))
##model1 <- mxModel(model,mxData(observed=training_subset1,type="raw"),
# name=paste("MODEL",fold,sep=" "))
model1 <- mxAddNewModelData(model=model, data=training_subset1, name=paste("MODEL",fold,sep=" "))

out1 <- safeRunAndEvaluate(model1,return.model=T)
ll1 <- evaluateDataLikelihood(out1$model, test_subset1)
ll2 <- 0
Expand All @@ -33,7 +36,6 @@ cvLikelihood <- function( model, subset1, subset2, fold_association1,fold_associ

lrs[fold] <- (ll1+ll2)
}
#cat("AVG LL",mean(lrs)," of fold LLs: ", lrs, "\n")
return(mean(lrs,na.rm=T))

}
Expand Down
8 changes: 7 additions & 1 deletion R/evaluateDataLikelihood.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
evaluateDataLikelihood <-
function(model, data, data_type="raw")
{

if(inherits(model,"MxModel") || inherits(model,"MxRAMModel")) {

# this is to trick the strict CRAN check
objective <- NULL
#
Expand Down Expand Up @@ -58,4 +59,9 @@ function(model, data, data_type="raw")


return(result);


} else {
stop("The chosen combination of parameters for semtree is not yet supported with lavaan! Please use OpenMx model specification!")
}
}
2 changes: 1 addition & 1 deletion R/mxAddNewModelData.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mxAddNewModelData <- function(model=model, data=data, name="default", ...) {
} else if(inherits(model,"lavaan")) {
# must be a matrix it seems... TODO: needs to be fixed I guess
model@Data@X[[1]] <- data #as.matrix(data)
model@Data@nobs <- dim(data)[1]
model@Data@nobs[[1]] <- dim(data)[1]
# model@[email protected]
return(model)
} else {
Expand Down
9 changes: 9 additions & 0 deletions tests/tree.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,12 @@ treeSub <- subtree(tree, startNode=3)
plot(treeSub)

toTable(tree)


controlOptions <- semtree.control(method = "fair")

tree2 <- semtree(lgcModel, lgcm, control=controlOptions)


controlOptions <- semtree.control(method = "cv")
tree3 <- semtree(lgcModel, lgcm, control=controlOptions)

0 comments on commit 6ac518f

Please sign in to comment.