Subscript out of bound error in R

徘徊边缘 提交于 2019-12-24 04:49:06

问题


While Using factanal function from stats package for performing factor analysis.

I tried following thing.

library(mirt)
library(ltm)
library(psych)
library(stats)
data(SAT12)
data=SAT12
cor_mat=polychoric(data,  ML=TRUE, global=F)
 fit <- factanal(factors=2, n.obs=nrow(data), covmat=cor_mat$rho)


Divide_item_Factor_Loading(fit)

when I am trying to run Divide_item_Factor_Loading(fit) an error called

   Error in a[[i]][[2]] : subscript out of bounds 

pops up.

my complete code of Divide_item_Factor_Loading:

Divide_item_Factor_Loading=function(fit)
{
  a=list()
  items=NULL
  for(i in 1:nrow(fit$loadings)) ######corresponding to rows of loading matrix
  {
    k=which(fit$loadings[i,]==max(abs(fit$loadings[i,])))  
    a[[i]]=c(i,as.numeric(k))
  } 
  fact_item_mat=matrix(, nrow=nrow(fit$loadings), ncol=ncol(fit$loadings))
  for(j in 1:(ncol(fit$loadings)))
  {
    for(i in 1:(nrow(fit$loadings)))
    {
      if(a[[i]][[2]]==j) {fact_item_mat[i,j]=a[[i]][[1]]}
    }    
  }
  nam=names(fit$loadings[,1])
  factor=list()
  for(i in 1:ncol(fit$loadings))
  {
    factor[[i]]=sort(fact_item_mat[,i], decreasing = FALSE, na.last = NA)
    fac=factor[[i]]
    fac=nam[fac]
    factor[[i]]=fac
  }
  names(factor)=paste("factor", 1:ncol(fit$loadings), sep="")
  return(factor)
}

What steps should I take now to avoid this error?


回答1:


Running your code and debugging your function (using debug function) I can see why you're having a "subscript out of bound" error:

  • the 15th element (among other) of your variable a is of length 1 so R is not happy when you're trying to reach a[[15]][2]...
  • the reason why this element is only of length one instead of 2 is because the maximum absolute value of factor is reached for a negative value and you're asking which value (not absolute) is equal to this maximal absolute value, so the answer is none...

Hence you need to change the line
which(fit$loadings[i,]==max(abs(fit$loadings[i,]))) to which(abs(fit$loadings[i,])==max(abs(fit$loadings[i,])))
and you'll get:

Divide_item_Factor_Loading(fit)
#$factor1
 #[1] "Item.1"  "Item.4"  "Item.6"  "Item.7"  "Item.8"  "Item.9"  "Item.10" "Item.11" "Item.13" "Item.14" "Item.15"
#[12] "Item.17" "Item.19" "Item.20" "Item.24" "Item.26" "Item.27" "Item.28" "Item.29"

#$factor2
 #[1] "Item.2"  "Item.3"  "Item.5"  "Item.12" "Item.16" "Item.18" "Item.21" "Item.22" "Item.23" "Item.25" "Item.30"
#[12] "Item.31" "Item.32"

Even if the debugged function will now work, I think you should change it because this is more complicated than it should be.

My proposition for an alternative function:

Divide_item_Factor_Loading_v2<-function(fit){
     a<-apply(fit$loadings,1,function(facs) which(abs(facs)==max(abs(facs))))
     return(list(factor1=names(a)[a==1],factor2=names(a)[a==2]))
}

This gives for your fit object the exact same result as your (debugged) function:

Divide_item_Factor_Loading_v2(fit)
#$factor1
 #[1] "Item.1"  "Item.4"  "Item.6"  "Item.7"  "Item.8"  "Item.9"  "Item.10" "Item.11" "Item.13" "Item.14" "Item.15"
#[12] "Item.17" "Item.19" "Item.20" "Item.24" "Item.26" "Item.27" "Item.28" "Item.29"

#$factor2
 #[1] "Item.2"  "Item.3"  "Item.5"  "Item.12" "Item.16" "Item.18" "Item.21" "Item.22" "Item.23" "Item.25" "Item.30"
#[12] "Item.31" "Item.32"



回答2:


To change the way the loadings are printed, use the cutoff argument to print.loadings.

Try something like this:

print(fit$loadings, cutoff=0)

The actual matrix contains all the values.

print(loadings(fit), cutoff=0)

Loadings:
       Factor1 Factor2
Item 1 0.014   0.418  
Item 2 0.130   0.350  
Item 3 0.036   0.553  
Item 4 0.166   0.294  
Item 5 0.990   0.125  

               Factor1 Factor2
SS loadings      1.025   0.705
Proportion Var   0.205   0.141
Cumulative Var   0.205   0.346

Now extract the maximum loading on each factor, using apply():

apply(loadings(fit), 2, max)

  Factor1   Factor2 
0.9895743 0.5531770 



回答3:


Check ?loadings, what you'll find out that there is a cutoff parameter that defines a value that "loadings smaller than this (in absolute value) are suppressed".



来源:https://stackoverflow.com/questions/28321135/subscript-out-of-bound-error-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!