rMVNorm              package:bayesSurv              R Documentation

_S_a_m_p_l_e _f_r_o_m _t_h_e _m_u_l_t_i_v_a_r_i_a_t_e _n_o_r_m_a_l _d_i_s_t_r_i_b_u_t_i_o_n

_D_e_s_c_r_i_p_t_i_o_n:

     According to the parametrization used, sample from the
     multivariate normal distribution.

     The following parametrization can be specified

     _s_t_a_n_d_a_r_d In this case we sample from either N(mu, Sigma) or from
          N(mu, Q^(-1)).      

     _c_a_n_o_n_i_c_a_l In this case we sample from N(Q^(-1)*b, Q^(-1)).      

     Generation of random numbers is performed by Algorithms 2.3-2.5 in
     Rue and Held (2005, pp. 34-35).

_U_s_a_g_e:

       rMVNorm(n, mean=0, Sigma=1, Q, param=c("standard", "canonical"))

_A_r_g_u_m_e_n_t_s:

       n: number of observations to be sampled.

    mean: For 'param="standard"', it is  a vector mu of means. If
          'length(mean)' is equal to 1, it is recycled and all
          components have the same mean.

          For 'param="canonical"', it is  a vector b of canonical
          means. If 'length(mean)' is equal to 1, it is recycled and
          all components have the same mean.     

   Sigma: covariance matrix of the multivariate normal distribution. It
          is ignored if 'Q' is given at the same time. 

       Q: precision matrix of the multivariate normal distribution.

          It does not have to be supplied provided 'Sigma' is given and
          'param="standard"'.

          It must be supplied if 'param="canonical"'. 

   param: a character which specifies the parametrization. 

_V_a_l_u_e:

     Matrix with sampled points in rows.

_A_u_t_h_o_r(_s):

     Arno&#353t Kom&#225rek komarek@karlin.mff.cuni.cz

_R_e_f_e_r_e_n_c_e_s:

     Rue, H. and Held, L. (2005). _Gaussian Markov Random Fields:
     Theory and Applications_. Boca Raton: Chapman and Hall/CRC.

_S_e_e _A_l_s_o:

     'rnorm', 'Mvnorm'.

_E_x_a_m_p_l_e_s:

     ### Mean, covariance matrix, its inverse
     ### and the canonical mean
     mu <- c(0, 2, 0.5)
     L <- matrix(c(1, 1, 1,  0, 0.5, 0.5,  0, 0, 0.3), ncol=3)
     Sigma <- L %*% t(L)
     Q <- chol2inv(t(L))
     b <- Q %*% mu

     print(Sigma)
     print(Q)
     print(Sigma %*% Q)

     ### Sample using different parametrizations
     set.seed(775988621)
     n <- 10000

     ### Sample from N(mu, Sigma)
     xx1 <- rMVNorm(n=n, mean=mu, Sigma=Sigma)
     apply(xx1, 2, mean)
     var(xx1)

     ### Sample from N(mu, Q^{-1})
     xx2 <- rMVNorm(n=n, mean=mu, Q=Q)
     apply(xx2, 2, mean)
     var(xx2)

     ### Sample from N(Q^{-1}*b, Q^{-1})
     xx3 <- rMVNorm(n=n, mean=b, Q=Q, param="canonical")
     apply(xx3, 2, mean)
     var(xx3)

