---
title: "sysVar_inOut_V05"
author: "Emily Butler, eabutler@u.arizona.edu & Ashley Kuelz, akuelz@email.arizona.edu"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{sysVar_inOut_V05}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

Once you have gone through the steps for estimating dynamic trajectory profiles for one of the models (see "inertia_coordination" and "coupled_oscillator" vignettes), you are ready to use the profiles as either predictors or outcomes of system variables, e.g., any variables of interest that you think are related to your dyadic system, but change slower than the state variables used to represent the dynamics. We show only the basic steps for getting the profile memberships from the inertia-coordination model here (see other vignettes for full procedure).

```{r, fig.show="hide", results="hide"}
library(rties)
data1 <- rties_ExampleDataFull

data2 <- dataPrep(basedata=data1, dyadId="couple", personId="person", obs_name="dial", dist_name="female", time_name="time", time_lag="absMaxCC") 

ic <- indivInertCoord(prepData=data2, whichModel="inertCoord")

lpaData <- inspectProfiles(whichModel="inertCoord", prepData=data2, paramEst=ic$params, n_profiles=2) 

fullData <- makeFullData(basedata=data1, dyadId="couple", personId="person", dist_name="female", lpaData=lpaData, params=ic$params)
```

### Predicting the System Variable From the Profiles 

The "sysVarOut" function uses the profile memberships to predict system variables, which can be either dyadic (sysVarType = "dyadic"), where both partners have the same score (e.g., relationship length) or individual (sysVarType = "indiv"), where the partners can have different scores (e.g., age). It takes as arguments the name of the dataframe containing the profile membership scores combined with your original dataframe (created by the "makeFullData" function), the name of the column in the dataframe containing the variable you would like to use as the system variable, and whether the system variable is "dyadic" or "individual". For dyadic system variables, the only predictor is profile membership and the model is a regular regression model since all variables are at the level of the dyad. If the system variable is individual then the model is a random-intercept dyadic model and 3 models are estimated: 1) the main effect of profile membership ("profile"), 2) main effects of profile membership and the distinguishing variable ("profilePlusDist"), and 3) the interaction of profile membership and the distinguishing variable ("profileByDist"). If the system variable is not normally distributed, any of the generalized linear models supported by glm (for dyadic system variables) or glmer (for individual system variables) are available by specifying the "family" distribution (use ?sysVarOut for more information).

For normally distributed system variables, the function returns a list including the lm or lme objects containing the full results for each model (called "models"). Similarly, for non-normal system variables, the function returns a list of the glm or glmer objects containing the full results for the models.

We start with an example of a normally (or at least close enough to normally) distributed dyadic system variable, "dyadInfluence", which was the average self-report for each couple of their attempts to influence each other during a conversation. We first run the "sysVarOut" function and then use the "sysVarOutResults" function to produce a set of results from the model. Since this is a dyadic system variable, the only model is the main effect of profile and we compare it to the base model. The results include a Likelihood Ratio test (LRT) of the overall model fit compared to the null (base) model, an omnibus anova table (which is identical to the LRT for a dyadic system variable), and the parameter estimates. In this example, we see there is no effect of profile on influence attempts.

```{r}
sysOut <- sysVarOut(fullData=fullData, sysVar_name="dyadInfluence", sysVarType="dyadic", dist0name="Men", dist1name="Women")
sysVarOutResults(sysOut$models$base, sysOut$models$profile)
```

We can also use the "sysVarOutPlots" function to produce a histogram of the residuals from the model and a boxplot of the system variable for each profile.
```{r, fig.width=5}
sysVarOutPlots(fullData=fullData, sysVar_name="dyadSup", sysVarType="dyadic", testModel=sysOut$models$profile, dist0name=NULL, dist1name=NULL)
```

