Quantcast

Error when trying to update cpglm model

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Error when trying to update cpglm model

Rubem Kaipper Ceratti
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([hidden email])


## Simulating new response vector and re-fitting the model
sim.hnp<-function(glmfit){
  dat.1<-[hidden email]
  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

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Error when trying to update cpglm model

Peter Ehlers
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([hidden email])
>
>
> ## Simulating new response vector and re-fitting the model
> sim.hnp<-function(glmfit){
>    dat.1<-[hidden email]
>    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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Error when trying to update cpglm model

Rubem Kaipper Ceratti
Thanks, Peter, it's working perfectly now. 

And yes, the function does return 'something'. I just wanted to make the code minimal.
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] 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.
Loading...