[R-sig-Geo] convert "errorsarlm" to "lm"

Roger Bivand Roger.Bivand at nhh.no
Sun Oct 22 21:23:53 CEST 2017


On Sun, 22 Oct 2017, niv de malach wrote:

> Regarding your question about MRF, I don't know what it is...

see for example:

http://www.fromthebottomoftheheap.net/2017/10/19/first-steps-with-mrf-smooths/

here using mgcv::gam() to fit a model with an MRF smooth. In effect, 
errorsarlm() is fitting something similar,

>
> I know that the package piecewiseSEM accept the following object types:
>
> ‘lm‘, ‘rq‘, ‘glm‘, ‘glm.nb‘, ‘gls‘, ‘pgls‘, ‘merMod‘, ‘merModLmerTest‘,
> ‘lme‘, ‘glmmPQL‘, ‘glmmadmb‘, and ‘glmmTMB‘.
>
> so  "transformation" of errorsarlm to any of these object should solve the
> problem.
>

No! You have to pass through the uncertainty associated with fitting the 
spatial regression coefficient. The same would apply with any other 
MRF-based approach. It is possible to pretend that this coefficient is 
fixed (I'll return to the example tomorrow). I don't think that any of the 
listed object types come from fitting functions supporting MRF, though. 
You would likely need to re-fit your piecewiseSEM over samples from the 
coefficients, to check that the path outcomes were robust (just a 
speculation). Any Bayesians who could help?

Roger

>
> Below there is a code where a dataset is simulated and analyzed using 'lm'
> and 'errorsarlm'. Only 'lm' is accepted for
> SEM analysis.
>
> Thanks,
> Niv
>
>
> #################### creating a simulated dataset
> ##############################
>
> set.seed(1)
> sample=50 # sample size (number of points)
>
> # spatial location
> Lattitude=rnorm(sample, mean = 0, sd = 4)
> Longitude=rnorm(sample, mean = 0, sd = 4
>
> # creating exploratory variables
> x1=rnorm(sample, mean = 30, sd = 20)
> x2=rnorm(sample, mean = -20, sd = 10)
> x3=rnorm(sample, mean = 5, sd = 3)
> x4=rnorm(sample, mean = 25, sd = 5)
> x5=rnorm(sample, mean = -10, sd = 10)
> x6=rnorm(sample, mean = 50, sd = 30)
>
> # response variables
> y1=10*x1+5*x2+2*x3+30*Lattitude+rnorm(sample, mean = 0, sd = 20) # y1 is
> function of x1-x3 +location+random error
> y2=10*x4+5*x5+2*x6+10*Longitude+rnorm(sample, mean = 0, sd = 20) # y1 is
> function of x4-x5 +location+random error
>
> mydata=data.frame(x1,x2,x3,x4,x5,x6,y1,y2,Lattitude,Longitude)
>
> ################# building errorsarlm objects and lm objects
> ####################################
>
> library(spdep)
>
> # creating neighbors for errorsarlm function
> xy=SpatialPoints(as.matrix(mydata[,c(9,10)]))
> myneighbors=dnearneigh(as.matrix(mydata[,c(9,10)]), 0, 1000, row.names =
> NULL, longlat = TRUE)# 1000 kilometer distance
> myweight=nb2listw(myneighbors,style="W") #
>
> # two errorsarlm objects
> sar1=errorsarlm(y1~x1+x2+x3,data=mydata,myweight)
> sar2=errorsarlm(y2~x4+x5+x6,data=mydata,myweight)
>
> # two lm objects
> lm1=lm(y1~x1+x2+x3,data=mydata)
> lm2=lm(y2~x4+x5+x6,data=mydata)
>
> ############### trying to use sarlm and lm in "piecewiseSEM"
> ####################
>
> library(piecewiseSEM) # package for structural equation modeling
> SEM_LM=sem.fit(list(lm1,lm2),data=mydata) # the package accept lm objects
> (and random models such as lme)
> SEM_errorsarLM=sem.fit(list(sar1,sar2),data=mydata) # the package does not
> accept errprsarlm objects
>
> # Error in nobs.default(x) : no 'nobs' method is available #
>
>
>
>
>>
> On Fri, Oct 20, 2017 at 2:06 PM, Roger Bivand <Roger.Bivand at nhh.no> wrote:
>
>> On Wed, 18 Oct 2017, niv de malach wrote:
>>
>> Hi all,
>>> I need to convert an "errorsarlm" object into a simple "lm" object because
>>> the package I use ("piecewiseSEM") could not deal with "errorsarlm". Is
>>> there a way to transform the object?
>>>
>>
>> Please give a reproducible example [1] showing what you are doing,
>> preferably with a built-in data set. In any case, an lm object made from a
>> sarlm object will fix the spatial coefficient and loose its uncertainty.
>>
>> [1] https://stackoverflow.com/questions/5963269/how-to-make-a-
>> great-r-reproducible-example
>>
>> Can you use an model class accepted in piecewiseSEM that fits an MRF
>> random effect?
>>
>> Roger
>>
>>
>>> Thanks,
>>> Niv
>>>>>>
>>>         [[alternative HTML version deleted]]
>>>
>>> _______________________________________________
>>> 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
>> <https://maps.google.com/?q=an+School+of&entry=gmail&source=g>Economics,
>> Helleveien 30, N-5045 Bergen, Norway.
>> voice: +47 55 95 93 55; e-mail: Roger.Bivand at nhh.no
>> Editor-in-Chief of The R Journal, https://journal.r-project.org/index.html
>> http://orcid.org/0000-0003-2392-6140
>> https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: Roger.Bivand at nhh.no
Editor-in-Chief of The R Journal, https://journal.r-project.org/index.html
http://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en


More information about the R-sig-Geo mailing list