R Under development (unstable) (2023-11-25 r85635 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > set.seed(789) > require("semtree") Loading required package: semtree Loading required package: OpenMx > data(lgcm) > > lgcm$agegroup <- ordered(lgcm$agegroup, labels=c("young","old") ) > lgcm$training <- as.factor(lgcm$training) > lgcm$noise <- as.factor(lgcm$noise) > > # LOAD IN OPENMX MODEL. > # A SIMPLE LINEAR GROWTH MODEL WITH 5 TIME POINTS FROM SIMULATED DATA > > manifests <- names(lgcm)[1:5] > lgcModel <- mxModel("Linear Growth Curve Model Path Specification", + type="RAM", + manifestVars=manifests, + latentVars=c("intercept","slope"), + # residual variances + mxPath( + from=manifests, + arrows=2, + free=TRUE, + values = c(1, 1, 1, 1, 1), + labels=c("residual1","residual2","residual3","residual4","residual5") + ), + # latent variances and covariance + mxPath( + from=c("intercept","slope"), + connect="unique.pairs", + arrows=2, + free=TRUE, + values=c(1, 1, 1), + labels=c("vari", "cov", "vars") + ), + # intercept loadings + mxPath( + from="intercept", + to=manifests, + arrows=1, + free=FALSE, + values=c(1, 1, 1, 1, 1) + ), + # slope loadings + mxPath( + from="slope", + to=manifests, + arrows=1, + free=FALSE, + values=c(0, 1, 2, 3, 4) + ), + # manifest means + mxPath( + from="one", + to=manifests, + arrows=1, + free=FALSE, + values=c(0, 0, 0, 0, 0) + ), + # latent means + mxPath( + from="one", + to=c("intercept", "slope"), + arrows=1, + free=TRUE, + values=c(1, 1), + labels=c("meani", "means") + ), + mxData(lgcm,type="raw") + ) > > > # TREE CONTROL OPTIONS. > # TO OBTAIN BASIC/DEFAULT SMETREE OPTIONS, SIMPLY TPYE THE FOLLOWING: > > controlOptions <- semtree.control(method = "naive") > controlOptions$alpha <- 0.05 > > # RUN TREE. > > tree <- semtree(model=lgcModel, data=lgcm, control = controlOptions) ❯ Model was not run. Estimating parameters now. Beginning initial fit attempt Fit attempt 0, fit=3946.79931515407, new current best! (was 12364.5789577388) Beginning initial fit attempt Fit attempt 0, fit=3946.79931515401, new current best! (was 3946.79931515407) Beginning initial fit attempt Fit attempt 0, fit=1248.74919675683, new current best! (was 1891.35934994014) Beginning initial fit attempt Fit attempt 0, fit=1439.96925184945, new current best! (was 2055.43996521376) Beginning initial fit attempt Fit attempt 0, fit=681.527229818419, new current best! (was 707.63710438988) Beginning initial fit attempt Fit attempt 0, fit=717.588526402621, new current best! (was 732.332147459551) ✔ Tree construction finished [took 2s]. > > # RERUN TREE WITH MODEL CONSTRAINTS. > # MODEL CONSTRAINTS CAN BE ADDED BY IDENTIFYING THE PARAMETERS TO BE > # CONSTRAINED IN EVERY NODE. ONLY UNCONSTRAINED PARAMETERS ARE THEN > # TESTED AT EACH NODE FOR GROUP DIFFERENCES. IN THIS EXAMPLE THE MODEL > # RESIDUALS ARE CONSTRAINED OVER THE NODES. > > constraints <- semtree.constraints(global.invariance = names(omxGetParameters(lgcModel))[1:5]) > > treeConstrained <- semtree(model=lgcModel, data=lgcm, control = controlOptions, + constraints=constraints) ❯ Model was not run. Estimating parameters now. Beginning initial fit attempt Fit attempt 0, fit=3946.79931515407, new current best! (was 12364.5789577388) ❯ Global Constraints: residual1 residual2 residual3 residual4 residual5 ❯ Freely Estimated Parameters: vari cov vars meani means Beginning initial fit attempt Fit attempt 0, fit=3946.79931515401, new current best! (was 3946.79931515403) Beginning initial fit attempt Fit attempt 0, fit=1252.81403264897, new current best! (was 1891.35941970206) Beginning initial fit attempt Fit attempt 0, fit=1441.7428976327, new current best! (was 2055.43989545181) Beginning initial fit attempt Fit attempt 0, fit=684.355820699315, new current best! (was 708.427334727481) Beginning initial fit attempt Fit attempt 0, fit=720.860220424304, new current best! (was 733.315562905203) ✔ Tree construction finished [took 1s]. > > # SEE PLOT. > # THE PLOT FUNCTION WILL SHOW ALL FREE PARAMETERS AT EACH TERMINAL NODE. > # THIS CAN CREATE UNREADABLE FIGURES FOR MODELS WITH MANY FREE PARAMETERS. > > plot(tree) > > summary(tree) SEMtree Summary Template model: Total Sample Size: 400 Number of nodes: 5 Number of leaf nodes: 3 Free Parameters: 10 ( residual1 residual2 residual3 residual4 residual5 vari cov vars meani means ) > > summary(treeConstrained) SEMtree Summary Template model: Total Sample Size: 400 Number of nodes: 5 Number of leaf nodes: 3 Free Parameters: 5 ( vari cov vars meani means ) > > print(tree) SEMtree with numbered nodes |-[1] agegroup > young [N=400 LR=1258.08, df=10] | |-[2] TERMINAL [N=200] | |-[3] training in [ 0 ] [N=200 LR=40.85, df=10] | | |-[4] TERMINAL [N=100] | | |-[5] TERMINAL [N=100] > > parameters(tree) 2 4 5 residual1 0.068 0.048 0.042 residual2 0.049 0.069 0.049 residual3 0.043 0.053 0.053 residual4 0.055 0.039 0.063 residual5 0.037 0.048 0.011 vari 0.082 0.104 0.085 cov -0.011 0.028 -0.044 vars 0.499 0.674 1.629 meani 5.017 1.941 2.005 means -0.142 -0.494 -1.084 > > parameters(tree, leafs.only=FALSE) 1 2 3 4 5 residual1 0.052 0.068 0.045 0.048 0.042 residual2 0.058 0.049 0.059 0.069 0.049 residual3 0.047 0.043 0.053 0.053 0.053 residual4 0.052 0.055 0.051 0.039 0.063 residual5 0.036 0.037 0.030 0.048 0.011 vari 2.410 0.082 0.095 0.104 0.085 cov 0.477 -0.011 -0.017 0.028 -0.044 vars 0.973 0.499 1.239 0.674 1.629 meani 3.496 5.017 1.973 1.941 2.005 means -0.466 -0.142 -0.789 -0.494 -1.084 > > treeSub <- subtree(tree, startNode=3) > > if (!is.null(treeSub)) + plot(treeSub) > > toTable(tree) agegroup training residual1 residual2 [1,] "UNKNOWN" "" "0.0678143334729878" "0.0486295425064186" [2,] "UNKNOWN" "not ( 0 )" "0.0476435279339305" "0.0685455050796777" [3,] "UNKNOWN" "0" "0.0423695920602258" "0.0493100759085406" residual3 residual4 residual5 [1,] "0.0433892775139552" "0.0554064952972673" "0.037214712097997" [2,] "0.0534749160086889" "0.0391593692217432" "0.0483121224874375" [3,] "0.0528253430414206" "0.0627918242697525" "0.010891975342117" vari cov vars [1,] "0.081994946915562" "-0.011485589156015" "0.498964617245941" [2,] "0.104153273558773" "0.0276175463106271" "0.674250756562627" [3,] "0.0845469228958119" "-0.0440543872117917" "1.62914856556209" meani means [1,] "5.01657430618992" "-0.142228248366675" [2,] "1.94145611783388" "-0.494403616038746" [3,] "2.00474258134493" "-1.0837573853068" > > > controlOptions <- semtree.control(method = "fair") > > tree2 <- semtree(lgcModel, lgcm, control=controlOptions) ❯ Model was not run. Estimating parameters now. Beginning initial fit attempt Fit attempt 0, fit=3946.79931515407, new current best! (was 12364.5789577388) Beginning initial fit attempt Fit attempt 0, fit=3946.79931515401, new current best! (was 3946.79931515407) Beginning initial fit attempt Fit attempt 0, fit=1248.74919675683, new current best! (was 1891.35934994014) Beginning initial fit attempt Fit attempt 0, fit=1439.96925184945, new current best! (was 2055.43996521376) ✔ Tree construction finished [took 2s]. > > > controlOptions <- semtree.control(method = "score") > tree3 <- semtree(lgcModel, lgcm, control=controlOptions) ❯ Model was not run. Estimating parameters now. Beginning initial fit attempt Fit attempt 0, fit=3946.79931515407, new current best! (was 12364.5789577388) Beginning initial fit attempt Fit attempt 0, fit=3946.79931515401, new current best! (was 3946.79931515407) Beginning initial fit attempt Fit attempt 0, fit=1248.74919675683, new current best! (was 1891.35934994014) Beginning initial fit attempt Fit attempt 0, fit=1439.96925184945, new current best! (was 2055.43996521376) Beginning initial fit attempt Fit attempt 0, fit=681.527229818419, new current best! (was 707.63710438988) Beginning initial fit attempt Fit attempt 0, fit=717.588526402621, new current best! (was 732.332147459551) ✔ Tree construction finished [took 1s]. > > proc.time() user system elapsed 8.32 0.31 8.64