4 Replies Latest reply on Jul 4, 2018 9:23 AM by emiliano tollemeto

    Proportional hazard model with R in Tableau

    emiliano tollemeto

      Hi, I need to estimate with cox model, but theres's a problem because Tableau don't recognize the cox function,

       

       

      here's the script:

       

      SCRIPT_REAL('mydata <- data.frame(time=.arg1,b var2=.arg2, var3=.arg3, var4=.arg4, var5=.arg4);

      coxmodel <- coxph(Surv(time,var2) ~ var3 + var4 + var5, data = mydata);

      prob <- predict(coxmodel, newdata = mydata, type = "link")',

      AVG([time]), AVG([var2]),AVG([var3]),AVG([var4]),AVG([var5]))

       

      What's the mistake?

       

      another question is: if i want to estimate the model only for a part of portfolio which is the method?

       

      Thanks a lot

       

      Emiliano

        • 1. Re: Proportional hazard model with R in Tableau
          nikher verma

          Hi Emiliano tollemeto ,

           

          Yes tableau will not recognize the coxph function as its a R function in survival library  .Using coxph function like this in tableau is just like you are calling this same function without including or installing the package " survival"  in R.

           

          You will have to include this package first in your code .

           

          In case you stuck somewhere please revert with twbx file .

           

           

          -Nikher Verma

          • 2. Re: Proportional hazard model with R in Tableau
            emiliano tollemeto

            Hi Nikher,

            I've just modified the script and that's ok,

             

            SCRIPT_REAL('library(survival);

            mydata <- data.frame(time=.arg1 , var2=.arg2, var3=.arg3, var4=.arg4, var5=.arg5);

            coxmodel <- coxph(Surv(time,bad) ~ var2 + var3 + var4, data = mydata);

            prob <- predict(coxmodel, newdata = mydata, type = "lp")',

            AVG([time]), AVG([var2]),AVG([var3]),AVG([var4]),AVG([var5]))

             

            Now the issues are 2:

            1. I want to estimate the model only for a part of dataset, in particular there's a cluster variable that identify the  sample ("Training" or "Validation")

            2. In the model var3 is a numeric flag (0 or 1), what's the correct script to introduce it?

             

            Thanks a lot

            Emiliano

            • 3. Re: Proportional hazard model with R in Tableau
              nikher verma

              Hi Emiliano Tollemeto ,

               

              1. I want to estimate the model only for a part of dataset, in particular there's a cluster variable that identify the  sample ("Training" or "Validation")  :: You need to divide your data you can use seed function (in order to keep your training ,test and validation same )else every time you will refresh your tableau page your data set will change .Either you use your cluster variable or can randomly select test or validation .

               

               

              2. In the model var3 is a numeric flag (0 or 1), what's the correct script to introduce it? :: Not sure ,but it seems your data set already have a var3 .

               

              One thing you should keep in mind ,when you are integrating R and Tableau ,all your statistical calculation can be done in R and you are just returning your the output in Tableau .

               

              So you can do all your calculation in one tableau function or do it in n- different function and its your call .

               

              If possible share your twbx file with sample data .

               

               

              -Nikher Verma

              • 4. Re: Proportional hazard model with R in Tableau
                emiliano tollemeto

                Hi Nikher,

                 

                 

                The sample is already created and defined with the variable ptf ("Test" or "Controllo") in the dataset, I would select it in R script or in another way to estimate the model.

                I've just created a twbx with the csv file that contain these variables:

                time: time to event

                bad: event (0=no event, 1=event)

                ptf: to select the development sample ("Test" ) or validation ("Controllo")

                score1: input variables  (continuos)

                score2: input variables  (continuos)

                flag: input variables - 0 or 1 (if 0 it means score2= null - I have forced score2 to zero to estimate the model but I'm not sure if is correct )

                score_final= the score that I have already estimated in R (without Tableau) with the formula: 

                cox <- coxph(Surv(time,bad)~flag+scor2+score1,  data=pop.train)  where pop.train is ptf="Test"

                g_score_final: score_final grouped in deciles

                 

                I would like to create the same formula in R Script with Tableau and compare it with score_final (It could be equal!) but the problems/issues are:

                - the score in the workbook is not equal (score_final2 vs score_final);

                - why is not possible create bins in the new score, do I create groups with another R script?

                - If I would save the result and show the calculate score how should I do?

                 

                The twbx in the attachment

                Thanks a lot

                Emiliano

                 

                 

                here's the script

                SCRIPT_REAL('library(survival); library(dplyr);

                mydata <- data.frame(time=.arg1 ,bad=.arg2, flag=.arg3, score2=.arg4, score1=.arg5);

                coxmodel <- coxph(Surv(time,bad) ~ flag + score2 + score1, data = mydata);

                prob <- predict(coxmodel, newdata = mydata, type = "lp")',

                AVG([time]), AVG([bad]), AVG([flag]), AVG([score2]),AVG([score1]))