@@ -2441,15 +2441,20 @@ mild <- function( dataFrame, voxmats, basisK,
2441
2441
# ' k = 2, uAlgorithm = 'pca' )
2442
2442
# '
2443
2443
# ' @export
2444
- initializeSimlr <- function ( voxmats , k , jointReduction = TRUE ,
2444
+ initializeSimlr <- function ( voxmats , k , jointReduction = FALSE ,
2445
2445
zeroUpper = FALSE , uAlgorithm = ' svd' , addNoise = 0 ) {
2446
2446
nModalities = length( voxmats )
2447
+ localAlgorithm = uAlgorithm
2448
+ if ( uAlgorithm == ' avg' ) {
2449
+ jointReduction = FALSE
2450
+ localAlgorithm = ' pca'
2451
+ }
2447
2452
if ( uAlgorithm == ' randomProjection' & jointReduction )
2448
2453
jointReduction = FALSE
2449
2454
if ( jointReduction ) {
2450
2455
X <- Reduce( cbind , voxmats ) # bind all matrices
2451
- if ( uAlgorithm == ' pca' ) {
2452
- X.pcr <- stats :: prcomp( t(X ), rank. = k ) # PCA
2456
+ if ( uAlgorithm == ' pca' | uAlgorithm == ' svd ' ) {
2457
+ X.pcr <- stats :: prcomp( t(X ), rank. = k , scale. = uAlgorithm == ' svd ' ) # PCA
2453
2458
u <- ( X.pcr $ rotation )
2454
2459
} else if ( uAlgorithm == ' ica' ) {
2455
2460
u = t( fastICA :: fastICA( t(X ), method = ' C' , n.comp = k )$ A )
@@ -2468,18 +2473,19 @@ initializeSimlr <- function( voxmats, k, jointReduction = TRUE,
2468
2473
uOut = list ()
2469
2474
uRand = replicate(k , rnorm(nrow(voxmats [[1 ]])))
2470
2475
for ( s in 1 : nModalities ) {
2471
- if ( uAlgorithm == ' pca ' ) {
2472
- uOut [[s ]] = t( stats :: prcomp( t(voxmats [[s ]]), rank. = k )$ rotation )
2473
- } else if ( uAlgorithm == ' ica' ) {
2474
- uOut [[s ]] = t( fastICA :: fastICA( t(voxmats [[s ]]), method = ' C' , n.comp = k )$ A )
2475
- } else if ( uAlgorithm == ' cca' ) {
2476
- uOut [[s ]] = sparseDecom2( list (voxmats [[1 ]], voxmats [[s ]]),
2476
+ X = Reduce( cbind , voxmats [ - s ] )
2477
+ if ( localAlgorithm == ' pca ' | localAlgorithm == ' svd' ) {
2478
+ uOut [[s ]] = ( stats :: prcomp( t( X ), rank. = k , scale. = uAlgorithm == ' svd' )$ rotation )
2479
+ } else if ( localAlgorithm == ' ica' ) {
2480
+ uOut [[s ]] = t( fastICA :: fastICA( t( X ), method = ' C' , n.comp = k )$ A )
2481
+ } else if ( localAlgorithm == ' cca' ) {
2482
+ uOut [[s ]] = sparseDecom2( list ( X , voxmats [[s ]]),
2477
2483
sparseness = c(0.5 ,0.5 ), nvecs = k , its = 3 , ell1 = 0.1 )$ projections2
2478
- } else if ( uAlgorithm == ' randomProjection' ) {
2484
+ } else if ( localAlgorithm == ' randomProjection' ) {
2479
2485
uOut [[s ]] = t( ( t(uRand ) %*% voxmats [[s ]] ) %*% t( voxmats [[s ]] ) )
2480
2486
uOut [[s ]] = uOut [[s ]] / norm( uOut [[s ]], " F" )
2481
2487
} else {
2482
- uOut [[s ]] = replicate( k , rnorm(nrow( voxmats [[ 1 ]])) )
2488
+ uOut [[s ]] = ( stats :: prcomp( t( X ), rank. = k , scale. = uAlgorithm == ' svd ' ) $ rotation )
2483
2489
}
2484
2490
if ( addNoise > 0 ) uOut [[s ]] = uOut [[s ]] + replicate(k , rnorm(nrow(voxmats [[1 ]]))) * addNoise
2485
2491
if ( zeroUpper ) uOut [[s ]][upper.tri(uOut [[s ]])] <- 0
@@ -2683,25 +2689,25 @@ simlr <- function(
2683
2689
sparsenessQuantiles ,
2684
2690
positivities ,
2685
2691
initialUMatrix ,
2686
- mixAlg = c( ' svd' , ' ica' , ' avg' , ' rrpca-l' , ' rrpca-s' , ' pca' , ' stochastic' ),
2692
+ mixAlg = c( ' svd' , ' ica' , ' avg' , ' rrpca-l' , ' rrpca-s' , ' pca' , ' stochastic' ),
2687
2693
orthogonalize = FALSE ,
2688
2694
repeatedMeasures = NA ,
2689
2695
lineSearchRange = c( - 1e10 , 1e10 ),
2690
2696
lineSearchTolerance = 1e-8 ,
2691
2697
randomSeed ,
2692
- constraint = c(" Grassmann " ," none " ," Stiefel" ),
2693
- energyType = c(' regression ' , ' normalized ' , ' cca ' , ' ucca' , ' lowRank' ),
2698
+ constraint = c(" none " ," Grassmann " ," Stiefel" ),
2699
+ energyType = c(' cca ' , ' regression ' , ' normalized ' , ' ucca' , ' lowRank' ),
2694
2700
vmats ,
2695
2701
connectors = NULL ,
2696
2702
optimizationStyle = c(" lineSearch" ," mixed" ," greedy" ) ,
2697
2703
scale = c( ' sqrtnp' , ' np' , ' centerAndScale' , ' center' , ' norm' , ' none' , ' impute' , ' eigenvalue' , ' robust' ),
2698
2704
expBeta = 0.0 ,
2699
2705
verbose = FALSE ) {
2700
- if ( missing( scale ) ) scale = c( " centerAndScale" , " np " )
2701
- if ( missing( energyType ) ) energyType = " regression "
2702
- if ( missing( mixAlg ) ) mixAlg = " ica "
2706
+ if ( missing( scale ) ) scale = c( " centerAndScale" )
2707
+ if ( missing( energyType ) ) energyType = " cca "
2708
+ if ( missing( mixAlg ) ) mixAlg = " svd "
2703
2709
if ( missing( optimizationStyle ) ) optimizationStyle = " lineSearch"
2704
- if ( ! missing( " randomSeed" ) ) set.seed( randomSeed )
2710
+ if ( ! missing( " randomSeed" ) ) set.seed( randomSeed ) # else set.seed( 0 )
2705
2711
energyType = match.arg(energyType )
2706
2712
constraint = match.arg(constraint )
2707
2713
optimizationStyle = match.arg(optimizationStyle )
@@ -2717,6 +2723,19 @@ simlr <- function(
2717
2723
}
2718
2724
# \sum_i \| X_i - \sum_{ j ne i } u_j v_i^t \|^2 + \| G_i \star v_i \|_1
2719
2725
# \sum_i \| X_i - \sum_{ j ne i } u_j v_i^t - z_r v_r^ T \|^2 + constraints
2726
+ #
2727
+ constrainG <- function ( vgrad , i , constraint ) {
2728
+ if ( constraint == " Grassmann" ) {
2729
+ # grassmann manifold - see https://stats.stackexchange.com/questions/252633/optimization-with-orthogonal-constraints
2730
+ # Edelman, A., Arias, T. A., & Smith, S. T. (1998). The geometry of algorithms with orthogonality constraints. SIAM journal on Matrix Analysis and Applications, 20(2), 303-353.
2731
+ projjer = diag( ncol( vgrad ) ) - t( vmats [[i ]] ) %*% vmats [[i ]]
2732
+ return ( vgrad %*% projjer )
2733
+ }
2734
+ if ( constraint == " Stiefel" ) # stiefel manifold
2735
+ vgrad = vgrad - vmats [[i ]] %*% ( t( vgrad ) %*% ( vmats [[i ]] ) )
2736
+ return ( vgrad )
2737
+ }
2738
+
2720
2739
normalized = FALSE
2721
2740
ccaEnergy = FALSE
2722
2741
nModalities = length( voxmats )
@@ -2827,6 +2846,8 @@ simlr <- function(
2827
2846
initialUMatrix = list ( )
2828
2847
for ( i in 1 : nModalities )
2829
2848
initialUMatrix [[ i ]] = randmat
2849
+ } else if ( class(initialUMatrix )[1 ] == ' numeric' ) {
2850
+ initialUMatrix = initializeSimlr( voxmats , initialUMatrix , uAlgorithm = mixAlg , jointReduction = FALSE )
2830
2851
}
2831
2852
2832
2853
if ( length( initialUMatrix ) != nModalities &
@@ -2846,9 +2867,12 @@ simlr <- function(
2846
2867
if ( verbose ) print(" <0> BUILD-V <0> BUILD-V <0> BUILD-V <0> BUILD-V <0> " )
2847
2868
vmats = list ()
2848
2869
for ( i in 1 : nModalities ) {
2849
- # vmats[[ i ]] = t(voxmats[[i]]) %*% initialUMatrix[[i]]
2850
- vmats [[ i ]] = matrix ( rnorm( ncol( voxmats [[i ]] ) * basisK ), ncol = basisK )
2851
- vmats [[ i ]] = vmats [[ i ]] / norm( vmats [[ i ]], " F" )
2870
+ vmats [[ i ]] = t(voxmats [[i ]]) %*% initialUMatrix [[i ]]
2871
+ # 0 # svd( temp, nu=basisK, nv=0 )$u
2872
+ # for ( kk in 1:nModalities ) vmats[[ i ]] = vmats[[ i ]] + t(voxmats[[i]]) %*% initialUMatrix[[kk]]
2873
+ # vmats[[ i ]] = # svd( vmats[[ i ]], nu=basisK, nv=0 )$u
2874
+ # ( stats::prcomp( vmats[[ i ]], retx=TRUE, rank.=basisK, scale.=TRUE )$x )
2875
+ # vmats[[ i ]] = vmats[[ i ]] / norm( vmats[[ i ]], "F" )
2852
2876
}
2853
2877
}
2854
2878
@@ -2867,7 +2891,7 @@ simlr <- function(
2867
2891
as.matrix( smoothingMatrices [[whichModality ]] %*% myenergysearchv ),
2868
2892
sparsenessQuantiles [whichModality ],
2869
2893
orthogonalize = FALSE ,
2870
- positivity = positivities [whichModality ], softThresholding = FALSE )
2894
+ positivity = positivities [whichModality ], softThresholding = TRUE )
2871
2895
2872
2896
if ( ccaEnergy ) {
2873
2897
# ( v'*X'*Y )/( norm2(X*v ) * norm2( u ) )
@@ -2913,17 +2937,6 @@ simlr <- function(
2913
2937
totalEnergy = c( initialEnergy )
2914
2938
if ( verbose ) print( paste( " initialDataTerm:" , initialEnergy ,
2915
2939
" <o> mixer:" , mixAlg , " <o> E: " , energyType ) )
2916
- constrainG <- function ( vgrad , i , constraint ) {
2917
- if ( constraint == " Grassmann" ) {
2918
- # grassmann manifold - see https://stats.stackexchange.com/questions/252633/optimization-with-orthogonal-constraints
2919
- # Edelman, A., Arias, T. A., & Smith, S. T. (1998). The geometry of algorithms with orthogonality constraints. SIAM journal on Matrix Analysis and Applications, 20(2), 303-353.
2920
- projjer = diag( ncol( vgrad ) ) - t( vmats [[i ]] ) %*% vmats [[i ]]
2921
- return ( vgrad %*% projjer )
2922
- }
2923
- if ( constraint == " Stiefel" ) # stiefel manifold
2924
- vgrad = vgrad - vmats [[i ]] %*% ( t( vgrad ) %*% ( vmats [[i ]] ) )
2925
- return ( vgrad )
2926
- }
2927
2940
2928
2941
getSyMGnorm <- function ( v , i , myw , mixAlg ) {
2929
2942
# norm2( X/norm2(X) - U * V'/norm2(U * V') )^2
@@ -3113,7 +3126,7 @@ simlr <- function(
3113
3126
sparsenessQuantiles [i ],
3114
3127
orthogonalize = FALSE , positivity = positivities [i ],
3115
3128
unitNorm = FALSE ,
3116
- softThresholding = FALSE )
3129
+ softThresholding = TRUE )
3117
3130
if ( normalized ) vmats [[i ]] = vmats [[i ]] / norm( vmats [[i ]], " F" )
3118
3131
}
3119
3132
if ( ccaEnergy ) {
@@ -3220,6 +3233,7 @@ simlr <- function(
3220
3233
# '
3221
3234
# ' @seealso \code{\link{simlr}}
3222
3235
# ' @export
3236
+
3223
3237
simlrU <- function ( projections , mixingAlgorithm , initialW ,
3224
3238
orthogonalize = FALSE , connectors = NULL ) {
3225
3239
# some gram schmidt code
0 commit comments