Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for cplm #116

Closed
krz opened this issue Jun 18, 2019 · 6 comments
Closed

support for cplm #116

krz opened this issue Jun 18, 2019 · 6 comments

Comments

@krz
Copy link

krz commented Jun 18, 2019

Hi,

I would appreciate support for the cplm package to compute compound-poisson linear models using the cpglm function.
I tried writing a method but it doesn't seem to be trivial.

thanks

@rvlenth
Copy link
Owner

rvlenth commented Jun 20, 2019

Thanks for the suggestion. I have not looked yet at this package but will see what appears to be possible. I really have the hope that package developers can incorporate emmeans support in their own packages, as they are the ones familiar with the features offered and what is needed to incorporate predictions with new data - which is essentially what needs to be coded. Have you contacted the developer/maintainer of cplm?

Also, it might be worth looking at the emmeans::qdrg() function; it may be possible to get it to function. See its documentation and the example for biglm.

@krz
Copy link
Author

krz commented Jun 20, 2019

thanks, it worked with qdrg

library(emmeans)
library(cplm)

data(mtcars)

m <- cpglm(mpg ~ cyl + disp + hp, data = mtcars)

rg <- qdrg(~ cyl + disp + hp,  data = mtcars, coef = coef(m), vcov = vcov(m))
summary(emmeans(rg, "cyl"), type = "response")

I also tried implementing a recover_data method. Actually it should be very similar to lm

recover_data.cpglm = emmeans:::recover_data.lm

but

> recover_data(m)
Error in slot(x, name) : 
  no slot of name "terms" for this object of class "cpglm"

However, this works:

> terms(m)
mpg ~ cyl + disp + hp
attr(,"variables")
list(mpg, cyl, disp, hp)
attr(,"factors")
     cyl disp hp
mpg    0    0  0
cyl    1    0  0
disp   0    1  0
hp     0    0  1
attr(,"term.labels")
[1] "cyl"  "disp" "hp"  
attr(,"order")
[1] 1 1 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: R_GlobalEnv>
attr(,"predvars")
list(mpg, cyl, disp, hp)
attr(,"dataClasses")
      mpg       cyl      disp        hp 
"numeric" "numeric" "numeric" "numeric"

and is the same output as from a lm object on the same data.

@rvlenth
Copy link
Owner

rvlenth commented Jun 20, 2019

Hmmm, well this is a mystery to me. I did a trace of calling emmeans:::recover_data.lm(m) and in debug mode, I got the same error message in calling terms(object), even though it works in the global environment. The problem seems to be related to the fact that the object is of class "cpglm" while the method is defined for class "cplm". I get:

> getMethod("terms", "cplm")
Method Definition:

function (x, ...) 
attr(x@model.frame, "terms")
<bytecode: 0x000001853bccf9f8>
<environment: namespace:cplm>

Signatures:
        x     
target  "cplm"
defined "cplm"

> getMethod("terms", "cpglm")
Error in getMethod("terms", "cpglm") : 
  no method found for function 'terms' and signature cpglm

> methods(class = "cpglm")
 [1] $            [            [[           AIC          coef         deviance     fitted      
 [8] formula      model.matrix names        predict      resid        residuals    show        
[15] summary      terms        vcov        
see '?methods' for accessing help and source code

So I am getting a mixed message: that there is a terms method for cpglm, but I can't retrieve it. I have lots of problems understanding S4 classes and inheritance, and this just adds to it. But the method for cplm tells us what we can do:

recover_data.cpglm = function(object, ...) {
    fcall = object$call
    trms = delete.response(attr(object@model.frame, "terms"))
    recover_data(fcall, trms, object$na.action, ...)
}

This part does work:

> head(recover_data(m))
                  cyl disp  hp
Mazda RX4           6  160 110
Mazda RX4 Wag       6  160 110
Datsun 710          4  108  93
Hornet 4 Drive      6  258 110
Hornet Sportabout   8  360 175
Valiant             6  225 105

That's a start; maybe an emm_basis method can be cobbled together too.

@krz
Copy link
Author

krz commented Jun 24, 2019

Thanks, I‘ll play around with it some more, but I’m happy with the qdrg solution so far.

@rvlenth
Copy link
Owner

rvlenth commented Jul 18, 2019

I think I'll close this now. Maybe you can encourage the developer of cplm to include emmeans support.

@rvlenth rvlenth closed this as completed Jul 18, 2019
rvlenth added a commit that referenced this issue Jul 29, 2020
added submodel support to aovlist
@rvlenth
Copy link
Owner

rvlenth commented Jul 29, 2020

Oops, I referred to this issue when I meant #216. Sorry for any confusion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants