actuaryzhang / cplm Goto Github PK
View Code? Open in Web Editor NEWCompound Poisson Linear Models
Compound Poisson Linear Models
Hi,
Great library; thanks for putting it together. Having an odd problem with running CPGLMM. I'm doing a few thousand regressions in a loop (apply isn't optimal for my use case). I don't expect all of them to converge, and have it set up in a tryCatch loop to handle this. However, I've found that once 1 regression fails, all those following it fail as well, even if when run in isolation they don't. If I start the loop at at different value (ie i=8 instead of i=1), regressions that previously failed do not...Any idea why this would be the case? I'm curious if using cpglmm changes some environmental feature that does not get shifted back when a regression completes?
Image 1 shows what happens if I start the loop at "regression 1," image 2 shows what happens if I start the loop at "regression 2."
Any help you can provide would be greatly appreciated.
Thanks,
Braden
See also our e-mail to you, from Jan.24, 2023.
The <Rdefines.h>
header has become "semi - deprecated",
and so in the Matrix package we have stopped using. It contains a very simple macro GET_SLOT()
which you have been using.
A back compatible change such that {cplm}
continues to install fine with the upcoming Matrix 1.5-4
is
1 file changed, 5 insertions(+), 1 deletion(-)
src/common.h | 6 +++++-
modified src/common.h
@@ -18,7 +18,11 @@
#include <Rinternals.h>
#include <Rmath.h>
#include <R_ext/Lapack.h> /* for BLAS and Lapack related */
-#include "Matrix.h" /* for cholmod functions and S4 structures (GET_SLOT)*/
+#include "Matrix.h" /* for cholmod functions and S4 structures */
+#ifndef GET_SLOT
+ // in Rdefines.h (formerly in Matrix.h) :
+# define GET_SLOT(x, what) R_do_slot(x, what)
+#endif
#ifndef FCONE
# define FCONE
#endif
fitted(model)
and predict(model, type='response')
are supposed to return the same set of values. But it is not the case for the cplm model objects.
library(cplm)
da <- subset(AutoClaim, IN_YY == 1)
da <- transform(da, CLM_AMT = CLM_AMT / 1000)
P1 <- cpglm(CLM_AMT ~ 1, data = da, offset = log(NPOLICY))
# Compare the values of fitted and predicted with type response specified.
all.equal(fitted(P1), predict(P1, type='response'), check.attributes = FALSE)
# Mean relative difference: 0.5924107
# The values are different!
# Plot and see:
plot(fitted(P1))
plot(predict(P1, type='response'))
# But if added the `newdata` argument, it's now same.
all.equal(fitted(P1), predict(P1, newdata=da, type='response'), check.attributes = FALSE)
# TRUE
# Compare to other models, there is no such problem?
# I checked with `glm::glm()`, `MASS::glm.nb()`, and `tweedie()` with `glm::glm()`.
Hello,
Under R 4.1.0, I'm using cplm::cpglm() within a tryCatch.
The control argument is understanding things like max.iter = X, but fails to accepts trace = 0 (at least my understanding is that it should remove any verbose print in the console).
Code below:
fit <- tryCatch(
expr = {
cplm::cpglm(
formula = formula,
data = data,
control = list(trace = 0)
)
},
error = function(cond) {
# Stuff
},
warning = function(cond) {
# Stuff
}
)
Console
Call:
cplm::cpglm(formula = formula, data = data, control = list(trace = 0))
Deviance Residuals:
Min 1Q Median 3Q Max
-3.0237 -2.6788 -0.5744 0.3147 5.9825
Estimate Std. Error t value Pr(>|t|)
(Intercept) -9.3936 0.3071 -30.589 <2e-16 ***
Group_A_vs_Control 0.2241 0.4094 0.547 0.586
Group_B_vs_Control 0.1434 0.4179 0.343 0.732
Signif. codes: 0 ‘’ 0.001 ‘’ 0.01 ‘’ 0.05 ‘.’ 0.1 ‘ ’ 1
Estimated dispersion parameter: 3.7397
Estimated index parameter: 1.3619
Residual deviance: 400.3 on 86 degrees of freedom
AIC: 441.87
Number of Fisher Scoring iterations: 5
Any tips on how to suppress the verbose output?
I had a user ask about emmeans support for cplm
objects. I found that the terms
method does not work in the namespace of my package. (See also rvlenth/emmeans#116) As explained in an answer on Stack Overflow -- https://stackoverflow.com/questions/56739673/flaky-s4-method-dispatch -- there exists an S3 generic for terms
but there is no S3 method for cplm
or cpglm
objects. The answerer points to documentation recommending that such S3 methods be added, and how to do it. In particular, add and export functions like this:
terms.cplm <- function(x, ...) terms(x, ...)
terms.cpglm <- function(x, ...) terms(x, ...)
To test it, try something like:
m <- cpglm(...)
stats::terms(m)
I think you need to do this for all S4 methods you provide for which there also exists an S3 generic.
In emmeans, I opted to create S4 objects, but use S3 methods exclusively, no S4 ones, for exactly these kinds of reasons.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.