简体   繁体   English

类似 function 到 R 的对数和 function(mlogit 包)用于生存 package

[英]Similar function to R's logsum function (mlogit package) for Survival package

I was trying to obtain the expected utility for each individual using R's survival package ( clogit function) and I was not able to find a simple solution such as mlogit's logsum .我试图使用 R 的survival package ( clogit函数)获得每个人的预期效用,但我无法找到一个简单的解决方案,例如 mlogit's logsum

Below I set an example of how one would do it using the mlogit package.下面我设置了一个示例,说明如何使用mlogit package 进行操作。 It is pretty straight forward: it just requires regressing the variables with the mlogit function, save the output and use it as an argument in the logsum function -- if needed, there is a short explanation in this vignette .这很简单:它只需要使用mlogit function 对变量进行回归,保存 output 并将其用作logsum ZC1C425268E68385D1AB5074C17A94F 中的参数,如果需要,请在此--v14Zignette中进行简短解释。 And what I want is to know the similar method for clogit .我想知道clogit的类似方法。 I've read the package's manual but I have failed to grasp what would be the most adequate function to perform the analsysis.我已经阅读了包装 手册,但我没有掌握最适合执行分析的 function。

Note1: My preference for a function like mlogit's is related to the fact that I might need to perform tons of regressions later on and being able to perform the correct estimation in different scenarios would be helpful.注意 1:我对 mlogit 之类mlogit's偏好与我以后可能需要执行大量回归并且能够在不同场景中执行正确估计会有所帮助的事实有关。

Note2: I do not intend that the dataset created below be representative of how data should behave.注意 2:我不打算让下面创建的数据集代表数据的行为方式。 I've set the example solely for the purpose of perfoming the function after the logit regressions.我设置示例仅是为了在 logit 回归后执行 function。

** **

library(survival)
library(mlogit)

#creating a dataset

df_test=data.frame(id=rep(1:20,each=4),
                   choice=rep(c("train","car","plane","boat")),
                   distance=c(rnorm(80)*10),
                   )

f=function(x,y,z) {
    
  v=round(rnorm(x,y,z))
    
    while(sum(v)>1 | sum(v)==0) {
      
      v=round(rnorm(x,y,z))
      
    }
  
return(v)
    
}

result1=c()

for (i in 1:20) {
  
  result=f(4,0.5,0.1)
  
  result1=c(result,result1)
  
}

df_test$distance=ifelse(df_test$distance<0,df_test$distance*-1,df_test$distance)
df_test$price = 0
df_test$price[df_test$choice=="plane"] = rnorm(20, mean = 300, sd=30)
df_test$price[df_test$choice=="car"] = rnorm(20, mean = 50, sd=10)
df_test$price[df_test$choice=="boat"] = rnorm(20, mean = 100, sd=15)
df_test$price[df_test$choice=="train"] = rnorm(20, mean = 120, sd=25)

df_test$choice2=result1
           
mlog=mlogit(choice2 ~ distance + price , data = df_test)

#the function logsum generates expected utility for each individual

logsum(mlog)

#so what would be adequate alternative with survival's clogit? I set an exemple below of
#of what i would like to regress and then perform something like logsum()

clog=clogit(choice2 ~ distance + price + as.factor(choice), strata(id), data = df_test)

** **

The vignette you offer says the logsum is calculated as:您提供的小插图说对数和计算如下:

在此处输入图像描述

To my reading that is similar to the calculation used to construct the "linear predictor".在我看来,这类似于用于构建“线性预测器”的计算。 the lp is t(coef(clog)) %*% Xhat . lp 是t(coef(clog)) %*% Xhat If I'm correct on that interpretation, then that is stored in the clog -object:如果我的解释是正确的,那么它存储在clog -object 中:

 clog["linear.predictor"]

So you could just take:所以你可以采取:

log(sum( exp(clog[["linear.predictors"]]) ))
[1] 4.286592

If you need to segregate this by df_test$choice then (since the length of the 'linear.predictors' element of clog is the same as the length of the 'choice' column of df_test ) it's just:`如果您需要通过df_test$choice将其隔离(因为clog的 'linear.predictors' 元素的长度与df_test的 'choice' 列的长度相同)它只是:`

 tapply(clog$linear.predictors, df_test$choice, function(x){ 
               log(sum( exp(x) ))})
 #--------------------------------------
    boat      car    plane    train 
2.824502 3.506756 2.825004 1.734258 

If you need to aggregate this by id , you would do it like this:如果您需要按id聚合它,您可以这样做:

tapply(clog$linear.predictors, df_test$id, function(x){ 
  log(sum( exp(x) ))})

       1        2        3        4        5        6        7        8        9 
1.405896 1.506152 1.454507 1.539060 1.467082 1.428482 1.393582 1.521877 1.480670 
      10       11       12       13       14       15       16       17       18 
1.466490 1.416338 1.500581 1.528075 1.488253 1.398405 1.445014 1.483623 1.404041 
      19       20 
1.460672 1.452709 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM