NeuralAnalysis / TrialData

Code base for the Matlab TrialData analysis library.
3 stars 4 forks source link

Implementation of factor analysis in `dimReduce` is wrong? #11

Closed raeedcho closed 2 years ago

raeedcho commented 2 years ago

I'm not 100% sure, but I think the implementation of factor analysis in the dimReduce function is not quite correct. In particular, the projection of the data into factor space just post-multiplies the signal matrix by w, but the result doesn't match the scores output of factoran. Also, my understanding is that w for factor analysis is the loading matrix to convert factors into original signals, not the other way around--to go backwards, there needs to be some sort of inversion of the matrix, or an explicit estimation of the posterior mean of the latents.

Possible fix would be to use fastfa to estimate the model and fastfa_estep to do the posterior estimation--both functions are from Byron Yu's GPFA codepack, which is included in the td_limblab folder.

mattperich commented 2 years ago

Thanks for catching this. I must admit I've never really used the FA implementation for any real purpose and it was obviously shoehorned it into the PCA implementation. Sounds like we should remove this functionality for now and re-implement using the scores output just to be sure it's all correct?

raeedcho commented 2 years ago

I would say re-implementing with the scores output would make sense, if I could figure out how to estimate scores on new data with an already fit model using factoran. Thinking about it more, I believe that it would be pretty simple to use fastfa and fastfa_estep to do it. The code already uses orthogonalize from the GPFA toolbox, so it wouldn't introduce any extra dependencies.

raeedcho commented 2 years ago

Not sure if this is how we want to do it here, but I submitted a pull request with a fix (https://github.com/NeuralAnalysis/TrialData/pull/12). @mattperich, take a look if you want, but if you don't care too much, I'll just merge it.