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

niv de malach nivdemalach at gmail.com
Sun Oct 22 07:28:16 CEST 2017

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

I know that the package piecewiseSEM accept the following object types:

‘lm‘, ‘rq‘, ‘glm‘, ‘glm.nb‘, ‘gls‘, ‘pgls‘, ‘merMod‘, ‘merModLmerTest‘,

so  "transformation" of errorsarlm to any of these object should solve the
problem.

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