[R] S3 Methods for Linear Regression

ONKELINX, Thierry Thierry.ONKELINX at inbo.be
Mon Sep 9 16:18:04 CEST 2013


Dear Harold,

An easy work-around would be to pass the names of the variables as a character vector.

fm <- lm.eiv(y ~ x1 + x2, dat, ind = c(2,3), semDep = 0, semMat = c("sem1", "sem2"))

And the change your lm.eiv.fit accordingly.

Or you could have a look at the .() function of the plyr package.

ddply(dat, c("sem1", "sem2"), some.function)

is equivalent to

ddply(dat, .(sem1, sem2), some.function)


Best regards,

Thierry
________________________________________
Van: r-help-bounces op r-project.org [r-help-bounces op r-project.org] namens Doran, Harold [HDoran op air.org]
Verzonden: maandag 9 september 2013 15:56
Aan: r-help op r-project.org
Onderwerp: [R] S3 Methods for Linear Regression

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]]

______________________________________________
R-help op r-project.org 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.
* * * * * * * * * * * * * D I S C L A I M E R * * * * * * * * * * * * *
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document.
The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.



More information about the R-help mailing list