[R-sig-Geo] Cause of error in spdep::errorsarlm

Dan Rosauer dan.rosauer at anu.edu.au
Fri Nov 7 04:10:07 CET 2014


Thanks very much Roger and Frede

This was very helpful.  

I have somehow managed to run some quite intricate model fitting and model selection code over several years, while handling formulas entirely as text, and being almost completely unaware of the formula class and associated functions such as model.frame.  But now I am elightened!

By the way Roger, I just found that errorsarlm works beautifully with dredge {MuMIn} for model selection and averaging.  The two together have just saved large amounts of time and coding compared to previous analyses.

cheers
Dan

-----Original Message-----
From: Roger Bivand [mailto:Roger.Bivand at nhh.no] 
Sent: Thursday, 6 November 2014 11:46 PM
To: Frede Aakmann Tøgersen
Cc: Dan Rosauer; r-sig-geo at r-project.org
Subject: RE: Cause of error in spdep::errorsarlm

On Wed, 5 Nov 2014, Frede Aakmann Tøgersen wrote:

> Hi
>
> Please remember to keep the r-sig-geo on the list.
>
> See ?formula. I guess that somewhere under the hood of glm()
> as.formula() is called. Checking the code of glm() you can see that
> model.frame() is called. From ?model.frame one notice that:
>
>     If either 'formula' or 'data' is already a model frame (a data
>     frame with a '"terms"' attribute) and the other is missing, the
>     model frame is returned.  Unless 'formula' is a terms object,
>     'as.formula' and then 'terms' is called on it.  (If you wish to
>     use the 'keep.order' argument of 'terms.formula', pass a terms
>     object rather than a formula.)
>
> All this happens in glm() but not in errorsarlm():

Thanks for a careful diagnosis. I've committed (R-forge, revision 563) simple fixes to all model fitting functions in spdep that used the formula argument without checking that it is of class "formula". Only spautolm() used the full model.frame() mechanism before, and this distinction remains.

With released spdep:

SAR.mod <- errorsarlm(as.formula(my_formula), data=df,
   listw=weight_list)

and

SAR.mod <- spautolm(my_formula, data=df, listw=weight_list)

give the same output. With the next release, the as.formula() is done internally. spautolm() was written in 2005/6, the others from 1998 onwards, and the differences in handling model.frame objects stem from practice in lm() at those times.

I can see that using a string to hold a formula may seem attractive as it permits paste() and other string functions to be used to modify the formula. I would however suggest that using the update() method for formulae is at least as flexible, and that formula arguments be given as formula objects, not as strings, even if this usage is possible.

Best wishes,

Roger

