[R-sig-Geo] Error in model frame while using ppm function

Rolf Turner r.turner at auckland.ac.nz
Fri Mar 15 21:45:49 CET 2013


There appears to be a lot of confusion (and unnecessary complication)
in what you've done, but the basic problem is with the way that you
are handling your covariates.  See inline below:

On 03/15/2013 07:33 PM, vikram ranga wrote:
> Dear list,
>
> I am having this error while running ppm function from spatstat package:
>
> Error in model.frame.default(formula = fmla, data = glmdata, subset =
> .mpl.SUBSET,  :
>    variable lengths differ (found for 'variables$var1')
>
> What i was trying to do:- is shown using toy example:
> #require(spatstat) #require(sp)
> a<-owin(poly = list(list(x = c(1, 8, 1), y = c(1, 1, 8)),list(x = c(3,
> 3, 4, 4), y = c(3, 4, 4, 3)),list(x=c(0,0.8,0),y=c(0,0,3)))) # makes
> polygons
> p<-rpoispp(a) # planar points
     This cannot possibly be what you actually did; this would have
     thrown an error.
> a.im<-as.im(a)
> p<-rpoispp(a.im)
     Why are you fooling about with images here?  Just do something like

     p <- rpoispp(100)

     to produce a pattern for illustrative purposes.
> var1<-runif(p$n)
> var2<-runif(p$n)
> var3<-runif(p$n)
> variables<-cbind(var1,var2,var3)
     This is where you really go wrong.  The variables object will be a 
matrix.
     You need it to be either a *data frame* --- having the same number of
     rows as the number of points in the quadrature scheme for the model
     --- or (better) a list of *images* each of which provides a 
covariate defined
     (effectively) at every point of the observation window.
> ppm.test<-ppm(p,~variables$var1+variables$var2+variables$var3,data=variables,method="ho")

     This is completely syntactically wrong in respect of the handling 
of "variables".
     Moreover the use of method "ho" here is silly.  You are fitting a 
Poisson model
     and thus the pseudolikelihood is the "real live likelihood". Hence 
using the Huang-Ogata
     method is redundant.

     Try:

     a<-owin(poly = list(list(x = c(1, 8, 1), y = c(1, 1, 8)),
                                   list(x = c(3,3, 4, 4), y = c(3, 4, 4, 
3)),
                                   list(x=c(0,0.8,0),y=c(0,0,3))))
     p <- rpoispp(100,win=a)
     Q <- quadscheme(p)
     U <- union.quad(Q)
     variables <- data.frame(
                         var1 = runif(npoints(U)),
                         var2 = runif(npoints(U)),
                         var3 = runif(npoints(U)))
     fit <- ppm(Q,~var1+var2+var3,covariates=variables)

     Or better:

     var1 <- 
im(runif(128^2),xcol=seq(0,8,length=128),yrow=seq(0,8,length=128))
     var2 <- 
im(runif(128^2),xcol=seq(0,8,length=128),yrow=seq(0,8,length=128))
     var3 <- 
im(runif(128^2),xcol=seq(0,8,length=128),yrow=seq(0,8,length=128))
     fit2   <- 
ppm(p,~var1+var2+var3,covariates=list(var1=var1,var2=var2,var3=var3))

>
> Error in model.frame.default(formula = fmla, data = glmdata, subset =
> .mpl.SUBSET,  :
>    variable lengths differ (found for 'variables$var1')
>
> However, the length of variables do not differ with ppp "p" but error
> shows variable lengths differ.
> I could not figure out why r throwing this error

     The error is thrown because your syntax is completely wrong.
     Going into the details of what is happening would be 
counter-productive.

     cheers,

         Rolf Turner



More information about the R-sig-Geo mailing list