I have a function for fitting a type of linear regression and have written
methods for it as shown below. I exclude the main lm.eiv.fit function as it is
large and I don't think necessary for the reproducible example. But, I can
certainly provide if that would be needed.
These methods allow for the normal formula interface with the function and some
of the other common methods (e.g., subset). One thing I cannot figure out is
how to allow the argument semMat in the function to recognize the variable can
also be in the dataframe "dat".
When I call the function as follows everything is just fine.
fm <- lm.eiv(y ~ x1 + x2, dat, ind = c(2,3), semDep = 0, semMat =
cbind(dat$sem1, dat$sem2))
However, if I were to use the following instead such that I do not specify that
both sem1 and sem2 are in the dataframe 'dat',
fm <- lm.eiv(y ~ x1 + x2, dat, ind = c(2,3), semDep = 0, semMat = cbind(sem1,
sem2))
Error in as.matrix(semMat) :
error in evaluating the argument 'x' in selecting a method for function
'as.matrix': Error in cbind(sem1, sem2) : object 'sem1' not found
How can I resolve this such that the argument semMat will also work with
variables in the dataframe 'dat' just as the variables in the formula depend on
dat?
lm.eiv <- function(...) UseMethod("lm.eiv")
lm.eiv.default <- function(x, y, ind, semDep, semMat, ...){
result <- lm.eiv.fit(x, y, ind, semDep, semMat, ...)
result$call <- match.call()
class(result) <- "eiv"
result
}
lm.eiv.formula <- function(formula, data, na.action, subset, ind, semDep,
semMat, ...){
mf <- match.call(expand.dots = FALSE)
m <- match(c("formula", "data", "na.action", "subset"), names(mf), 0L)
mf <- mf[c(1L, m)]
mf$drop.unused.levels <- TRUE
mf[[1L]] <- as.name("model.frame")
mf <- eval(mf, parent.frame())
y <- model.response(mf)
mt <- attr(mf, "terms")
x <- model.matrix(mt, mf, contrasts)
result <- lm.eiv.default(x, y, ind, semDep, semMat, ...)
result$call <- match.call()
result$formula <- formula
result
}
aa <- lm.eiv.formula(InstructScore_Spring ~ InstructScore_Fall, dat, ind = 2,
semDep = 0, semMat = dat$InstructScoreSE_Fall)
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.