3 Replies Latest reply on Nov 7, 2016 4:18 AM by Abhishek Pandya

    Logistic Regression in R - Grouping the Probabilities by Dimension

    Ed Kukec

      Hi All,

       

      I am doing a logistic regression that calculates the probability of admission to hospital from the emergency department based on 4 Predictor variables, here's my glm r code:

       

      SCRIPT_REAL("

       

      admitstatus     <- .arg1

      age                  <- as.factor(.arg2)

      triagecode      <- as.factor(.arg3)

      cci_score        <- .arg4

      gender            <- as.factor(.arg5)

       

      fit <- glm(admitstatus ~ age + triagecode + cci_score + gender,family = binomial(logit))

      prob <- predict(fit,type = 'response')

      fit$fitted

      ",

      avg([Admit Status] ),attr([Age Group]),attr([Triagecode]), avg([Cci Score Without Age]), ATTR([Gender])

       

      )

       

      This works fine but the only issue is that the regression calculates probabilities on the entire dataset but I want it to calculate probabilities based on patient type.  I have a dimension called [Patient Type] that groups visits into two categories:

       

      1. COPD patients

      2. GI Bleed patients

       

      Is there a way to group the logistic regression using that column instead of having to create two different datasets...ie some type of subset parameter.  In SAS you can do this as:

       

      proc logistic data=RESULTS.DATA_TABLE outest=EST1;

        BY Patient Type

       

      Thanks,

      Ed

        • 1. Re: Logistic Regression in R - Grouping the Probabilities by Dimension
          Bora Beran

          Hi Ed,

          Yes. This is in fact very easy. SCRIPT_ functions are table calculations and Tableau will call the function once for each partition in your dataset.

           

          So if you click on the pill that contains this function and select Edit Table Calculation and go to Advanced... setting in the compute using drop down and make sure Patient Type is in the box that says Partitioning, you should get the results you're looking for.

           

          Thanks,

           

          Bora

          • 2. Re: Logistic Regression in R - Grouping the Probabilities by Dimension
            Ed Kukec

            Thanks a lot Bora, that seemed to work.  Another quick question if I may.  I would like to export the results, ie coefficients, standard errors and odds ratios from my logistic regression to some format such as excel but I haven't been able to find a way to do it correctly.  There is some sort of summary and write function but the syntax and how to call these functions in Tableau seems to escape me.  Here's my regression script below, was hoping you could add in the necessary bits and tell me how to call these procedures since Tableau doesn't really have an R command line to do it.  We can just call the results table "results.csv" or something and export it to the root of my C Drive.

             

            SCRIPT_REAL("

             

            admitstatus <- .arg1

            age         <- as.factor(.arg2)

            triagecode  <- as.factor(.arg3)

            cci_score   <- .arg4

            gender      <- as.factor(.arg5)

             

            fit <- glm(admitstatus ~ age + triagecode + cci_score + gender,family = binomial(logit))

            prob <- predict(fit,type = 'response')

            fit$fitted

            ",

            avg([Admit Status] ),attr([Age Group]),attr([Triagecode]), avg([Cci Score Without Age]), ATTR([Gender])

             

            )

             

            Thanks,

            Ed

            • 3. Re: Logistic Regression in R - Grouping the Probabilities by Dimension
              Abhishek Pandya

              Hi ED KUKEC,

               

              Please find the attached snap for the same, you need to Export it to Excel.

              Excel.png

               

              Thanks

              Abhi.