TimeSplitter:eval中的错误(替换(退出),data,parent.frame()):找不到对象'Age'

问题描述 投票:0回答:1

我是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中的错误(替换(退出),data,parent.frame()):找不到对象'Age'另外:警告消息:在max(data [[time_var]])中:没有非缺少的参数max;返回-Inf

我的数据库中有一个“Age”列,它是数字的。我无法理解这里出了什么问题。

r dplyr survival-analysis
1个回答
0
投票

问题是event_var变量需要是因子(或字符)类。该示例也存在问题,因为它只有一个级别用于状态变量。如果你允许一些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]])

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

© www.soinside.com 2019 - 2024. All rights reserved.