提问人:Armel Soubeiga 提问时间:9/30/2023 最后编辑:Armel Soubeiga 更新时间:10/2/2023 访问量:76
R:使用有序分类变量模拟纵向数据
R: simulation of longitudinal data with an ordered categorical variable
问:
我正在尝试为我的研究模拟纵向数据。数据必须包含一个分类变量和另一个有序分类变量。为此,我使用了 R 包,它可以模拟纵向数据。simstudy
1- 对于简单的分类变量(),我可以使用以下代码来做到这一点:var_c_3m
library(tidyverse)
library(simstudy)
def <- defData(varname = "id", formula = "1:10") # Creating identifiers
## Longitudinal data with varying observation and interval times
## Source: https://kgoldfeld.github.io/simstudy/articles/longitudinal.html
def <- defData(def, varname = "nCount", dist = "noZeroPoisson", formula = 6)
def <- defData(def, varname = "mInterval", dist = "gamma", formula = 30, variance = 0.01)
def <- defData(def, varname = "vInterval", dist = "nonrandom", formula = 0.07)
df <- genData(n, def)
df <- addPeriods(df)
# nCount defines the number of measurements for an individual
# mInterval specifies the average time between intervals for a subject
# vInterval specifies the variance of those interval times
# Simulating a categorical variable with 3 categories according to the distribution (.5, .3, .2)
def_ <- defDataAdd(varname = "var_c_3m", dist = "categorical",
formula = ".5;.3;.2",
variance = "Ibuprofen;Paracetamol;Aspirin")
df <- addColumns(def_, df)
df
id period time timeID var_c_3m
1: 1 0 0 1 Aspirin
2: 1 1 19 2 Ibuprofen
3: 1 2 47 3 Aspirin
4: 1 3 66 4 Aspirin
5: 2 0 0 5 Paracetamol
6: 2 1 33 6 Paracetamol
7: 2 2 81 7 Ibuprofen
8: 2 3 126 8 Ibuprofen
9: 2 4 156 9 Paracetamol
10: 2 5 199 10 Ibuprofen
11: 2 6 254 11 Paracetamol
12: 2 7 292 12 Paracetamol
...
48: 10 0 0 48 Ibuprofen
49: 10 1 32 49 Paracetamol
50: 10 2 68 50 Aspirin
51: 10 3 94 51 Ibuprofen
52: 10 4 122 52 Paracetamol
2- 对于有序分类变量,我正在尝试根据数据帧创建它,花时间(这里)和每个变量。simstudy软件包提供了该功能,但它似乎仅适用于横截面数据,即当不像纵向数据那样重复时。df
period
id
genOrdCat()
id
根据分布或其他方法向我的数据帧添加具有类别的有序分类变量的任何解决方案将不胜感激。3
df
(.5, .3, .2)
id period time timeID var_c_3m var_ord_3m
1: 1 0 0 1 Aspirin 1
2: 1 1 19 2 Ibuprofen 1
3: 1 2 47 3 Aspirin 2
4: 1 3 66 4 Aspirin 3
5: 2 0 0 5 Paracetamol 1
6: 2 1 33 6 Paracetamol 1
7: 2 2 81 7 Ibuprofen 1
8: 2 3 126 8 Ibuprofen 2
9: 2 4 156 9 Paracetamol 2
10: 2 5 199 10 Ibuprofen 2
11: 2 6 254 11 Paracetamol 3
12: 2 7 292 12 Paracetamol 3
答: 暂无答案
上一个:在电信数据的流失预测模型中处理分类缺失数据 [已关闭]
下一个:后向差分编码器
评论