-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
276 lines (210 loc) · 7.29 KB
/
README.Rmd
File metadata and controls
276 lines (210 loc) · 7.29 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# simMultiCov
<!-- badges: start -->
[](https://github.com/BmBaczkowski/simMultiCov/actions/workflows/R-CMD-check.yaml)
[](https://app.codecov.io/gh/BmBaczkowski/simMultiCov?branch=main)
<!-- badges: end -->
**simMultiCov** provides tools for simulating multilevel (clustered) covariates with flexible correlation structures. It supports continuous, binary, and ordinal covariates, allowing researchers to generate realistic clustered data for methodological studies, power analyses, and simulation-based research.
## Features
- **Multiple covariate types**: Simulate continuous, binary, and ordinal covariates
- **Flexible correlation structures**: Specify separate within-cluster and between-cluster correlations
- **Latent variable approach**: Binary and ordinal covariates are generated using latent normal distributions
- **Unequal cluster sizes**: Support for varying numbers of observations per cluster
- **Multiple datasets**: Generate multiple independent datasets in a single call
- **Reproducible simulations**: Optional seed parameter for reproducibility
## Installation
You can install the development version of simMultiCov from GitHub with:
``` r
# install.packages("devtools")
devtools::install_github("BmBaczkowski/simMultiCov")
```
Or using the `remotes` package:
``` r
# install.packages("remotes")
remotes::install_github("BmBaczkowski/simMultiCov")
```
## Quick Start
### Basic Example
```{r example}
library(simMultiCov)
# Define covariates
covs <- make_covariates(
covariates = list(
make_continuous("age", mean = 50, total_var = 100, icc = 0.2),
make_binary("treatment", prob = 0.5, icc = 0.1),
make_ordinal("satisfaction", probs = c(0.2, 0.3, 0.5), icc = 0.15)
),
correlations = list(
define_correlation("age", "treatment", corr_within = 0.1, corr_between = 0.05)
)
)
# View the specification
print(covs)
```
### Simulate Data
```{r simulate}
# Simulate a single dataset with 10 clusters of 20 observations each
df <- simulate(covs, n_clusters = 10, cluster_size = 20, seed = 123)
# View the first few rows
head(df)
# Check the data structure
str(df)
```
### Unequal Cluster Sizes
```{r unequal}
# Simulate with varying cluster sizes
df_unequal <- simulate(
covs,
n_clusters = 5,
cluster_size = c(10, 15, 20, 25, 30),
seed = 456
)
# View cluster sizes
table(df_unequal$cluster)
```
### Multiple Datasets
```{r multiple}
# Generate 3 independent datasets
datasets <- simulate(
covs,
n_clusters = 5,
cluster_size = 10,
n_datasets = 3,
seed = 789
)
# Check the number of datasets
length(datasets)
# Each dataset is a data frame
class(datasets[[1]])
```
## Detailed Examples
### Continuous Covariates
```{r continuous}
# Create a continuous covariate with ICC of 0.3
ability <- make_continuous(
name = "ability",
mean = 100,
total_var = 225,
icc = 0.3
)
# Build covariate specification
covs <- make_covariates(covariates = list(ability))
# Simulate data
df <- simulate(covs, n_clusters = 8, cluster_size = 15, seed = 111)
# Verify ICC
library(dplyr)
df %>%
group_by(cluster) %>%
summarise(cluster_mean = mean(ability)) %>%
summarise(
between_var = var(cluster_mean),
total_var = var(df$ability),
icc = between_var / total_var
)
```
### Binary Covariates
```{r binary}
# Create a binary treatment variable
treatment <- make_binary(
name = "treatment",
prob = 0.6,
icc = 0.15,
labels = c("Control", "Treatment")
)
# Build specification
covs <- make_covariates(covariates = list(treatment))
# Simulate data
df <- simulate(covs, n_clusters = 6, cluster_size = 20, seed = 222)
# Check treatment distribution
table(df$treatment)
# Check ICC
df %>%
group_by(cluster) %>%
summarise(cluster_prop = mean(treatment == "Treatment")) %>%
summarise(
between_var = var(cluster_prop),
total_prop = mean(df$treatment == "Treatment"),
icc = between_var / (total_prop * (1 - total_prop))
)
```
### Ordinal Covariates
```{r ordinal}
# Create an ordinal satisfaction variable
satisfaction <- make_ordinal(
name = "satisfaction",
probs = c(0.15, 0.25, 0.35, 0.25),
icc = 0.2,
labels = c("VeryLow", "Low", "High", "VeryHigh")
)
# Build specification
covs <- make_covariates(covariates = list(satisfaction))
# Simulate data
df <- simulate(covs, n_clusters = 10, cluster_size = 25, seed = 333)
# Check distribution
table(df$satisfaction)
```
### Correlated Covariates
```{r correlated}
# Create multiple correlated covariates
covs <- make_covariates(
covariates = list(
make_continuous("income", mean = 50000, total_var = 100000000, icc = 0.25),
make_continuous("education", mean = 16, total_var = 4, icc = 0.2),
make_binary("employed", prob = 0.7, icc = 0.1)
),
correlations = list(
define_correlation("income", "education", corr_within = 0.5, corr_between = 0.6),
define_correlation("income", "employed", corr_within = 0.3, corr_between = 0.2),
define_correlation("education", "employed", corr_within = 0.4, corr_between = 0.3)
)
)
# View the specification
summary(covs)
# Simulate data
df <- simulate(covs, n_clusters = 15, cluster_size = 30, seed = 444)
# Check correlations
cor(df[, c("income", "education")])
```
## Use Cases
**simMultiCov** is particularly useful for:
- **Methodological research**: Testing the performance of multilevel models under various data-generating conditions
- **Power analysis**: Determining required sample sizes for multilevel studies
- **Teaching**: Demonstrating multilevel data structures and analysis techniques
- **Simulation studies**: Generating realistic clustered data for Monte Carlo simulations
- **Sensitivity analysis**: Assessing how violations of assumptions affect model performance
## Package Structure
The package provides the following main functions:
- `make_continuous()`: Define continuous covariates
- `make_binary()`: Define binary covariates
- `make_ordinal()`: Define ordinal covariates
- `define_correlation()`: Specify correlations between covariates
- `make_covariates()`: Combine covariate specifications into a complete structure
- `simulate()`: Generate simulated data from a covariate specification
## Getting Help
- **Documentation**: Run `?function_name` for detailed help on any function
- **Issues**: Report bugs or request features on [GitHub Issues](https://github.com/BmBaczkowski/simMultiCov/issues)
## Citation
If you use simMultiCov in your research, please cite:
``` r
citation("simMultiCov")
```
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
4. Push to the branch (`git push origin feature/AmazingFeature`)
5. Open a Pull Request