Søren Højsgaard
2009-Oct-19 13:20 UTC
[R] Defining S3-methods for S4-objects: cannot coerce type 'S4' to vector of type 'integer'
In the 'doBy' package there is an esticon() function for calculating linear contrasts for various model types. I have defined an S3-method 'esticon.mer()' for 'mer' objects from the lme4 package. Building the package and invoking the method gives:> esticon(fm1, c(1,1))Confidence interval ( WALD ) level = 0.95 Error in as.integer(x) : cannot coerce type 'S4' to vector of type 'integer' However, if is source the source code into R then I get what I expected:> esticon(fm1, c(1,1))Confidence interval ( WALD ) level = 0.95 beta0 Estimate Std.Error X2.value DF Pr(>|X^2|) Lower.CI Upper.CI 1 0 261.8724 6.786701 1488.888 1 0 248.5707 275.1741 The problem above arises because esticon.mer() uses the variance-covariance matrix of the fixed effects (vcv below) in a calculation (cm is a matrix): cm %*% vcv %*% t(cm). The result is: 1 x 1 Matrix of class "dgeMatrix" [,1] [1,] 46.05931 - and taking diag() of that matrix causes the error above... However, things work fine if I use as.matrix() as is: diag(as.matrix(cm %*% vcv %*% t(cm))). - This tentatively suggests that there is no diag() method for 'dgeMatrix' objects (can hardly be the case!), but why then does my code work when it is sourced into R??? - Does anyone have an idea about what goes wrong? ) - Is there anything 'special' that one must do in connection with defining S3-methods for S4 objects (e.g. in connection with package building)?? I use R 2.9.2 on windows XP. I am sure that such issues must have come up for others in the past but I've been unable to find questions/answers in the archives. Sorry if the topic has already been covered. Regards Søren Højsgaard [[alternative HTML version deleted]]
Martin Morgan
2009-Oct-19 15:20 UTC
[R] Defining S3-methods for S4-objects: cannot coerce type 'S4' to vector of type 'integer'
S?ren H?jsgaard wrote:> In the 'doBy' package there is an esticon() function for calculating linear contrasts for various model types. I have defined an S3-method 'esticon.mer()' for 'mer' objects from the lme4 package. Building the package and invoking the method gives: > >> esticon(fm1, c(1,1)) > Confidence interval ( WALD ) level = 0.95 > Error in as.integer(x) : > cannot coerce type 'S4' to vector of type 'integer' > However, if is source the source code into R then I get what I expected: >> esticon(fm1, c(1,1)) > Confidence interval ( WALD ) level = 0.95 > beta0 Estimate Std.Error X2.value DF Pr(>|X^2|) Lower.CI Upper.CI > 1 0 261.8724 6.786701 1488.888 1 0 248.5707 275.1741 > The problem above arises because esticon.mer() uses the variance-covariance matrix of the fixed effects (vcv below) in a calculation (cm is a matrix): > cm %*% vcv %*% t(cm). The result is: > > 1 x 1 Matrix of class "dgeMatrix" > [,1] > [1,] 46.05931 > - and taking diag() of that matrix causes the error above... However, things work fine if I use as.matrix() as is: diag(as.matrix(cm %*% vcv %*% t(cm))). > > - This tentatively suggests that there is no diag() method for 'dgeMatrix' objects (can hardly be the case!), but why then does my code work when it is sourced into R??? > > - Does anyone have an idea about what goes wrong? )A little bit of a guess, but does doBy have a NAMESPACE? If so, then perhaps the appropriate methods are not being imported (iimportFrom(Matrix, diag) ??) Martin> - Is there anything 'special' that one must do in connection with defining S3-methods for S4 objects (e.g. in connection with package building)?? > > I use R 2.9.2 on windows XP. I am sure that such issues must have come up for others in the past but I've been unable to find questions/answers in the archives. Sorry if the topic has already been covered. > > Regards > S?ren H?jsgaard > > > [[alternative HTML version deleted]] > > > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at 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.-- Martin Morgan Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793