Skip to content

Commit 6ac518f

Browse files
committed
bugfix with crossvalidation for OpenMx
improved lavaan support
1 parent a4e9af3 commit 6ac518f

5 files changed

Lines changed: 24 additions & 6 deletions

File tree

R/crossvalidatedSplit.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ crossvalidatedSplit <- function(model=NULL, mydata=NULL, control=NULL,
1212

1313

1414

15-
mvars <- length(model@manifestVars)
15+
#mvars <- length(model@manifestVars)
1616
n.comp <- 0
1717

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

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

31-
for(c in (mvars+1):ncol(mydata)) {
31+
#for(c in (mvars+1):ncol(mydata)) {
32+
for (c in meta$covariate.ids) {
3233
#case for factored covariates##############################
3334
if(is.factor(mydata[,c])) {
3435
#unordered factors#####################################

R/cvLikelihood.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ cvLikelihood <- function( model, subset1, subset2, fold_association1,fold_associ
2424
}
2525
else {
2626
#return(NA)
27-
model1 <- mxModel(model,mxData(observed=training_subset1,type="raw"),name=paste("MODEL",fold,sep=" "))
27+
##model1 <- mxModel(model,mxData(observed=training_subset1,type="raw"),
28+
# name=paste("MODEL",fold,sep=" "))
29+
model1 <- mxAddNewModelData(model=model, data=training_subset1, name=paste("MODEL",fold,sep=" "))
30+
2831
out1 <- safeRunAndEvaluate(model1,return.model=T)
2932
ll1 <- evaluateDataLikelihood(out1$model, test_subset1)
3033
ll2 <- 0
@@ -33,7 +36,6 @@ cvLikelihood <- function( model, subset1, subset2, fold_association1,fold_associ
3336

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

3941
}

R/evaluateDataLikelihood.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
evaluateDataLikelihood <-
22
function(model, data, data_type="raw")
33
{
4-
4+
if(inherits(model,"MxModel") || inherits(model,"MxRAMModel")) {
5+
56
# this is to trick the strict CRAN check
67
objective <- NULL
78
#
@@ -58,4 +59,9 @@ function(model, data, data_type="raw")
5859

5960

6061
return(result);
62+
63+
64+
} else {
65+
stop("The chosen combination of parameters for semtree is not yet supported with lavaan! Please use OpenMx model specification!")
66+
}
6167
}

R/mxAddNewModelData.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ mxAddNewModelData <- function(model=model, data=data, name="default", ...) {
1313
} else if(inherits(model,"lavaan")) {
1414
# must be a matrix it seems... TODO: needs to be fixed I guess
1515
model@Data@X[[1]] <- data #as.matrix(data)
16-
model@Data@nobs <- dim(data)[1]
16+
model@Data@nobs[[1]] <- dim(data)[1]
1717
# model@Data@case.idx
1818
return(model)
1919
} else {

tests/tree.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,12 @@ treeSub <- subtree(tree, startNode=3)
110110
plot(treeSub)
111111

112112
toTable(tree)
113+
114+
115+
controlOptions <- semtree.control(method = "fair")
116+
117+
tree2 <- semtree(lgcModel, lgcm, control=controlOptions)
118+
119+
120+
controlOptions <- semtree.control(method = "cv")
121+
tree3 <- semtree(lgcModel, lgcm, control=controlOptions)

0 commit comments

Comments
 (0)