>
>> my_formula    <- "resp ~  pred1 + pred2"
>> SAR.mod           <- errorsarlm(my_formula, data=df, listw=weight_list)
> Error: $ operator is invalid for atomic vectors
>> traceback()
> 3: terms.default(formula, data = data)
> 2: terms(formula, data = data)
> 1: errorsarlm(my_formula, data = df, listw = weight_list)
>>
>> class(my_formula)
> [1] "character"
>> class(resp ~  pred1 + pred2)
> [1] "formula"
>>
>
> In errorsarlm() terms() is called with my_formula and data and since formula is a string an error is raised.
>
> In the man page of errorsarlm() one sees:
>
> formula: a symbolic description of the model to be fit. The details of
>          model specification are given for 'lm()'
>
> And in ?lm we see:
>
> formula: an object of class '"formula"' (or one that can be coerced to
>          that class): a symbolic description of the model to be
>          fitted.  The details of model specification are given under
>          'Details'.
>
> So strictly speaking errorsarlm() should be able to handle a formula defined as a string (maintainer on cc).
>
>
> Yours sincerely / Med venlig hilsen
>
>
> Frede Aakmann Tøgersen
> Specialist, M.Sc., Ph.D.
> Plant Performance & Modeling
>
> Technology & Service Solutions
> T +45 9730 5135
> M +45 2547 6050
> frtog at vestas.com
> http://www.vestas.com
>
> Company reg. name: Vestas Wind Systems A/S This e-mail is subject to 
> our e-mail disclaimer statement.
> Please refer to www.vestas.com/legal/notice If you have received this 
> e-mail in error please contact the sender.
>
>
>> -----Original Message-----
>> From: Dan Rosauer [mailto:dan.rosauer at anu.edu.au]
>> Sent: 5. november 2014 07:59
>> To: Frede Aakmann Tøgersen
>> Subject: RE: Cause of error in spdep::errorsarlm
>>
>> That worked.  Thank you!
>>
>> But I don't understand.  Normally if you try to assign unquoted text 
>> to a variable, it check if the text refers to named objects, and if not gives an error.
>>
>> And the quoted version worked perfectly for GLM models.
>>
>> So problem solved, and something learned.
>>
>> cheers
>> Dan
>>
>> ~~~~~~~~~~~~~~~~~~~~~~
>> Dan Rosauer
>> Postdoctoral Researcher
>> Moritz Lab
>> Ecology, Evolution & Genetics
>> Research School of Biology
>> Gould Building, Daley Road
>> Australian National University
>> Canberra ACT 0200
>>
>> +61 413 950 275 (mobile)
>> +61 2 6125 1028 (office)
>> dan.rosauer at anu.edu.au
>>
>> sites.google.com/site/danielrosauer
>>
>>
>>
>>
>> -----Original Message-----
>> From: Frede Aakmann Tøgersen [mailto:frtog at vestas.com]
>> Sent: Wednesday, 5 November 2014 5:19 PM
>> To: Dan Rosauer; r-sig-geo at r-project.org
>> Subject: RE: Cause of error in spdep::errorsarlm
>>
>> Hi Dan
>>
>> Remove the " from your formula. It should be
>>
>> my_formula    <- resp ~  pred1 + pred2
>>
>>
>> Yours sincerely / Med venlig hilsen
>>
>>
>> Frede Aakmann Tøgersen
>> Specialist, M.Sc., Ph.D.
>> Plant Performance & Modeling
>>
>> Technology & Service Solutions
>> T +45 9730 5135
>> M +45 2547 6050
>> frtog at vestas.com
>> http://www.vestas.com
>>
>> Company reg. name: Vestas Wind Systems A/S This e-mail is subject to 
>> our e- mail disclaimer statement.
>> Please refer to www.vestas.com/legal/notice If you have received this 
>> e-mail in error please contact the sender.
>>
>>> -----Original Message-----
>>> From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r- 
>>> project.org] On Behalf Of Dan Rosauer
>>> Sent: 5. november 2014 03:21
>>> To: r-sig-geo at r-project.org
>>> Subject: [R-sig-Geo] Cause of error in spdep::errorsarlm
>>>
>>> Hi Roger et al
>>>
>>> I hope someone can help me with the following error with a call to 
>>> the spdep function errorsarlm()
>>>
>>> Calling the function returns this:  Error: $ operator is invalid for 
>>> atomic vectors
>>>
>>> Things I have already tried:
>>> * testing with real and simulated data - error occurs
>>> * running it on windows and linux - error is identical
>>> * omitting various arguments to the function to see which one is the 
>>> cause - error occurs with or without the data or listw arguments.  
>>> So it apparently triggers this error before it even gets to the 
>>> problem of the missing arguments, and apparently they don't relate 
>>> to the actual content of the data or spatial weights.
>>>
>>> Here is a test script which generates the error:
>>>
>>> ######################
>>> library(spdep)
>>>
>>> # create a sample data frame
>>> df            <- data.frame(lat=runif(100,0,3),long=runif(100,0,3),
>>> pred1=runif(100,0,1), pred2=runif(100,0,1), resp=runif(100,0,10))
>>>
>>> cat("\nPreparing neighbourhood weights for SARLM\n\n")
>>> coords        <-as.matrix(cbind(df$long, df$lat))
>>> cont.nb       <- dnearneigh(coords,d1=0, d2=100,longlat=TRUE)
>>> weight_list   <- nb2listw(cont.nb, glist=NULL, style="W", zero.policy=TRUE)
>>>
>>> my_formula    <- "resp ~  pred1 + pred2"
>>>
>>> # this line gives the error: $ operator is invalid for atomic vectors
>>> SAR.mod           <- errorsarlm(my_formula, data=df, listw=weight_list)
>>> ######################
>>>
>>> The R version is 3.1.0, and spdep 0.5-77
>>>
>>> Any advice on how to fix this problem would be much appreciated.
>>>
>>> The best possibility is that I've made a dumb mistake which can be 
>>> easily fixed... :-)
>>>
>>> thanks
>>>
>>> Dan
>>>
>>>
>>> ~~~~~~~~~~~~~~~~~~~~~~
>>> Dan Rosauer
>>> Postdoctoral Researcher
>>> Moritz Lab
>>> Ecology, Evolution & Genetics
>>> Research School of Biology
>>> Gould Building, Daley Road
>>> Australian National University
>>> Canberra ACT 0200
>>>
>>> +61 413 950 275 (mobile)
>>> +61 2 6125 1028 (office)
>>> dan.rosauer at anu.edu.au
>>>
>>> sites.google.com/site/danielrosauer
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at r-project.org
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

--
Roger Bivand
Department of Economics, Norwegian School of Economics, Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list