Hi
I am trying to retrieve the coefficients from a linear regression analysis, in order to use them further down the workflow. I see a lot of references here on the site to use a "Model Coefficients" macro, but when I try to import it, Alteryx (I'm using 11.5.1.31573) doesn't recognize it as a macro, and wants to open it as a workflow.
I've also tried to use the parsing tool, to see if i could get that to work, also with no luck.
So - how do I get my hand on the coeffcients from the linear regression tool, in order to use them "manually" down stream?
Solved! Go to Solution.
Hi @MadsS,
Thank you for posting!
If I understand your issue correctly, once you have downloaded the macro from Alteryx Gallery (https://gallery.alteryx.com/#!app/Model-Coefficients/56bcd989a248970ce0ba08be), you need to insert it in the canvas in Alteryx Designer (right click, Insert, Macro -at the bottom- Browse and look for Model Coefficients.yxmc).
Kind regards,
Paul Noirel
Sr Customer Support Engineer, Alteryx
Input the model object to a R tool and use the following code to extract the model variables and their coefficients.
# Read in the Alteryx stream containing the model object the.model <- read.Alteryx("#1") # Make sure that it is in fact a model object, and return an error if it is not if (!all(names(the.model) %in% c("Name", "Object"))) stop.Alteryx("A model object was not provided") # The model object has been serialized as a string, which on being read from Alteryx is # converted to an R factor. The line of code first coerces the serialized model string to # a string (in R a "character" data type) and the unserialized the model object mod.obj <- unserializeObject(as.character(the.model$Object[1])) # From the first test error check we know the Alteryx stream contained a model, but not # all models have coefficients, so test to see if there are model coefficients, and throw an # error otherwise if (!is.null(mod.obj$coefficients)) { the.coefs <- mod.obj$coefficients write.Alteryx(data.frame(Variable = names(the.coefs), Coefficient = the.coefs)) } else { stop.Alteryx(paste("A model of class", class(mod.obj)[1], "does not have coefficients")) }
I will also post the message that DrDan posted with this code:
This code and the corresponding macro will work for the Linear, Logistic, Count, and Gamma Regression tools. However, if the objective is to calculate predicted values from the model, then extracting the coefficients and using them in a formula tool (or in some other tool) is strongly discouraged. The reasons for this are: (1) the process of translating a set of model coefficients to a formula is often prone to serious error (particularly when categorical variables are used) and (2) doing this is likely to be a waste of time on the part of the user since the Score tool will accurately calculate fitted values at the very small cost of dragging one additional tool onto the canvas.
It turns out you can expose anything related to an R model object to Alteryx using an R tool, whether that is needed, or is even a good choice, is another matter.
This worked for me, thanks :-)
Hi,
The prior discussion was very helpful for a problem I am working to solve. Would be possible to extend the Model Coefficients tool technique outlined in the R code for extracting the coefficients to accomplish task below.
I am using the logistic regression tool in a workflow but the data set is very sparse so I am using a sampling technique called case-control sampling. It essentially uses all the positive cases from the data set and a reduced sample of the negative cases in the data set. In order to get the final model after using from a case-control logistic regression I need to take the resulting model and make a manual adjustment to the intercept term.
I can read the model coefficients from the Object output as with the Model Coefficients tool. Then I would like to make the "manual" change to the intercept term and reconstruct the model as an object that can be used later in the workflow with tools such as the Scoring tool.
Any ideas you might have would be really great.
Thanks!
Greg
@CharlieS you're a lifesaver. Thanks a lot!
Hi @gregnolder @MadsS @jeanfgomess1
Thanks for your continued support for Alteryx. To empower users to extract and explain their model variable coefficients, we recently published a new set of model explanation macros: LIME & SHAP here.
It supports both Regression & Classification predictive tools including SVM, Decision Tree, Linear Regression, etc. Give a try and hope it could give you a boost!