简体   繁体   English

如何使用 R 中的多路包在并行因子分析中为非负性设置约束?

[英]How to set constraints for non-negativity in parallel factor analysis using the multiway package in R?

I want to perform a Parallel Factor Analysis (PARAFAC) with data from Fluorescence spectroscopy.我想使用荧光光谱数据进行平行因子分析 (PARAFAC)。 I have a three dimensional array (39x151x43) that I pass to the parafac function in the "multiway" package.我有一个三维数组 (39x151x43),我将它传递给“multiway”包中的 parafac 函数。 So far, it works well but I get negative values for some of the components.到目前为止,它运行良好,但我得到了一些组件的负值。

I read in the manual that one can specify constraints for non-negativity.我在手册中读到,可以为非负性指定约束。 However, I don't understand how the vector for the constraints option should look like.但是,我不明白约束选项的向量应该是什么样子。 When I try to do the following I get an error that says "Input 'const' must be 3 element vector specifying constraint for each mode"当我尝试执行以下操作时,我收到一条错误消息,指出“输入‘const’必须是为每种模式指定约束的 3 元素向量”

#### creating dummy values for Stackoverflow ####
A <- c(1:39)
B <- rnorm(151, mean =1, sd=0.5)
C <- rnorm(43, mean=1, sd = 0.5)

myArray <- array(c(A,B,C), dim = c(39,151,43))
dim(myArray)

library(multiway)  # load the library
myModel <- parafac(myArray, nfac = 3) # how to set const? 
#Input 'const' must be  3  element vector specifying constraint for each mode

Some research was to no avail.一些研究无济于事。 How should the vector look like so that the parafac function accepts it?向量应该如何显示以便 parafac 函数接受它?

The parameters of constraints vector are unconstrained:0,orthogonal:1,non-negative:2.约束向量的参数为无约束:0,正交:1,非负:2。

If all three inputs A,B,C are to be non-negative the constraint vector should be const=c(2,2,2)如果所有三个输入 A、B、C 都为非负,则约束向量应为const=c(2,2,2)

#Always remember to use set.seed when performing random sampling
#this ensures the example is reproducible 

set.seed(123)
A <- c(1:39)
B <- rnorm(151, mean =1, sd=0.5)
C <- rnorm(43, mean=1, sd = 0.5)

myArray <- array(c(A,B,C), dim = c(39,151,43))
dim(myArray)

library(multiway)

#Without constraints  
myModel_Default <- parafac(myArray, nfac = 3) 

#Percentage of negative elements

sum(myModel_Default$A<0)/length(myModel_Default$A)
#[1] 0.3333333
sum(myModel_Default$B<0)/length(myModel_Default$B)
#[1] 0.6865342
sum(myModel_Default$C<0)/length(myModel_Default$C)
#[1] 0.3488372

#With constraints

myModel_NonNeg <- parafac(myArray, nfac = 3,const=c(2,2,2))

sum(myModel_NonNeg$A<0)/length(myModel_NonNeg$A)
#[1] 0
sum(myModel_NonNeg$B<0)/length(myModel_NonNeg$B)
#[1] 0
sum(myModel_NonNeg$C<0)/length(myModel_NonNeg$C)
#[1] 0 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM