matthias-da / robCompositions

Robust Methods for Compositional Data
11 stars 8 forks source link

pfa #17

Open matthias-da opened 2 years ago

matthias-da commented 2 years ago

Dear Authors, back to my previous question asked by email some days ago with no response, please have a look at the factor scores returned from robCompositions package in this example:

data(expenditures)
x <- expenditures
 res.rob <- pfa(x, factors=1, score="regression") 

according to ?pfa since the covariance is not specified,
the covariance is estimated from isometric log-ratio transformed data internally, but the data used for factor analysis are backtransformed to the clr space.

So the clr transformed data obtain as follows:

# ilr transformation
ilr <- function(x){
x.ilr=matrix(NA,nrow=nrow(x),ncol=ncol(x)-1)
  for (i in 1:ncol(x.ilr)){
    x.ilr[,i]=sqrt((i)/(i+1))*log(((apply(as.matrix(x[,1:i]), 1, prod))^(1/i))/(x[,i+1]))
  }
return(x.ilr)
}

# construct orthonormal basis: (matrix with ncol(x) rows and ncol(x)-1 columns)
V=matrix(0,nrow=ncol(x),ncol=ncol(x)-1)
for (i in 1:ncol(V)){
  V[1:i,i] <- 1/i
  V[i+1,i] <- (-1)
  V[,i] <- V[,i]*sqrt(i/(i+1))
}
z=ilr(x) #ilr transformed data
y=z%*%t(V) #clr transformed data

now the factor scores using regression method might be calculated as follows:

 loa<-c(0.970,0.830,0.986,0.876,0.977)  #res.rob object

facscores<- y%*%loa
> head(facscores)
             [,1]
[1,] -0.009485110
[2,]  0.009680645
[3,]  0.008426665
[4,] -0.015401000
[5,] -0.003610644
[6,] -0.004584145

but calling res.rob$scores returns us

head(res.rob$scores)
> head(res.rob$scores)
       Factor1
[1,] -755.2681
[2,]  705.5309
[3,] 4196.5652
[4,] -778.6955
[5,] -628.2141
[6,] -663.4534

so please check am I wrong or there is some bug in the pfa command?

One previous answer:

Yes, looks like a bug. I think the problem is

if (scores != "none" && !have.x)
        z <- x

I guess this should be "z <- y".