TimeSplitter:eval(替代(退出),数据,parent.frame())中的错误:找不到对象“年龄”

时间:2019-03-22 13:02:13

标签: r dplyr survival-analysis

我是R新手。如果您能解决我的问题,我将不胜感激。

这是我的代码:

dat1 <- read.csv("data.csv",header=T)
spl_dat1 <-
     dat1 %>%
     timeSplitter(by = 5,
                  time_var = "Age",
                  event_var = "Alive",
                  event_start_status = "1",
                  time_related_vars = c("Born", "Death"))

这是我的数据库图片: enter image description here

这是我的dput(head(dat1))

Born = c(1949L,1949L, 1949L, 1949L, 1949L, 1949L), 
Death = c(1970L, 1954L, 1954L,1954L, 1954L, 1968L), 
Age = c(22, 6, 6, 6, 6, 20), 
Alive = c(0L, 0L, 0L, 0L, 0L, 0L), 
Type = structure(c(3L, 5L, 5L, 5L, 5L, 5L), 
Label = c("AdministrativeOffices", "DepartmentsDSC", "GeneralOffice", 
"InstitutionsDSC", "Ministries", "NationalBureausAMC"), class = "factor"),
GeneralOffice = c(1L, 0L, 0L, 0L, 0L, 0L), 
Ministries = c(0L, 1L, 1L, 1L, 1L, 1L), 
DepartmentsDSC = c(0L, 0L, 0L, 0L, 0L, 0L), 
AdministrativeOffices = c(0L, 0L, 0L, 0L, 0L, 0L), 
NationalBureausAMC = c(0L, 0L, 0L, 0L, 0L, 0L), 
InstitutionsDSC = c(0L, 0L, 0L, 0L, 0L, 0L), 
Law = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("CentralCommitteeoftheCPC", "NationalPeoplesCongress", "NPCStandingCommittee", "StateCouncilMeeting"), class = "factor"),
 NationalPeoplesCongress = c(1L, 1L, 1L, 1L, 1L, 1L), 
NPCStandingCommittee = c(0L, 0L, 0L, 0L, 0L, 0L),
 CentralCommitteeoftheCPC = c(0L, 0L, 0L, 0L, 0L, 0L),
 StateCouncilMeeting = c(0L, 0L, 0L, 0L, 0L, 0L),
 Function = structure(c(3L, 3L, 4L, 3L, 3L, 3L), .Label = c("EconomicManagement", "EnforcementSupervision", "GovernmentOffices", "MacroRegulation", "SocialAffairs"), class = "factor")

这是structure(dat1)

Name Born Death Age AgeGroup Alive       Type   
1    1949  1970  22        2     0 Ministries                                            
2    1949  1954   6        1     0 Ministries          
3    1949  1954   6        1     0 Ministries   
4    1949  1954   6        1     0 Ministries   
5    1949  1954   6        1     0 Ministries
6    1949  1968  20        2     0 Ministries
7    1949  2018  70        3     1 Ministries
8    1949  2018  70        3     1 Ministries 
9    1949  1959  11        2     0 Ministries 
10   1949  2018  70        3     1 Ministries
11   1949  1952   4        1     0 Ministries 

但是当我运行代码时,发生了错误。

  

eval(替换(退出),数据,parent.frame())中的错误:     找不到对象“年龄”   另外:警告消息:   在max(data [[time_var]])中:没有max的必填参数;返回-Inf

我的数据库中有一个“年龄”列,它是数字。我不明白这里出了什么问题。

1 个答案:

答案 0 :(得分:0)

问题在于event_var变量需要是factor(或character)类。该示例还有一个问题,因为该状态变量只有一个级别。如果您允许某些Alive向量为1并使其成为一个因子,则没有错误。显然,这是Greg软件包文档中的遗漏。小插图示例未提及此要求,但不会出错,因为data.frame的默认设置是将字符值作为因素,并且示例使用字符向量。

dat1 <- data.frame(

Born = c(1949L,1949L, 1949L, 1949L, 1949L, 1949L), 
Death = c(1970L, 1954L, 1954L,1954L, 1954L, 1968L), 
Age = c(22, 6, 6, 6, 6, 20), 
Alive = factor( c(0L, 0L, 0L, 1L, 0L, 1L)), 
Type = structure(c(3L, 5L, 5L, 5L, 5L, 5L), 
                 Label = c("AdministrativeOffices", "DepartmentsDSC", "GeneralOffice", 
                           "InstitutionsDSC", "Ministries", "NationalBureausAMC"), class = "factor"),
GeneralOffice = c(1L, 0L, 0L, 0L, 0L, 0L), 
Ministries = c(0L, 1L, 1L, 1L, 1L, 1L), 
DepartmentsDSC = c(0L, 0L, 0L, 0L, 0L, 0L), 
AdministrativeOffices = c(0L, 0L, 0L, 0L, 0L, 0L), 
NationalBureausAMC = c(0L, 0L, 0L, 0L, 0L, 0L), 
InstitutionsDSC = c(0L, 0L, 0L, 0L, 0L, 0L), 
Law = structure(c(2L, 2L, 2L, 2L, 2L, 2L), .Label = c("CentralCommitteeoftheCPC", "NationalPeoplesCongress", "NPCStandingCommittee", "StateCouncilMeeting"), class = "factor"),
NationalPeoplesCongress = c(1L, 1L, 1L, 1L, 1L, 1L), 
NPCStandingCommittee = c(0L, 0L, 0L, 0L, 0L, 0L),
CentralCommitteeoftheCPC = c(0L, 0L, 0L, 0L, 0L, 0L),
StateCouncilMeeting = c(0L, 0L, 0L, 0L, 0L, 0L),
Function = structure(c(3L, 3L, 4L, 3L, 3L, 3L), .Label = c("EconomicManagement", "EnforcementSupervision", "GovernmentOffices", "MacroRegulation", "SocialAffairs"), class = "factor"))

spl_dat1 <-
    dat1 %>%
    timeSplitter(by = 5,
                 time_var = "Age",
                 event_var = "Alive",
                 event_start_status = "1",
                 time_related_vars = c("Born", "Death"))

没有错误。该代码仅强制字符变量使用以下代码进行处理:

if (is.character(data[[event_var]])) 
    data[[event_var]] <- factor(data[[event_var]])

...但是不对数字或整数分类变量执行所需的强制。