This guide explains an exemplary usage of the *semtree* package step by step. We will create a latent growth curve model in OpenMx, load some data, and infer a SEM Tree.

## Step 1 – Load packages

Make sure that you have installed the package „semtree“ (also see Download) and OpenMx (see OpenMx website). Open up R and import all necessary packages:

require("semtree");

## STEP 2 – Load dataset

Download the example file: example1.txt (2168).

Load the dataset with the following command (add a local path to the file, if necessary):

dataset <- data.frame(read.csv("example1.txt",sep="\t", header=T))

## STEP 3 – Create a model

Let’s create a linear latent growth curve model with a latent intercept and a latent slope, five occasions of measurement, and independent residual errors with equal variance. The following Figure shows a graphical representation of the model that we will define:

The following code is an OpenMx model specification of the above model:

manifests <- names(dataset)[1:5] growthCurveModel <- mxModel("Linear Growth Curve Model Path Specification", type="RAM", manifestVars=manifests, latentVars=c("intercept","slope"), mxData(dataset, type="raw"), # residual variances mxPath( from=manifests, arrows=2, free=TRUE, values = c(.1, .1, .1, .1, .1), labels=c("residual","residual","residual","residual","residual") ), # latent variances and covariance mxPath( from=c("intercept","slope"), arrows=2, connect="unique.pairs", free=TRUE, values=c(2, 0, 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") ) ) # close model

We can estimate parameters from the full parameter set by fitting the model in OpenMx:

run <- mxRun(growthCurveModel) summary(run);

## Step 4 – Grow a tree

The following command will start the tree growing process:

mytree <- semtree(growthCurveModel)

All columns in the dataset with names not corresponding to manifest variables in the model are considered covariates and, thus, are included in the list of split candidates. In this example, the columns „X1“ to „X5“ correspond to manifest variables. The remaining columns „age“, „training“, and „noise“ are therefore split candidates.

Please be patient. A large number of models is fitted during the inference of a tree.

## Step 5 – Plot the tree

This command will visually display the SEM Tree:

plot(mytree);

The graphical output should look similar to the following Figure:

Let’s draw the expected growth trajectories for each resulting leaf with the following code

# create expected trajectories from parameters expected.growth <- matrix( rep(t(parameters(tree))[, "meani"], each=5)+ rep(t(parameters(tree))[, "means"], each=5)*(0:4), nrow=3, byrow=T) # plot expected trajectories for each leaf plot(c(0,4), c(-3,5), xlab="time", ylab="score", type="n") lines(0:4, expected.growth[1,], col="red", type="b", lw=3) lines(0:4, expected.growth[2,], col="orange", type="b", lw=3) lines(0:4, expected.growth[3,], col="blue", type="b", lw=3) legend("bottomleft", c("left leaf", "middle leaf", "right leaf"),c ol=c("red", "orange", "blue"), lw=3)

This should look similar to this:

# Modify hyper-parameters of the tree

Hyper-parameters determine the way a SEM Tree is grown. For example, different variable selection strategies

can be employed, e.g., likelihood-ratio tests or cross-vadlidated likelihood-ratios. Other parameters affect

the maximum tree height or the type-I-error rate if a statistical selection criterion is used to select

variables. These hyper-parameters can be controlled with a semtree.control object. To obtain the default

object that is used in the semtree package, type:

my.control <- semtree.control(); semtree(model, control=my.control);

See the documentation for a full description of parameters. I mention only a few examples here:

- Set the significance level of a statistical selection criterion to 5%:
my.control$alpha <- 0.05

- Activate a conservative Bonferroni correction for multiple testing
my.control$bonferroni <- TRUE

- Set the minimum number of observations in a leaf node to 20:
my.control$min.N <- 20;

- Choose the fair (no variable selection bias) or crossvalidated likelihood-ratio as selection criterion:
my.control$method <- "fair";

or

my.control$method <- "cv";

- Exclude leafs with Heywood cases:
my.control$exclude.heywood <- TRUE;

# Variations on the graphical output

## Fallen leaves

If you prefer all leaf nodes to be at the same bottom line, add the command „fallen=TRUE“ to the plotting command:

plot(mytree, fallen=TRUE);

In case you prefer the sample size to be reflected in the width of the branches, use

plot(mytree, branch.type=5);