As a second example, we consider a binomial individual level system variable, "ambivB" which was a self-report of ambivalence about the relationship, answered yes vs. no. Since this is an individual system variable, three models are now estimated: 1) main effect of profile, 2) main effects of profile and the distinguishing variable, and 2) the interaction of profile by the distinguishing variable. We call "sysVarOutResults" three times, once for each model, again comparing each to the base model. The results contain the same information as described for the previous example, but since this a logistic model (due to the binomial system variable) the odds ratios obtained by exponentiating the parameter estimates are also provided. As in the previous example, we find no evidence of any effect of profiles or the distinguishing variable on ambivalence.

```{r}
sysOut <- sysVarOut(fullData=fullData, sysVar_name="ambivB", sysVarType="indiv", dist0name="Men", dist1name="Women", family="binomial")
sysVarOutResults(sysOut$models$base, sysOut$models$profile, Gaussian=F)
sysVarOutResults(sysOut$models$base, sysOut$models$profilePlusDist, Gaussian=F)
sysVarOutResults(sysOut$models$base, sysOut$models$profileByDist, Gaussian=F)
```

We can again use the "sysVarOutPlots" function, this time with "binomial=TRUE", to produce a plot of the predicted probabilities of the system variable for each profile.
```{r, fig.width=5}
sysVarOutPlots(fullData=fullData, sysVar_name="ambivB", sysVarType="indiv", testModel=sysOut$models$profileByDist, dist0name=NULL, dist1name=NULL, binomial=T)
```

### Predicting Profile Membership From the System Variable

The "sysVarIn" function turns the direction of prediction around and uses the system variable to predict couples' profile memberships. It takes as arguments the name of the dataframe containing the profile membership scores combined with your original dataframe (created by the "makeFullData" function), the name of the column in the dataframe containing the variable you would like to use as the system variable, whether the system variable is "dyadic" or "individual", and the number of profiles. If there are 2 profiles, then binomial regression models are used. If there are more than 2 profiles then multinomial regression is used. For dyadic system variables, a couple's shared score is the only predictor of their profile membership (the model is called "sysVarMain"). For individual system variables, two models are tested, one with the main effects of both partner's system variable ("sysVarMain") and one with the main effects and their interaction ("sysVarInteract"). In both cases an intercept-only model is included as a comparison point (called "base"). The function returns a list of the model objects and result summaries can be obtained with the "sysVarInResults" function. The results include a Chisquare Deviance test (ChiSq) of the overall model fit compared to the null (base) model, the parameter estimates and odds ratios obtained by exponentiating the parameter estimates.  

We first consider a dyadic system variable and again find no evidence for it predicting the profiles.
```{r}
sysIn <- sysVarIn(fullData=fullData, sysVar_name="dyadInfluence", n_profiles=2, sysVarType="dyadic")
sysVarInResults(sysIn$models$base, sysIn$models$sysVarMain, n_profiles=2)
```
We can also use the "sysVarInPlots" function to produce a plot of profile membership at observed values of the system variable.
```{r, fig.width=5}
inPlots <- sysVarInPlots(fullData=fullData, sysVar_name="dyadInfluence", sysVarType="dyadic", n_profiles=2)
```

Next we consider a categorical individual level system variable. Because it is at the individual level, there are two models estimated, one for the main effects of each partner's system variable and one for their interaction, so we use the "sysVarInResults" function twice, once for each model. We still see no evidence of any effects.
```{r}
sysIn <- sysVarIn(fullData=fullData, sysVar_name="conflictCat", n_profiles=2, sysVarType="indiv")
sysVarInResults(sysIn$models$base, sysIn$models$sysVarMain, n_profiles=2)
sysVarInResults(sysIn$models$base, sysIn$models$sysVarInteract, n_profiles=2)
```

Finally, we can produce a plot of the model predicted probabilities of being in each profile for the different combinations of the partner's system variables.
```{r, fig.width=5}
inPlots <- sysVarInPlots(fullData=fullData, sysVar_name="conflictCat", sysVarType="indiv", n_profiles=2, testModel= sysIn$models$sysVarInteract, dist0name="men", dist1name="women")
```
