4 Replies Latest reply on Feb 16, 2016 7:02 AM by Ed Kukec

# Logistic Regression with R - Trying to compute using rollup that doesn't work

Hi All,

I have attached a basic packaged workbook where I'm trying to do a simple logistic regression using R and trying to compute/predict a probability of an emergency department admission to hospital using 4 predictor variables...age, gender, triage code (1 to 5) and a Charleston Score which assignes a numerical score to a visit based on diagnoses and comorbidities.  As you'll see...the logistic prediction works fine and predicts the probability of a person being admitted at the individual level.  You'll see this in the first worksheet "Predicted Admission by Person".  The problem is that when I try and rollup the predictions by person to the Hospital Level (ie compute using), essentially get an average probability of admission by hospital....the calculation fails and spits out the following error:

"Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels"

I've watched some videos and seen some tutorials of this being done but for some reason when I try to compute the predictions using the Hospital rollup, it fails.  Essentially, I want to take the average actual admission rate by hospital and compare it to the predicted admission rate and do some risk adjustment stuff.  But it seems to fail.  I'm scratching my head and have tried a few different things.  I even tried to do a level of detail calculation that calculates and average of the prediction using the Hospital and Individual Sequence number but it fails telling me that I essentially can't use table calculations in a level of detail calculation.

Anyway, I have attached a packaged worksheet with a small random sample of data, hoping I might get some help with this. Still relatively new with using R so experiencing the frustrations with it ;-)

Thanks,

Ed

P.S. Calling Tableau guru Jonathan Drummey or Bora Beran Bora Beran ;-)

Jonathan Drummey

• ###### 1. Re: Logistic Regression with R - Trying to compute using rollup that doesn't work

Hi Ed,

Your R script is indeed a Table Calculation that works

on a view with a granularity of the dimensions combination:

[Patient Type] - [Hospital] - [Unique Patient Visit Identifier].

One should retain the same granularity on each view

to get the results as expected -- even if a particular field,

say [Unique Patient Visit Identifier], doesn't need to be shown.

Further aggregating (averaging) the results returned from R

requires Nested Table Calculations. Moreover, a subsequent

Table Calc Filter is required to show only one Mark on a view

per Patient Type & Hospital.

Please find the attached wb.

Hope it could help.

Yours,

Yuri

• ###### 2. Re: Logistic Regression with R - Trying to compute using rollup that doesn't work

Thanks Yuri!  Still don't get the use of the If First() == 0 function but it seems to have worked. Thanks again.

Ed

• ###### 3. Re: Logistic Regression with R - Trying to compute using rollup that doesn't work

Hi Ed,

if first()==0 ... is a Table Calculation Filter (inside a calculation itself),

which effectively filters only the first row in a Table Calc Partition.

A Partition is defined by every unique value combinations

of all Dimensions on a view, which are not included in Addressing

(so they stay on the Partitioning) for a particular Table Calculation.

Hope this helps.

Yours,

Yuri

• ###### 4. Re: Logistic Regression with R - Trying to compute using rollup that doesn't work

Thanks Yuri, makes sense.  Last quick question if I may...apologies again for being a bit of a newbie with R so not sure about how to do certain things.  Here's my GLM Model Script in Tableau that Predicts Admissions based on several predictor variables.  In the script below, how do I output the results, ie odds ratios, standard errors and coefficients into an external file...say an Excel or CSV File.  I've seen the function Write in R but I've tried it a few times and can't get the syntax to work.  If you could add the necessary code below to export the results to a file called summary.csv on my C Drive, that would be great!

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