File: repnormmixinit.R

package info (click to toggle)
r-cran-mixtools 2.0.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,944 kB
  • sloc: ansic: 781; makefile: 6
file content (41 lines) | stat: -rwxr-xr-x 1,104 bytes parent folder | download | duplicates (7)
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
repnormmix.init = function (x, lambda = NULL, mu = NULL, s = NULL, k = 2, arbmean = TRUE, arbvar = TRUE) 
{
    n <- ncol(x)
    m <- nrow(x)
    y <- apply(x, 2, mean)
    x <- x[, order(y)]
    x.bin = list()
    for (j in 1:k) {
        x.bin[[j]] <- x[, max(1, floor((j - 1) * n/k)):ceiling(j * 
            n/k)]
    }
    if (is.null(s)) {
        s.hyp = sapply(lapply(x.bin, as.vector), sd)
        if (arbvar) {
            s = 1/rexp(k, rate = s.hyp)
        }
        else {
            s.hyp = mean(s.hyp)
            s = 1/rexp(1, rate = s.hyp)
        }
    }
    if (is.null(s) == FALSE && arbvar == TRUE) {
        k = length(s)
    }
    if (is.null(mu)) {
        mu.hyp <- sapply(lapply(x.bin, as.vector), mean)
        mu = rnorm(k, mean = mu.hyp, sd = s)
		if(arbmean==FALSE){
		mu = mean(mu)
		}
    }
	if (is.null(mu)==FALSE && arbmean==TRUE){
    		k = length(mu)
	}
    if (is.null(lambda)) {
        lambda = runif(k)
        lambda = lambda/sum(lambda)
    }
    else k = length(lambda)
    list(lambda = lambda, mu = mu, s = s, k = k)
}