我想先通过对组(下面代码中的
对于特定用户的选择
用户
)进行计算来计算选择切换概率。 然后,我将平均组级概率并得到总概率。 我有数万个组,因此需要快速的代码。 我的代码是一个for循环
,需要超过10分钟才能运行。 我用相同的代码/逻辑在Excel中进行操作,只需要几秒钟。对于特定用户的选择
m到n的切换
被定义为选择在期间t-1时是m
且在期间t时是n的观测占比
。
我的原始代码首先通过for循环标记第一次和最后一次购买。然后使用另一个for循环获取切换矩阵。 我只能通过整个数据而不是通过组创建切换矩阵。 即使这样,它仍然非常缓慢。 添加用户将使其变得更慢。 t<-c(1,2,1,1,2,3,4,5)
user<-c('A','A','B' ,'C','C','C','C','C')
choice<-c(1,1,2,1,2,1,3,3)
dt<-data.frame(t,user,choice)
t user choice
1 A 1
2 A 1
1 B 2
1 C 1
2 C 2
3 C 1
4 C 3
5 C 3
# **step one** create a second choice column for later construction of the switching matrix
#Label first purchase and last purchase is zero
for (i in 1:nrow(dt))
{ ifelse (dt$user[i+1]==dt$user[i],dt$newcol[i+1]<-0,dt$newcol[i+1]<-1) }
# **step two** create stitching matrix
# switching.m is a empty matrix with the size of total chocie:3x3 here
length(unique(dt$user))
total.choice<-3
switching.m<-matrix(0,nrow=total.choice,ncol=total.choice)
for (i in 1:total.choice)
{
for(j in 1:total.choice)
{
if(length(nrow(switching.m[switching.m[,1]==i& switching.m[,2]==j,])!=0))
{switching.m[i,j]=nrow(dt[dt[,1]==i&dt[,2]==j,])}
else {switching.m[i,j]<0}
}
}
特定用户/组的期望输出应如下。即使用户没有作出特定选择,输出也应具有相同的矩阵大小。
# take user C
#output for switching matrix
second choice
first 1 2 3
1 0 1 1
2 1 0 0
3 0 0 1
#output for switching probability
second choice
first 1 2 3
1 0 0.5 0.5
2 1 0 0
3 0 0 1
lapply(split(dt$choice,dt$user),function(x) table(x[-length(x)],x[-1]))
。 - nicola