0

I have a R glm object and I want to evaluate the coefficient associate to a single input variable of a new data frame. Here is an example:

library(statmod)


data = tibble(X = c('a', 'b', 'c', 'd', 'e'), Z = c('a', 'c', 'c', 'c', 'a'), W = c(21:25), Y = c(0.1, 0.2, 0.3, 0.4, 0.88)) 
model = glm(Y ~ I(X %in% c('a', 'b')) + I(X %in% c('d', 'e')) + I(Z %in% c('a', 'c')) + I(log(W)), data=data, family=tweedie(var.power=1.44, link.power=0))
newdata = tibble(X = c('f', 'a'), Z = c('a', 'a'), W = c(5, 15))

I want to extract the coefficient associated to X = 'f' (which should be the based level, 0). I want to extract the coefficient associated to W = 5 (which should be log(5) * beta)

Thanks you,

John

John E.
  • 137
  • 2
  • 10
  • I'm not totally certain which values you are looking for, but the **broom** package can be pretty useful for getting output from models in a nice format. To help folks help you, consider adding info on which values from the model you want to get/what the final output would be and the packages you are using (is `tweedie()` from **statmod**?). – aosmith Jul 27 '20 at 19:37
  • It is not possible to give an output value as an example since I am looking for an universal example. This is why I used the based level of a categorical variable as an example and a continuous variable as a second example. The goal is to extract the coefficient of a given input variable. For example, if the coefficient of a "blue" house for a glm is equal to 0.5. Then if I give you the info of a new data and the house is "blue", then the output should be 0.5 regardless the other coefficients in the model. – John E. Jul 27 '20 at 22:13
  • For one I do not get ur I(Z %in% c('a', 'c')) in the model, those are all values of Z. Secondly, X = f is not a variable in the original model. You can not extract a value that does not exist. If I make a regression model with X = males or females, I can not get a parameter value for hermaphrodites. – Baraliuh Jul 27 '20 at 22:22
  • still unclear to me. Are you trying to **predict** (try the `predict()` method, if there is one)? Or calculate **expected marginal means** (see the `emmeans` package) – Ben Bolker Jul 27 '20 at 22:35
  • @Baraliuh the I(*) function is an indicator that tells predict(*) if the condition is TRUE or FALSE. If the condition is FALSE (X = f) it will give the base level (reference level) (this is the behavior I want to have). What we cannot do is to predict(*) without all the input variable in the new dataset, because predict(*) doesn't know what to do with the missing input variable. – John E. Jul 28 '20 at 00:26

0 Answers0