R TraMinerR - 为什么 seqformat 抛出错误?
R TraMinerR - why is seqformat throwing error?
我是 TraMineR 的新手,但我正在尝试检查患者随时间推移用于临床就诊的模式序列。
设置将其从 SPELL 格式转换为 STS 格式后,我的数据如下所示。这里可以看到begin和end变量都是整型变量
> df %>% head(20)
# A tibble: 20 x 6
id index begin end status status_1
<int> <int> <int> <int> <fct> <int>
1 1 1 1 1 Video 3
2 1 2 2 2 Video 3
3 2 1 1 1 Phone 2
4 2 2 2 2 Phone 2
5 2 3 3 3 Phone 2
6 3 1 1 1 Video 3
7 4 1 1 1 Video 3
8 5 1 1 1 Phone 2
9 6 1 1 1 Video 3
10 6 2 2 2 Video 3
11 6 3 3 3 Video 3
12 6 4 4 4 Video 3
13 6 5 5 5 Video 3
14 7 1 1 1 Phone 2
15 7 2 2 2 Phone 2
16 8 1 1 1 Video 3
17 9 1 1 1 Phone 2
18 10 1 1 1 Phone 2
19 10 2 2 2 Phone 2
20 10 3 3 3 InPerson 1
使用 skimr 中的 skim() 快速查看,我们还可以看到变量类型并且没有丢失数据。
> df %>% skimr::skim()
-- Data Summary ------------------------
Values
Name Piped data
Number of rows 4530
Number of columns 6
_______________________
Column type frequency:
factor 1
numeric 5
________________________
Group variables None
-- Variable type: factor ----------------------------------------------------------------------------------------------------------------------------------------------
# A tibble: 1 x 6
skim_variable n_missing complete_rate ordered n_unique top_counts
* <chr> <int> <dbl> <lgl> <int> <chr>
1 status 0 1 FALSE 3 Pho: 2496, Vid: 1864, InP: 170
-- Variable type: numeric ---------------------------------------------------------------------------------------------------------------------------------------------
# A tibble: 5 x 11
skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
* <chr> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 id 0 1 1203. 702. 1 584 1207 1817 2426 ▇▇▇▇▇
2 index 0 1 1.86 1.24 1 1 1 2 11 ▇▁▁▁▁
3 begin 0 1 1.86 1.24 1 1 1 2 11 ▇▁▁▁▁
4 end 0 1 1.86 1.24 1 1 1 2 11 ▇▁▁▁▁
5 status_1 0 1 2.37 0.556 1 2 2 3 3 ▁▁▇▁▆
但是,当我尝试使用 seqformat 将我的数据从 SPELL 转换为状态序列时,使用以下代码:
df_sts <- seqformat(df, id = "id", begin = "begin", end = "end", status = "status_1", from = "SPELL", to = "STS", process = FALSE)
我收到这个错误:
Error in is.wholenumber(c(endcolumn, begincolumn)) :
'list' object cannot be coerced to type 'integer'
我正在尝试按照 TraMineR 用户指南中概述的步骤进行操作,但我真的不确定这个错误是从哪里来的,因为开始和结束变量都是整数...有人可以帮助我理解什么问题出在这里,如何解决“错误”?
TraMineR
似乎不能很好地处理 tibbles。将数据声明为数据框应该可以解决问题。
df2 <- as.data.frame(df)
我检查了TraMineR的代码,找到了错误的原因。 Tibbles 和数据框在提取单个变量时表现不同。当使用 data.frame 执行此操作时,我们获得一个向量,在 tibble 的情况下,提取的列仍然是 class tibble。
library(tidyverse)
df <- tribble(
~id, ~index, ~begin, ~end, ~status, ~status_1,
1, 1, 1, 1, "Video", 3,
1, 2, 2, 2, "Video", 3,
2, 1, 1, 1, "Phone", 2,
2, 2, 2, 2, "Phone", 2,
2, 3, 3, 3, "Phone", 2,
3, 1, 1, 1, "Video", 3,
4, 1, 1, 1, "Video", 3,
5, 1, 1, 1, "Phone", 2,
6, 1, 1, 1, "Video", 3,
6, 2, 2, 2, "Video", 3,
6, 3, 3, 3, "Video", 3,
6, 4, 4, 4, "Video", 3,
6, 5, 5, 5, "Video", 3) |>
mutate(status = factor(status))
# Subsetting a tibble
c(df[,3],df[,4])
$begin
[1] 1 2 1 2 3 1 1 1 1 2 3 4 5
$end
[1] 1 2 1 2 3 1 1 1 1 2 3 4 5
# Subsetting a data.frame
c(df2[,3],df2[,4])
[1] 1 2 1 2 3 1 1 1 1 2 3 4 5 1 2 1 2 3 1 1 1 1 2 3 4 5
is.wholenumber <- function(x){as.integer(x) == x}
# tibble --> error
all(is.wholenumber(c(df[,3],df[,4])))
Error in is.wholenumber(c(df[, 3], df[, 4])) :
'list' object cannot be coerced to type 'integer'
# data.frame -> works as expected
all(is.wholenumber(c(df2[,3],df2[,4])))
[1] TRUE
我是 TraMineR 的新手,但我正在尝试检查患者随时间推移用于临床就诊的模式序列。
设置将其从 SPELL 格式转换为 STS 格式后,我的数据如下所示。这里可以看到begin和end变量都是整型变量
> df %>% head(20)
# A tibble: 20 x 6
id index begin end status status_1
<int> <int> <int> <int> <fct> <int>
1 1 1 1 1 Video 3
2 1 2 2 2 Video 3
3 2 1 1 1 Phone 2
4 2 2 2 2 Phone 2
5 2 3 3 3 Phone 2
6 3 1 1 1 Video 3
7 4 1 1 1 Video 3
8 5 1 1 1 Phone 2
9 6 1 1 1 Video 3
10 6 2 2 2 Video 3
11 6 3 3 3 Video 3
12 6 4 4 4 Video 3
13 6 5 5 5 Video 3
14 7 1 1 1 Phone 2
15 7 2 2 2 Phone 2
16 8 1 1 1 Video 3
17 9 1 1 1 Phone 2
18 10 1 1 1 Phone 2
19 10 2 2 2 Phone 2
20 10 3 3 3 InPerson 1
使用 skimr 中的 skim() 快速查看,我们还可以看到变量类型并且没有丢失数据。
> df %>% skimr::skim()
-- Data Summary ------------------------
Values
Name Piped data
Number of rows 4530
Number of columns 6
_______________________
Column type frequency:
factor 1
numeric 5
________________________
Group variables None
-- Variable type: factor ----------------------------------------------------------------------------------------------------------------------------------------------
# A tibble: 1 x 6
skim_variable n_missing complete_rate ordered n_unique top_counts
* <chr> <int> <dbl> <lgl> <int> <chr>
1 status 0 1 FALSE 3 Pho: 2496, Vid: 1864, InP: 170
-- Variable type: numeric ---------------------------------------------------------------------------------------------------------------------------------------------
# A tibble: 5 x 11
skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
* <chr> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 id 0 1 1203. 702. 1 584 1207 1817 2426 ▇▇▇▇▇
2 index 0 1 1.86 1.24 1 1 1 2 11 ▇▁▁▁▁
3 begin 0 1 1.86 1.24 1 1 1 2 11 ▇▁▁▁▁
4 end 0 1 1.86 1.24 1 1 1 2 11 ▇▁▁▁▁
5 status_1 0 1 2.37 0.556 1 2 2 3 3 ▁▁▇▁▆
但是,当我尝试使用 seqformat 将我的数据从 SPELL 转换为状态序列时,使用以下代码:
df_sts <- seqformat(df, id = "id", begin = "begin", end = "end", status = "status_1", from = "SPELL", to = "STS", process = FALSE)
我收到这个错误:
Error in is.wholenumber(c(endcolumn, begincolumn)) :
'list' object cannot be coerced to type 'integer'
我正在尝试按照 TraMineR 用户指南中概述的步骤进行操作,但我真的不确定这个错误是从哪里来的,因为开始和结束变量都是整数...有人可以帮助我理解什么问题出在这里,如何解决“错误”?
TraMineR
似乎不能很好地处理 tibbles。将数据声明为数据框应该可以解决问题。
df2 <- as.data.frame(df)
我检查了TraMineR的代码,找到了错误的原因。 Tibbles 和数据框在提取单个变量时表现不同。当使用 data.frame 执行此操作时,我们获得一个向量,在 tibble 的情况下,提取的列仍然是 class tibble。
library(tidyverse)
df <- tribble(
~id, ~index, ~begin, ~end, ~status, ~status_1,
1, 1, 1, 1, "Video", 3,
1, 2, 2, 2, "Video", 3,
2, 1, 1, 1, "Phone", 2,
2, 2, 2, 2, "Phone", 2,
2, 3, 3, 3, "Phone", 2,
3, 1, 1, 1, "Video", 3,
4, 1, 1, 1, "Video", 3,
5, 1, 1, 1, "Phone", 2,
6, 1, 1, 1, "Video", 3,
6, 2, 2, 2, "Video", 3,
6, 3, 3, 3, "Video", 3,
6, 4, 4, 4, "Video", 3,
6, 5, 5, 5, "Video", 3) |>
mutate(status = factor(status))
# Subsetting a tibble
c(df[,3],df[,4])
$begin
[1] 1 2 1 2 3 1 1 1 1 2 3 4 5
$end
[1] 1 2 1 2 3 1 1 1 1 2 3 4 5
# Subsetting a data.frame
c(df2[,3],df2[,4])
[1] 1 2 1 2 3 1 1 1 1 2 3 4 5 1 2 1 2 3 1 1 1 1 2 3 4 5
is.wholenumber <- function(x){as.integer(x) == x}
# tibble --> error
all(is.wholenumber(c(df[,3],df[,4])))
Error in is.wholenumber(c(df[, 3], df[, 4])) :
'list' object cannot be coerced to type 'integer'
# data.frame -> works as expected
all(is.wholenumber(c(df2[,3],df2[,4])))
[1] TRUE