File: get_things.R

package info (click to toggle)
r-cran-bdgraph 2.73%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,344 kB
  • sloc: cpp: 8,160; ansic: 157; makefile: 5
file content (396 lines) | stat: -rwxr-xr-x 13,878 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
#     Copyright (C) 2012 - 2021  Reza Mohammadi                                |
#                                                                              |
#     This file is part of BDgraph package.                                    |
#                                                                              |
#     BDgraph is free software: you can redistribute it and/or modify it under |
#     the terms of the GNU General Public License as published by the Free     |
#     Software Foundation; see <https://cran.r-project.org/web/licenses/GPL-3>.|
#                                                                              |
#     Maintainer: Reza Mohammadi <a.mohammadi@uva.nl>                          |
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |

## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
#  Get graph from class objects "sim", "graph", "bdgraph", "ssgraph", or "select"
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
get_graph = function( obj_G, cut = 0.5 )
{
    if( is.matrix( obj_G ) ) 
    {
        if( nrow( obj_G ) != ncol( obj_G ) ) stop( "Adjacency matrix must be squere" )
        
        if( ( sum( obj_G == 0 ) + sum( obj_G == 1 ) ) != ( ncol( obj_G ) ^ 2 ) ) 
            stop( "Elements of adjacency matrix must be 0 or 1" )
        
        G = unclass( obj_G )
    }else{
        if(   inherits( obj_G, "sim"     ) ) G <- unclass( obj_G $ G ) 
        if(   inherits( obj_G, "graph"   ) ) G <- unclass( obj_G ) 
        
        if( ( inherits( obj_G, "bdgraph" ) ) | ( inherits( obj_G, "ssgraph" ) ) ) G <- BDgraph::select( obj_G, cut = cut ) 
        if(   inherits( obj_G, "select"  ) ) G <- obj_G $ refit
        
        G = as.matrix( G )
    }
    
    return( G )    
}
    
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
get_g_prior = function( g.prior, p )
{
    if( is.data.frame( g.prior ) ) g.prior <- data.matrix( g.prior )
    if( inherits( g.prior, "dtCMatrix" ) ) g.prior = as.matrix( g.prior )
    if( ( inherits( g.prior, "bdgraph"  ) ) | ( inherits( g.prior, "ssgraph" ) ) ) g.prior <- BDgraph::plinks( g.prior )
    if( inherits( g.prior, "sim" ) ) 
    {
        K       = as.matrix( g.prior $ K )
        g.prior = abs( K / diag( K ) )
    }
    
    if( !is.matrix( g.prior ) )
    {
        if( ( g.prior <= 0 ) | ( g.prior >= 1 ) ) stop( "'g.prior' must be between 0 and 1" )
        g.prior = matrix( g.prior, p, p )
    }else{
        if( ( nrow( g.prior ) != p ) | ( ncol( g.prior ) != p ) ) stop( "'g.prior' and 'data' have non-conforming size" )
        if( any( g.prior < 0 ) || any( g.prior > 1 ) ) stop( "Elements of  matrix 'g.prior' must be between 0 and 1" )
    }
    
    g.prior[ lower.tri( g.prior, diag = TRUE ) ] <- 0
    g.prior = g.prior + t( g.prior )
    
    return( g.prior )
}
     
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
get_g_start = function( g.start, g_prior, p )
{
    if( is.matrix( g.start ) )
    {
        if( ( sum( g.start == 0 ) + sum( g.start == 1 ) ) != ( p ^ 2 ) ) 
            stop( "Elements of matrix 'g.start' must be 0 or 1" )
        
        G = g.start
    }
    
    if( inherits( g.start, "sim"   ) ) G <- unclass( g.start $ G )
    if( inherits( g.start, "graph" ) ) G <- unclass( g.start )
    
    if( ( inherits( g.start, "bdgraph"   ) ) |  ( inherits( g.start, "ssgraph" ) ) ) G <- g.start $ last_graph
    if( ( inherits( g.start, "character" ) ) && ( g.start == "empty" ) ) G = matrix( 0, p, p )
    if( ( inherits( g.start, "character" ) ) && ( g.start == "full"  ) ) G = matrix( 1, p, p )
    
    if( ( nrow( G ) != p ) | ( ncol( G ) != p ) ) 
        stop( "'g.start' and 'data' have non-conforming size" )
    
    G[ g_prior == 1 ] = 1
    G[ g_prior == 0 ] = 0
    
    G[ lower.tri( G, diag( TRUE ) ) ] <- 0
    G  = G + t( G )
    
    return( G = G )
}
     
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
get_K_start = function( G, g.start, Ts, b_star, threshold )
{
    p = ncol( G )
    
    if( ( inherits( g.start, "bdgraph" ) ) | ( inherits( g.start, "ssgraph" ) ) ) 
        K <- g.start $ last_K
    
    if( inherits( g.start, "sim" ) )  K <- g.start $ K
    
    if( ( !inherits( g.start, "bdgraph" ) ) && ( !inherits( g.start, "ssgraph" ) ) && ( !inherits( g.start, "sim" ) ) )
    {
        K = G
        
        result = .C( "rgwish_c", as.integer(G), as.double(Ts), K = as.double(K), as.integer(b_star), as.integer(p), as.double(threshold), PACKAGE = "BDgraph" )
        K      = matrix( result $ K, p, p ) 
    }
    
    return( K = K )
}
    
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
get_S_n_p = function( data, method, n, not.cont = NULL )
{
    if( inherits( data, "sim" ) )
    {
        not.cont <- data $ not.cont  # Do not change the order of these links
        data     <- data $ data
    }
    
    if( !is.matrix( data ) & !is.data.frame( data ) ) 
        stop( "'data' must be a matrix or dataframe" )
    
    if( is.data.frame( data ) ) 
        data <- data.matrix( data )
    
    if( any( is.na( data ) ) ) 
    {
        if( method == "dw"  ) stop( "'bdgraph.dw()' does not deal with missing values" )	
        if( method == "ggm" ) stop( "'ggm' method does not deal with missing values. You could choose option 'method = \"gcgm\"'" )	
        if( method == "tgm" ) stop( "'tgm' method does not deal with missing values. You could choose option 'method = \"gcgm\"'" )	
        
        gcgm_NA = 1
    }else{
        gcgm_NA = 0
    }
    
    if( isSymmetric( data ) )
    {
        if( method == "gcgm" ) stop( "'method = \"gcgm\"' requires all data" )
        if( method == "dw"   ) stop( "'method = \"dw\"' requires all data" )
        if( method == "tgm"  ) stop( "'method = \"tgm\"' requires all data" )
        if( is.null( n )     ) stop( "Please specify the number of observations 'n'" )
    }
    
    p <- ncol( data )
    
    if( p < 3 ) 
        stop( "Number of variables/nodes ('p') must be more than 2" )
    
    if( is.null( n ) ) 
        n <- nrow( data )
    
    if( method == "ggm" ) 
    {
        if( isSymmetric( data ) )
        {
            cat( "Input is identified as the covariance matrix \n" )
            S <- data
        }else{
            S <- t( data ) %*% data
        }
    }
    
    if( method == "gcgm" )
    {
        if( is.null( not.cont ) )
        {
            not.cont = c( rep( 1, p ) )
            
            for( j in 1:p )
                if( length( unique( data[ , j ] ) ) > min( n / 2 ) ) 
                    not.cont[ j ] = 0
        }else{
            
            if( !is.vector( not.cont )  ) 
                stop( "'not.cont' must be a vector with length of number of variables" )
            
            if( length( not.cont ) != p ) 
                stop( "'not.cont' must be a vector with length of number of variables" )
            
            if( ( sum( not.cont == 0 ) + sum( not.cont == 1 ) ) != p ) 
                stop( "Elements of vector 'not.cont' must be 0 or 1" )
        }
        
        R <- 0 * data
        
        for( j in 1:p )
            if( not.cont[ j ] )
                R[ , j ] = match( data[ , j ], sort( unique( data[ , j ] ) ) ) 
        
        R[ is.na( R ) ] = -1000     # dealing with missing values	
        
        # copula for continuous non-Gaussian data
        if( ( gcgm_NA == 0 ) && ( min( apply( R, 2, max ) ) > ( n - 5 * n / 100 ) ) )
        {
            # copula transfer 
            data = stats::qnorm( apply( data, 2, rank ) / ( n + 1 ) )
            
            data = t( ( t( data ) - apply( data, 2, mean ) ) / apply( data, 2, stats::sd ) )
            
            method = "ggm"
        }else{	
            # for non-Gaussian data
            Z                  <- stats::qnorm( apply( data, 2, rank, ties.method = "random" ) / ( n + 1 ) )
            Zfill              <- matrix( stats::rnorm( n * p ), n, p )   # for missing values
            Z[ is.na( data ) ] <- Zfill[ is.na( data ) ]                  # for missing values
            Z                  <- t( ( t( Z ) - apply( Z, 2, mean ) ) / apply( Z, 2, stats::sd ) )
            S                  <- t( Z ) %*% Z
        }
    } 
    
    if( method == "dw" )
    {
        if( is.null( not.cont ) )
        {
            not.cont = c( rep( 1, p ) )
        }else{
            if( !is.vector( not.cont )  ) stop( "'not.cont' must be a vector with length of number of variables" )
            if( length( not.cont ) != p ) stop( "'not.cont' must be a vector with length of number of variables" )
            if( ( sum( not.cont == 0 ) + sum( not.cont == 1 ) ) != p ) stop( "Elements of vector 'not.cont' must be 0 or 1" )
        }
        
        # for non-Gaussian data
        Z                  <- stats::qnorm( apply( data, 2, rank, ties.method = "random" ) / ( n + 1 ) )
        Zfill              <- matrix( stats::rnorm( n * p ), n, p )   # for missing values
        Z[ is.na( data ) ] <- Zfill[ is.na( data ) ]                  # for missing values
        Z                  <- t( ( t( Z ) - apply( Z, 2, mean ) ) / apply( Z, 2, stats::sd ) )
        S                  <- t( Z ) %*% Z
    } 
    
    if( method == "tgm" )
        S <- t( data ) %*% data
    
    list_out = list( method = method, S = S, n = n, p = p, colnames_data = colnames( data ), data = data )
    
    if( method == "gcgm" )
    {
        list_out$not.cont = not.cont
        list_out$gcgm_NA  = gcgm_NA
        list_out$Z        = Z
        list_out$R        = R
    }

    if( method == "dw" )
    {
        list_out$not.cont = not.cont
        list_out$gcgm_NA  = gcgm_NA
        list_out$Z        = Z
    }
        
    return( list_out )
} 
    
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
get_Ds_tgm_R = function( data, tu, mu, D, n, p, in_C = TRUE )
{
    if( in_C == TRUE )
    {
        S  = matrix( 0, p, p )
        Ds = matrix( 0, p, p )
        
        # void get_Ds_tgm( double data[], double D[], double mu[], double tu[], double Ds[], double S[], int *n, int *p )
        result = .C( "get_Ds_tgm", as.double(data), as.double(D), as.double (mu), as.double(tu), 
                     Ds = as.double(Ds), S = as.double(S), as.integer(n), as.integer(p), PACKAGE = "BDgraph" )
        
        S  = matrix( result $ S , p, p ) 
        Ds = matrix( result $ Ds, p, p ) 
    }else{
        
        #X_new = matrix( nrow = n, ncol = p )
    	#for( i in 1:n ){
        #    sqrt_tu_i = sqrt( tu[ i ] );
        #    for( j in 1:p ) X_new[ i, j ] = sqrt_tu_i * ( data[ i, j ] - mu[ j ] );
    	#}
    	#S  = t( X_new ) %*% X_new
    	
    	S  = matrix( 0, p, p )
    	for( i in 1:p )
    	    for( j in 1:p )
    	        for( k in 1:n )
    	            S[ i, j ] = S[ i, j ] + tu[ k ] * ( data[ k, i ] - mu[ i ] ) * ( data[ k, j ] - mu[ j ] );
        
    	Ds = D + S
    }
    
	return( list( S = S, Ds = Ds ) )
}

## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
get_Ts_R = function( Ds, in_C = TRUE )
{
    if( in_C == TRUE )
    {
        p = ncol( Ds )
        
        Ts      = matrix( 0, p, p )
        inv_Ds  = matrix( 0, p, p )
        copy_Ds = matrix( 0, p, p )
       
        # void get_Ts( double Ds[], double Ts[], double inv_Ds[], double copy_Ds[], int *p )
        result = .C( "get_Ts", as.double(Ds), Ts = as.double(Ts), as.double(inv_Ds), as.double(copy_Ds), as.integer(p), PACKAGE = "BDgraph" )
        
        Ts = matrix( result $ Ts, p, p ) 
        
    }else{
    	invDs = solve( Ds )
	    Ts    = chol( invDs )
    }

	return( Ts )    
}
    
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
update_tu_R = function( tu, data, K, mu, nu, n, p, in_C = TRUE )
{
    if( in_C == TRUE )
    {
        Ts      = matrix( 0, p, p )
        inv_Ds  = matrix( 0, p, p )
        copy_Ds = matrix( 0, p, p )
       
        # void update_tu( double data[], double K[], double tu[], double mu[], double *nu, int *n, int *p )
        result = .C( "update_tu", as.double(data), as.double(K), tu = as.double(tu), as.double(mu), as.double(nu), as.integer(n), as.integer(p), PACKAGE = "BDgraph" )
        
        tu = c( result $ tu ) 
        
    }else{
        d_mu_i = numeric( p )
        
        for( i in 1:n )
    	{
            for( j in 1:p )
                d_mu_i[ j ] = data[ i, j ] - mu[ j ];
                
            #d_mu_i_x_K = d_mu_i %*% K;
            #delta_y_i  = sum( d_mu_i_x_K * d_mu_i );
            
            delta_y_i = 0.0;
            for( k in 1:p )
                for( l in 1:p )
                    delta_y_i = delta_y_i + d_mu_i[ l ] * K[ l, k ] * d_mu_i[ k ];
                
    		shape_tu_i = ( nu + p ) / 2.0;
    		rate_tu_i  = ( nu + delta_y_i ) / 2.0;
    			
    		tu[ i ] = stats::rgamma( 1, shape = shape_tu_i, scale = 1.0 / rate_tu_i )
        }
    }

	return( tu )    
}

## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
update_mu_R = function( data, mu, tu, n, p, in_C = TRUE )
{
    if( in_C == TRUE )
    {
        # void update_mu( double data[], double mu[], double tu[], int *n, int *p )
        result = .C( "update_mu", as.double(data), mu = as.double(mu), as.double(tu), as.integer(n), as.integer(p), PACKAGE = "BDgraph" )
        
        mu = c( result $ mu ) 
        
    }else{
        mu = c( tu %*% data / sum( tu ) )
    }

	return( mu )    
}
                      
## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |