Skip to content

Instantly share code, notes, and snippets.

@samuelleach
Created March 3, 2014 06:57
Show Gist options
  • Save samuelleach/9319739 to your computer and use it in GitHub Desktop.
Save samuelleach/9319739 to your computer and use it in GitHub Desktop.
Tableau calculated field using R to predict values using a polynomial fit of degree two. Data may contain zeroes which are excluded from the fit, but are predicted by the model.
SCRIPT_REAL("
xdata <- .arg1
ydata <- .arg2
indices <- which( abs(ydata) < 0.01 )
ndata <- length( ydata )
weights <- rep(1, ndata)
weights[indices] = 0
model <- lm( ydata ~ xdata + I(xdata^2), weights=weights )
model$fitted
",
SUM( [year] ), SUM( [13-17] ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment