File: gmm_fit_em_001.R

package info (click to toggle)
r-cran-mixturefitting 0.6.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 448 kB
  • sloc: ansic: 518; makefile: 2
file content (39 lines) | stat: -rw-r--r-- 1,080 bytes parent folder | download
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
library( MixtureFitting )

set.seed(42)
p = c( 0.5, 0.5, 1.5, 10, 3, 1 )

x = c( rnorm(2000 * p[1], p[3], p[5]), rnorm(2000 * p[2], p[4], p[6]) )
init = gmm_init_vector( x, 2 )

gf = gmm_fit_em( x, init, implementation = "C" )
if( !all( abs( gf$p - c( 0.5, 0.5, 1.5, 10, 3, 1 ) ) < 0.1 ) ) {
    stop( 1, gf$p - p )
}

gf = gmm_fit_em( x, init, implementation = "R" )
if( !all( abs( gf$p - c( 0.5, 0.5, 1.5, 10, 3, 1 ) ) < 0.1 ) ) {
    stop( 2, gf$p - p )
}

gf = gmm_fit_em( x, init, x * 0 + 0.5, implementation = "C" )
if( !all( abs( gf$p - c( 0.5, 0.5, 1.5, 10, 3, 1 ) ) < 0.1 ) ) {
    stop( 3, gf$p - p )
}

gf = gmm_fit_em( x, init, x * 0 + 0.5, implementation = "R" )
if( !all( abs( gf$p - c( 0.5, 0.5, 1.5, 10, 3, 1 ) ) < 0.1 ) ) {
    stop( 4, gf$p - p )
}

init = gmm_init_vector( x, 1 )

gf = gmm_fit_em( x, init, c( numeric(1000)+1, numeric(1000)) )
if( !all( abs( gf$p - c( 1, 1.5, 3 ) ) < 0.1 ) ) {
    stop( 5, gf$p - p )
}

gf = gmm_fit_em( x, init, c( numeric(1000), numeric(1000)+1) )
if( !all( abs( gf$p - c( 1, 10, 1 ) ) < 0.1 ) ) {
    stop( 6, gf$p - p )
}