On 2012-06-19 08:31, Rubem Kaipper Ceratti wrote:
Dear all,
I've been having problems running update() to re-fit a cpglm model inside a
function (as in the code below). The solution is probably simple, but I'm
stuck. If anyone could help, I'd greatly appreciate it.
Regards,
Rubem
## R code
library(cplm)
## Data simulation
period<-factor(1:4)
herd<-factor(1:50)
dat<-expand.grid(period=period,herd=herd)
beta<-c(-.3,1.7,2.5,3.4)
X<-model.matrix(~period,dat)
mu<-as.vector(exp(X%*%beta))
phi<-1; p<-1.6
dat$resp<-rtweedie(nrow(dat),p,mu,phi)
## cpglm model
modPC1<-cpglm(resp~period,data=dat)
head(modPC1@model.frame)
## Simulating new response vector and re-fitting the model
sim.hnp<-function(glmfit){
dat.1<-glmfit@model.frame
n<-nrow(dat.1)
dat.1[,1]<-rtweedie(n,glmfit$p,fitted(glmfit),glmfit$phi)
mfun<-update(object=glmfit,data=dat.1)
# rp<-resid(mfun,type='pearson')
# sort(abs(rp))
}
sim.hnp(modPC1) # Error in is.data.frame(data) : object 'dat.1' not found
Seems to be an environment problem. A quick fix might be
to insert
assign("dat.1", dat.1, envir=.GlobalEnv)
before your call to update(), and
rm(dat.1, envir=.GlobalEnv)
after the call. Presumably, you'll also ensure to return
something from the function.
Peter Ehlers
______________________________________________
R-help@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.