0 Replies Latest reply on Nov 28, 2018 9:53 AM by Adam Yansick

    R Linear Model - Unexpected Prediction Behavior

    Adam Yansick

      Workbook with dummy data attached. Using Tableau 2018

       

      In the sheet, there is a calculated field called Predicted Impressions that has the following code

       

      SCRIPT_REAL("

      dat <- data.frame(cbind(cost = .arg1, impressions = .arg2))

      model <- lm(impressions ~ cost, data = dat)

      model$fit

      ",

      SUM([Cost]),

      SUM([Impressions])

      )

       

      When run in R, I produce expected results (this is just randomly generated data, so it won't make actually make sense). When I run this code in Tableau, I run into an issue I can't puzzle out:

       

      The aggregated view shows the same number for Impressions and Predicted Impressions. This makes sense (and the number matches the sum found in R via sum(model$fit). However, when I dis-aggregate the data, I am left with a very curious output (I expanded the columns to exaggerate the issue):

      Why does the view show up like this? I expected an output that aligns one predicted value per one actual value and one explanatory variable. Even if I shrink the columns to be only width 1 each:

      The predicted values do not match up to the values in the R output (which generates what I want to see). For instance, in R the impressions of 7314 has a predicted value of 1528.449 and a cost of 7.8871631. 

       

      Can anyone explain this behavior to me and if there is something wrong with my code that does not produce the expected output?