Is it possible to use package of effects in R for lm models without hooks?
Is it possible to use a package of effects in R to build (marginal) effects for a linear model that does not include interception?
Here's the R code I've tried for such a model:
x <- seq(1,100,by=0.1)
y <- 1 + 2*x + rnorm(n=length(x))
model <- lm(y ~ 0 + x)
require(effects)
plot(allEffects(model))
The error it produces is as follows:
Error in plot(allEffects(model)) :
error in evaluating the argument 'x' in selecting a method for function 'plot':
Error in mod.matrix %*% mod$coefficients[!is.na(mod$coefficients)] :
non-conformable arguments
If anyone has any ideas on how to fix this error, please let me know.
Thank,
Isabel
source to share
I'm sure this is a bug in the code. In particular, when dispatched, the code allEffects
calls effect
, which calls Effect.lm
. This feature is also recreated using
Effect.lm("x", mod=model)
# Error in mod.matrix %*% mod$coefficients[!is.na(mod$coefficients)] :
# non-conformable arguments
The error comes from line 30 of the body of this function
body(effects:::Effect.lm)[[30]]
# mod.matrix <- mod.matrix[, !is.na(mod$coefficients)]
The problem arises when you only have 1 non-NA coefficient (as with one regressor and no interception). The problem is that when you do this subset in one column, the result does not automatically translate to the vector, not the matrix. We can create our own version of the function that fixes this problem.
my.Effect.lm<-effects:::Effect.lm
body(my.Effect.lm)[[30]] <- quote(mod.matrix <- mod.matrix[, !is.na(mod$coefficients), drop=FALSE])
environment(my.Effect.lm) <- asNamespace("effects")
Then
model <- lm(y ~ 0 + x)
plot(my.Effect.lm("x", model))
must work. I must admit I didn't figure out how to get it allEffects
to work. I don't remember how to change how S3 dispatch works when it allows functions in different namespaces. There seems to be no easy way to fix the function that is actually in the namespace effects
.
Thus, in general, a function should work without interceptions if you have multiple regressors. You can contact the package author to report this issue.
source to share