fit.model: Builds and Fits the Mixed Effects Model Fit Random Effects...

Description Usage Arguments Author(s) Examples

Description

Function estimates the binned matrices and fits the random effects models Function designed to fit the random effects model to the data.

Usage

1
fit.model(obs.fit, snm.obj, basisSplineFunction)

Arguments

obs.fit

List of estimated coefficient matrices and residuals from full and reduced models

snm.obj

An object of class snm

basisSplineFunction

Basis spline function

Author(s)

Brig Mecham <brig.mecham@sagebase.org> and John D. Storey <jstorey@princeton.edu>

Examples

 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
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (obs.fit,snm.obj,
          basisSplineFunction)
{
  snm.obj$M <- snm.obj$dat - obs.fit$res1
  snm.obj$M[snm.obj$nulls,] <- obs.fit$fit0[snm.obj$nulls,]

# Split the data into nbins bins based on their mean intensities
  bins <- getSpanningSet(snm.obj)
  
# Build the matrix of weighted raw data and matrix of weighted fitted values for each bin.
  lnp <- length(bins)
  np <- 1:lnp
  Y.pooled <- 0*snm.obj$dat[np,]
  M.pooled <- 0*snm.obj$M[np,]
  for(i in 1:lnp) {
    Y.pooled[i,] = apply(matrix(snm.obj$r.dat[as.vector(bins[[i]]),], ncol=ncol(snm.obj$dat)),2,
              weighted.mean, w=snm.obj$weights[as.vector(bins[[i]])])
    M.pooled[i,] = apply(matrix(snm.obj$M[as.vector(bins[[i]]),], ncol=ncol(snm.obj$M)),2,
              weighted.mean, w=snm.obj$weights[as.vector(bins[[i]])])
  }
  
# Build the basis spline matrix for the pooled coefficients.
  bSM.model <- buildBasisSplineMatrix(M.pooled, basisSplineFunction)
  exp <- new.env()
# Build the data object and fit the mixed effects model
  expObj <- makeDataObject(Y.pooled, np, snm.obj,
                           bSM.model, exp,bins)
  model.objects <- make.ref.model.matrices(snm.obj, exp)
  rff <- do.call("lmer", list(model.objects$ZF,expObj,NULL,TRUE,list(),NULL,FALSE,TRUE,TRUE,expObj$weights))
# Add useful variables to snm.obj
  snm.obj$E.pooled <- matrix(rff@resid, nr=dim(Y.pooled)[1])
  snm.obj$Y.pooled <- Y.pooled
  snm.obj$M.pooled <- M.pooled
  snm.obj$bin.densities <- sapply(bins,length)
  qs <- list(pi0=snm.obj$pi0, pvalues=snm.obj$pvalues)
#  Parse the lmer object!!! 
  snm.obj$array.fx <- calcArrayEffects(ranef(rff), basisSplineFunction, 
                               snm.obj,
                               model.objects,snm.obj$M)
  return(snm.obj)
  }

Sage-Bionetworks/snm documentation built on May 9, 2019, 12:14 p.m.