-
Notifications
You must be signed in to change notification settings - Fork 0
/
GammafromCTM_test-input-output.Rmd
232 lines (182 loc) · 5.62 KB
/
GammafromCTM_test-input-output.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
---
title: "Testing the input and output of Area"
author: "Judith Neve"
date: "20/01/2022"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(CTmeta)
```
This document tests what can be used as input and output for the function Area. Each argument is examined in turn, and at the end some additional checks are done to verify plots can be made for different configurations of the model.
Where the function runs, the output is compared to the expected output.
After every attempt to "break" the function, a diagnostic is written as a comment in the code chunk, and the code chunk is followed by a "to do" list to indicate what needs to be specified/improved/changed in the function documentation or the function itself.
Original example:
```{r}
# library(CTmeta)
## Example 1 ##
#
Drift <- myDrift
#
q <- dim(Drift)[1]
Sigma <- diag(q) # for ease
#
gammafromCTM <- CTmeta::Gamma.fromCTM(Drift, Sigma)
gammafromCTM.mod <- Gamma.fromCTM(Drift, Sigma)
## Example 2: input from fitted object of class "ctsemFit" ##
#
#data <- myData
#if (!require("ctsemFit")) install.packages("ctsemFit")
#library(ctsemFit)
#out_CTM <- ctFit(...)
#Gamma.fromCTM(out_CTM)
# #
# if (!require("ctsem")) install.packages("ctsem")
library(ctsem)
library(ctsemOMX)
# #
library(mlVAR)
# outcome <- list()
# for (s in 1:50) {
# set.seed(s)
#
# n <- 5
# t <- 100
# ctfit.data <- matrix(NA, nrow = t*n, ncol = 5)
# ctfit.data[,1] <- rep(1:n, each = t)
# ctfit.data[,2] <- rep(0:(t-1), n)
# ctfit.data[,5] <- 1
# for (i in 0:(n-1)*t) {
# temp <- simulateVAR(pars = myPhi[1:2,], means = 0, lags = 1, Nt = t, init = 0, residuals = mySigmaVAR[1:2,], burnin = 100)
# ctfit.data[(i+1):(i+t), 3] <- temp[,1]
# ctfit.data[(i+1):(i+t), 4] <- temp[,2]
# }
#
# colnames(ctfit.data) <- c("idcol", "time", "V1", "V2", "d")
#
# ctfit.data <- as.data.frame(ctfit.data) %>%
# pivot_wider(names_from = time,
# names_prefix = "T",
# values_from = c("V1", "V2", "d")) %>%
# select(-idcol, -d_T0)
#
# colnames(ctfit.data)[(2*t+1):ncol(ctfit.data)] <- paste0("dT", 1:(t-1))
#
#
# model <- ctModel(n.manifest=2, n.latent=2, Tpoints=t, LAMBDA=diag(2),
# manifestNames=c('V1', 'V2'),
# latentNames=c('V1', 'V2'))
# out_CTM <- ctFit(dat=ctfit.data, ctmodelobj=model)
#
# outcome[[s]] <- Gamma.fromCTM(out_CTM) %>% try()
# }
# set.seed(3)
#
# n <- 100
# t <- 5
# ctfit.data <- matrix(NA, nrow = t*n, ncol = 5)
# ctfit.data[,1] <- rep(1:n, each = t)
# ctfit.data[,2] <- rep(0:(t-1), n)
# ctfit.data[,5] <- 1
# for (i in 0:(t-1)*n+1) {
# temp <- simulateVAR(pars = myPhi[1:2,], means = 0, lags = 1, Nt = t, init = 0, residuals = mySigmaVAR[1:2,], burnin = 100)
# ctfit.data[i:(i+n-1), 3] <- temp[,1]
# ctfit.data[i:(i+n-1), 4] <- temp[,2]
# }
#
# colnames(ctfit.data) <- c("idcol", "time", "V1", "V2", "d")
#
# library(tidyverse)
# ctfit.data <- as.data.frame(ctfit.data) %>%
# pivot_wider(names_from = time,
# names_prefix = "T",
# values_from = c("V1", "V2", "d")) %>%
# select(-idcol, -d_T0)
#
# colnames(ctfit.data)[(2*t+1):ncol(ctfit.data)] <- paste0("dT", 1:(t-1))
#
#
# model <- ctModel(n.manifest=2, n.latent=2, Tpoints=t, LAMBDA=diag(2),
# manifestNames=c('V1', 'V2'),
# latentNames=c('V1', 'V2'))
# out_CTM <- ctFit(dat=ctfit.data, ctmodelobj=model)
#
# summary(out_CTM)
#
# Gamma.fromCTM(out_CTM) %>% try()
if (!require("ctsem")) install.packages("ctsem")
library(ctsem)
library(ctsemOMX)
#
############ adapted from https://rdrr.io/cran/ctsemOMX/man/ctFit.html ############
data(ctExample1)
model <- ctModel(n.manifest=2, n.latent=2, Tpoints=6, LAMBDA=diag(2),
manifestNames=c('LeisureTime', 'Happiness'),
latentNames=c('LeisureTime', 'Happiness'), TRAITVAR="auto")
out_CTM <- ctFit(dat=ctExample1, ctmodelobj=model)
##################################################################################
#
Gamma.fromCTM(out_CTM)
```
# Drift
## Drift with NA
```{r}
Drift.break <- Drift
Drift.break[1,2] <- NA
Gamma.fromCTM(Drift = Drift.break, Sigma = Sigma) %>% try()
```
Added an error
## Non-numeric Drift
```{r}
Drift.break <- Drift
Drift.break[1,2] <- "text"
Gamma.fromCTM(Drift = Drift.break, Sigma = Sigma) %>% try()
```
## Drift with one more row
```{r}
Drift.break <- rbind(Drift, c(0,1))
Gamma.fromCTM(Drift = Drift.break, Sigma = Sigma) %>% try()
```
Correct error
## Drift with one more col
```{r}
Drift.break <- cbind(Drift, c(0,1))
Gamma.fromCTM(Drift = Drift.break, Sigma = Sigma) %>% try()
```
Correct error
## Drift with one more row and one more col
```{r}
Drift.break <- matrix(data = c(16, -8, -4, -8, 29, 12, -4, 12, 41), nrow = 3, ncol = 3, byrow = TRUE)
Gamma.fromCTM(Drift = Drift.break, Sigma = Sigma) %>% try()
```
Corrected error so it is about the mismatch of sizes
# Sigma
## Sigma with NA
```{r}
Sigma.break <- Sigma
Sigma.break[1,2] <- NA
Gamma.fromCTM(Drift = Drift, Sigma = Sigma.break) %>% try()
```
It does return something full of NAs - appropriate
## Non-numeric Sigma
```{r}
Sigma.break <- Sigma
Sigma.break[1,2] <- "text"
Gamma.fromCTM(Drift = Drift, Sigma = Sigma.break) %>% try()
```
Made the error be in Check_Sigma.
## Sigma with one more row
```{r}
Sigma.break <- rbind(Sigma, c(1,1))
Gamma.fromCTM(Drift = Drift, Sigma = Sigma.break) %>% try()
```
## Sigma with one more col
```{r}
Sigma.break <- cbind(Sigma, c(1,1))
Gamma.fromCTM(Drift = Drift, Sigma = Sigma.break) %>% try()
```
## Sigma with one more row and one more col
```{r}
Sigma.break <- cbind(rbind(Sigma, c(1,1)), c(1,1,1))
Gamma.fromCTM(Drift = Drift, Sigma = Sigma.break) %>% try()
```