1MD <-
2function (X, y, nFac, nBlk = 0, mInt = 3, g = 2, nMod, p, s2,
3    nf, facs, nFDes = 4, Xcand, mIter = 20, nStart = 5, startDes = NULL,
4    top = 20, eps = 1e-05)
5{
6    if (nFac + nBlk != ncol(X))
7        stop("nFac + nBlk != ncol(X)")
8    if (nFac + nBlk != ncol(Xcand))
9        stop("nFac + nBlk != ncol(Xcand)")
10    if (ncol(Xcand) != ncol(X))
11        stop("ncol(Xcand) != ncol(X)")
12    ITMAX <- as.integer(mIter)
13    N0 <- as.integer(nrow(X))
14    NRUNS <- as.integer(nFDes)
15    N <- as.integer(nrow(Xcand))
16    X <- as.matrix(X)
17    storage.mode(X) <- "double"
18    Y <- as.double(y)
19    GAMMA <- as.double(g[1])
20    GAM2 <- as.double(0)
21    if (length(g) > 1)
22        GAM2 <- as.double(g[2])
23    COLS <- as.integer(nFac)
24    BL <- as.integer(nBlk)
25    CUT <- as.integer(mInt)
26    GAMMA <- as.double(g[1])
27    if (length(g) == 1) {
28        IND <- as.integer(0)
29    }
30    else {
31        IND <- as.integer(1)
32        GAM2 <- as.double(g[2])
33    }
34    Xcand <- as.matrix(Xcand)
35    storage.mode(Xcand) <- "double"
36    NM <- as.integer(nMod)
37    P <- as.double(as.numeric(p))
38    SIGMA2 <- as.double(as.numeric(s2))
39    NF <- as.integer(as.numeric(nf))
40    MNF <- as.integer(max(NF))
41    JFAC <- as.matrix(facs)
42    storage.mode(JFAC) <- "integer"
43    if (is.null(startDes)) {
44        if (is.null(nStart))
45            stop("nStart needed when startDes is NULL")
46        INITDES <- as.integer(1)
47        NSTART <- as.integer(nStart)
48        MBEST <- matrix(0, nrow = NSTART, ncol = NRUNS)
49        storage.mode(MBEST) <- "integer"
50    }
51    else {
52        INITDES <- as.integer(0)
53        startDes <- as.matrix(startDes)
54        NSTART <- as.integer(nrow(startDes))
55        if (ncol(startDes) != NRUNS)
56            stop("ncol(startDes) should be nFDes")
57        MBEST <- as.matrix(startDes)
58        storage.mode(MBEST) <- "integer"
59    }
60    NTOP <- as.integer(top)
61    TOPD <- as.double(rep(0, NTOP))
62    TOPDES <- matrix(0, nrow = NTOP, ncol = NRUNS)
63    dimnames(TOPDES) <- list(seq(top), paste("r", seq(NRUNS),
64        sep = ""))
65    storage.mode(TOPDES) <- "integer"
66    EPS <- as.double(eps)
67    flag <- as.integer(-1)
68    lst <- .Fortran("md", NSTART, NRUNS, ITMAX, INITDES, N0,
69        IND, X, Y, GAMMA, GAM2, BL, COLS, N, Xcand, NM, P, SIGMA2,
70        NF, MNF, JFAC, CUT, MBEST, NTOP, TOPD, TOPDES, EPS, flag,
71        PACKAGE = "BsMD")
72    names(lst) <- c("NSTART", "NRUNS", "ITMAX", "INITDES", "N0",
73        "IND", "X", "Y", "GAMMA", "GAM2", "BL", "COLS", "N",
74        "Xcand", "NM", "P", "SIGMA2", "NF", "MNF", "JFAC", "CUT",
75        "MBEST", "NTOP", "TOPD", "TOPDES", "EPS", "flag")
76    invisible(structure(lst, class = c("MD", class(lst))))
77}
78