1

I have partial least squares path models (a mixture of formative and reflective latent variables) run via function sempls in the semPLS package. I would like to check for spatial autocorrelation in the model residuals (generated by ordinary least squares regressions between latent variables), but must first figure out how to extract them. link to data file

land2011g <- read.table("percover2011.csv", header=T, sep=',', row.names=1)
land2011<-land2011g[c(1:2,4:16),]
topaphids<-land2011[c('A.cracc','R.padi','A.nerii','T.trifolii','R.maidis',
'Tetr.sp.','Pemphigus.sp.')]
r1 <- land2011[c(1:3,128:131)]
r1.s <- as.data.frame(scale(r1, center=T, scale=T))
r2 <- land2011[c(11:13,132:135)]
r2.s <- as.data.frame(scale(r2, center=T, scale=T))
r3 <- land2011[c(21:23,136:139)]
r3.s <- as.data.frame(scale(r3, center=T, scale=T))
r4 <- land2011[c(31:33,140:143)]
r4.s <- as.data.frame(scale(r4, center=T, scale=T))
r5 <- land2011[c(41:43,144:147)]
r5.s <- as.data.frame(scale(r5, center=T, scale=T))
weediness<-land2011[c('largest.total.cover.m2')]
noncols<-land2011[c('total.noncols')]

ifrom <- c("r1","r2","r3","r4","r5","weed.cover","aphids","aphids")
ito<-c("aphids","aphids","aphids","aphids","aphids",'aphids',"prsv","wmv")
inner.mod<-data.frame(ifrom,ito)
inner.mat<-as.matrix(inner.mod)

ofrom<-c('r1.corn.wheat','r1.soy','r1a.pasture.hay.grass',
'r1a.urban','r1a.forest.shrub','r2.corn.wheat','r2.soy',
'r2a.pasture.hay.grass','r2a.urban','r2a.forest.shrub',     
'r3.corn.wheat','r3.soy','r3a.pasture.hay.grass','r3a.urban',
'r3a.forest.shrub','r4.corn.wheat','r4.soy','r4a.pasture.hay.grass',
'r4a.urban','r4a.forest.shrub', 'r5.corn.wheat','r5.soy',
'r5a.pasture.hay.grass','r5a.urban','r5a.forest.shrub',
'weed.cover','aphids','prsv','wmv')
oto<-c('r1','r1','r1','r1','r1','r2','r2','r2','r2','r2','r3','r3','r3',
'r3','r3','r4','r4','r4','r4','r4','r5','r5','r5','r5','r5',
'largest.total.cover.m2','total.noncols','arcsin.sqrt.PRSV',
'arcsin.sqrt.WMV')
outer.mod<-data.frame(ofrom,oto)
outer.mat<-as.matrix(outer.mod)

prsv<-land2011[c("arcsin.sqrt.PRSV")]
wmv<-land2011[c("arcsin.sqrt.WMV")]
subsets <- cbind(r1.s,r2.s,r3.s,r4.s,r5.s,weediness,topaphids,
noncols,prsv,wmv)

plspm11<-plsm(data=subsets, strucmod=inner.mat, measuremod=outer.mat)

pmodp11<-sempls(plspm11, data=subsets, method="pearson", scaled=TRUE, 
maxit=100, convCrit="square")

The residuals(pmodp11) function returns a "NULL" value.

The densityplot function in the semPLS package will plot the residuals for me so I assume they must be stored in the sempls object, but I am not sure how to access them.

densityplot(pmodp11, subsets, use=c("residuals"))

density plot of sempls object pmodp11

I also found the following code related to residuals prediction in semPLS, but attempting to use it gives me this error:

Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "sempls".

residuals.sempls <- function(object, what=c("LVs", "MVs"), scale=c("scaled", "original"), total=FALSE){
    what <- match.arg(what)
    scale <- match.arg(scale)
    model <- object$model
    data <- object$data
    # LVs
    if(what=="LVs"){
        res <- object$factor_scores - predict(object, what=what, scale=scale, total=total)
    }
    # MVs
    else{
      if(scale=="scaled"){
        pdata <- predict(object, what=what, scale=scale, total=total)
        res <- data - pdata
      }
      else{
        data <- rescale(data)
        pdata <- predict(object, what=what, scale=scale, total=total)
        res <- data - pdata
      }
    }
    return(res)
}
GMA
  • 11
  • 4
  • Can you make this into a reproducible example (so that I can reproduce your error)? http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?noredirect=1#comment63050894_5963269 – Hack-R Jun 13 '16 at 14:29
  • Sure -- I added in a link to the data and the rest of my code above. – GMA Jun 13 '16 at 18:11

1 Answers1

0

After the function predict.sempls (defined on R-Forge_predict.sempls) is inserted into the residuals.sempls function, residuals.sempls now seems to work with a sempls object to return model residuals.

residuals.sempls <- function(object, what=c("LVs", "MVs"), scale=c("scaled", "original"), total=FALSE){
    what <- match.arg(what)
    scale <- match.arg(scale)
    model <- object$model
    data <- object$data
    # LVs
    if(what=="LVs"){
        res <- object$factor_scores - predict.sempls(object, what=what, scale=scale, total=total)
    }
    # MVs
    else{
      if(scale=="scaled"){
        pdata <- predict.sempls(object, what=what, scale=scale, total=total)
        res <- data - pdata
      }
      else{
        data <- rescale(data)
        pdata <- predict.sempls(object, what=what, scale=scale, total=total)
        res <- data - pdata
      }
    }
    return(res)
}
GMA
  • 11
  • 4