如何从sempls {semPLS}(或来自densityplot {semPLS})中提取残差?

时间:2016-06-13 14:26:06

标签: r

我有semPLS包中的函数sempls运行的偏最小二乘路径模型(形成和反射潜在变量的混合)。我想检查模型残差中的空间自相关(由潜在变量之间的普通最小二乘回归生成),但必须首先弄清楚如何提取它们。    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")

残差(pmodp11)函数返回“NULL”值。

semPLS包中的densityplot函数会为我绘制残差,所以我假设它们必须存储在sempls对象中,但我不知道如何访问它们。

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

density plot of sempls object pmodp11

我还在semPLS中找到了与残差预测相关的以下代码,但尝试使用它会给我这个错误:

  

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)
}

1 个答案:

答案 0 :(得分:0)

在函数predict.sempls(在R-Forge_predict.sempls上定义)插入到residuals.sempls函数后,residuals.sempls现在似乎与sempls对象一起返回模型残差。

<div class="container">
    <ul id="list">
        <li>Image1</li>
        <li>Image2</li>
        <li>Image3</li>
    </ul>
    <div id="images">
        <img src="http://unsplash.it/100/70?image=10" alt="Title 1">
        <img src="http://unsplash.it/100/70?image=15" alt="Title 2">
        <img src="http://unsplash.it/100/70?image=20" alt="Title 3">
    </div>
</div>