我们需要解决三个问题才能将这些数据与TraMineR
.
时间必须谨慎,因为它用于确定谨慎序列中的位置或位置之间的差异。这里的一个解决方案是将小时转换为刻钟。
提供的唯一时间信息是服务时间,即持续时间。我们需要额外的信息(或假设)来将这些持续时间转换为开始和结束时间。我将假设从时间 1 开始观察每个人(id)并且服务时间是连续的。因此,第一个法术的开始时间将为 1,第二个法术的开始时间为 1 加上第一个法术的持续时间,依此类推。结束时间将是第一个法术的法术持续时间,前一个结束时间加上下一个法术的法术持续时间。
有三个分类变量,不清楚应该使用什么作为状态变量。我将假设状态是程序领域和组织编号之间的交互。
下面的代码说明了这些转换:
library(TraMineR)
dat <- read.table(header=TRUE, text="
ID Program.Area Impact.Area Hours.Served Organization.Served x
1 Tutoring Education 2 org 1
1 Hunger Basic.Needs .25 org 2
1 Gardening Beautification 1 org 3
2 Tutoring Education 2 org 4
3 Hunger Basic.Needs 3 org 2
3 Hunger Basic.Needs 1 org 2
4 Tutoring Education 1.5 org 1
4 Tutoring Education 1.5 org 1
4 Tutoring Education 2 org 4
5 Hunger Basic.Needs 1 org 2
5 Hunger Basic.Needs 1 org 5
")
需要谨慎的时间
dat[,4] <- 4*dat[,4]
names(dat)[4] <- "Quarter.Hours.Served"
计算开始时间和结束时间假设Hours.Served
是连续的,并且第一个法术从 1 开始。
k <- ncol(dat) + 1
dat[,k] <- 1
dat[,k+1] <- dat[,4]
names(dat)[k] <- "Begin"
names(dat)[k+1] <- "End"
for (i in 2:nrow(dat)) {
if (dat[i-1,1]==dat[i,1]) {
dat[i,k] <- dat[i-1,k+1] + 1
dat[i,k+1] <- dat[i,4] + dat[i-1,k+1]
}
}
程序领域和组织编号之间的交互状态
dat[,k+2] <- interaction(dat[,2],dat[,"x"])
names(dat)[k+2] <- "Status"
dat[,c(1,k,k+1,k+2)]
# ID Begin End Status
# 1 1 1 8 Tutoring.1
# 2 1 9 9 Hunger.2
# 3 1 10 13 Gardening.3
# 4 2 1 8 Tutoring.4
# 5 3 1 12 Hunger.2
# 6 3 13 16 Hunger.2
# 7 4 1 6 Tutoring.1
# 8 4 7 12 Tutoring.1
# 9 4 13 20 Tutoring.4
# 10 5 1 4 Hunger.2
# 11 5 5 8 Hunger.5
将拼写数据转换为 STS 形式并创建状态序列对象
s.dat <- seqformat(dat[,c(1,k,k+1,k+2)], from="SPELL", to="STS",
limit=max(dat[,k+1]))
seq <- seqdef(s.dat, cnames=1:20)
print(seq, format="SPS")
# Sequence
# 1 (Tutoring.1,8)-(Hunger.2,1)-(Gardening.3,4)
# 2 (Tutoring.4,8)
# 3 (Hunger.2,16)
# 4 (Tutoring.1,12)-(Tutoring.4,8)
# 5 (Hunger.2,4)-(Hunger.5,4)
seqiplot(seq)
