0

I have a project to estimate commercial real estate properties given categorical and continuous variables. I have ran a step-wise linear regression model to pick out a good formula in RStudio. (Adj. Rsq = .90 also, I know I need to do PCA and some type of categorical ANOVA test still, but I just want to get a beta estimator out before going deeper.)

How do I take the resulting output from my step(lm()) function and create an character based algebraic expression/equation with the coefficients, like:

  • log(price)= M1X1 + M2X2 + ... + MnXn.

Where M is my coefficient, and X is my variable. I know I could do it by hand in excel but with so many interactions and base variables it seems excessive. Maybe there is a function in R, or I could make a function in python that would ask what are the values for the variables needed for the calculation; but I haven't thought of any.

Thank you so much! If anything is not specific enough I will do my best to further explain.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • When asking for coding help you should provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data that we can use to test possible solutions. Also, you should really only ask at most one question at a time because you can only accept at most one answer. Finally, your second question is really more about statistical modeling than programming so it's a better fit for [stats.se] (the site dedicated to statistical questions) than here. For the first part, have you looked at `coef()`? – MrFlick Mar 31 '17 at 02:36
  • 2
    `lm` allows transformed variables, e.g. `lm(log(demand) ~ I(Time^2), BOD)`. Use `predict` to get the predicted values given new values of the predictors. – G. Grothendieck Mar 31 '17 at 03:07
  • @MrFlick Thank you for the critics on developing correct questions, I'll be sure to use your advice (I was actually directed here after first posting this question in [Cross Validated](http://stats.stackexchange.com/). – S_Stand_ring Mar 31 '17 at 15:38
  • As for `coef()` it does give me the coefficients, but I am looking more for an output that returns a complete expression. Like in [this comment](http://stats.stackexchange.com/questions/63600/how-to-translate-the-results-from-lm-to-an-equation) "Given the OP's mention of a wish to put equations on graphs, I've been pondering whether they actually want a function to take the output of lm and produce a character expression like "y^=−0.00761+0.09156x " suitable for such a plotting task (hence my repeated call to clarify what they wanted - which hasn't been done, unfortunately)." by @Glen_b – S_Stand_ring Mar 31 '17 at 15:38
  • @G.Grothendieck After looking more into `predict` I don't believe it is what I am looking for since we are not given the model's character equation. On a side note, `predict` looks extremely useful! Is it most commonly used to test new data on a model to ensure the model is not too fitted? – S_Stand_ring Mar 31 '17 at 15:54

1 Answers1

0

As far as I know there is no built in function to do this (formulas can get quite messy for different model types). But if you have a simple linear model, perhaps this function can help

as_disp_eqn <- function(model, formatter=prettyNum, ...) {
    model_terms <- terms(model)
    model_response <- deparse(as.list(attr(model_terms, "variables")[-1])[[attr(model_terms, "response")]])
    coefs <- coef(model)
    rhs <- paste0(formatter(coefs, ...), ifelse(names(coefs)!="(Intercept)", paste(" *", names(coefs)),""))
    paste(model_response, "=", paste(rhs, collapse=" + "))
}

You can pass in a model and get a printed display. For example

mm <- lm(formula = Sepal.Length ~ Sepal.Width + Petal.Length + Species, data = iris)
as_disp_eqn(mm, digits=2)
# [1] "Sepal.Length = 2.4 + 0.43 * Sepal.Width + 0.78 * Petal.Length + -0.96 * Speciesversicolor + -1.4 * Speciesvirginica"
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • This is great. I'm not sure if my model is "simple" but this is actually what I'm looking for. I'm sure I can figure out a way to adapt it if it doesn't. Thank you for a young knowledge seeker. – S_Stand_ring Mar 31 '17 at 19:18