Skip to main content

generate normal variables with SAMPLING covariance matrix as given

covsam <- function(nobs,covm, seed=1237) {; library (expm);

nons=number of observations, covm = given covariance matrix ; nvar <- ncol(covm); tot <- nvar*nobs;

dat <- matrix(rnorm(tot), ncol=nvar); covmat <- cov(dat); a2 <- sqrtm(solve(covmat)); m2 <- sqrtm(covm); dat2 <- dat %% a2 %% m2 ; rc <- cov(dat2);};cm <-Generate normal variables with SAMPLING covariance matrix(c(1,0.5,0.1,0.5,1,0.5,0.1,0.5,1),ncol=3); cm;res <- covsam(10,cm) ;res; as given

generate normal variables with POPULATION covariance matrix as given

covsam <- function(nobs,covm, seed=1237) {; 
          library (expm);
          # nons=number of observations, covm = given covariance matrix ; 
          nvar <- ncol(covm); 
          tot <- nvar*nobs;
          dat <- matrix(rnorm(tot), ncol=nvar); 
          covmat <- cov(dat); 
          a2 <- sqrtm(solve(covmat)); 
          m2 <- sqrtm(covm);
          dat2 <- dat %*% a2 %*% m2 ; 
          rc <- cov(dat2);};
          cm <- matrix(c(1,0.5,0.1,0.5,1,0.5,0.1,0.5,1),ncol=3);
          cm; 
          res <- covsam(10,cm)  ;
          res;

covpop <- function(nobs,covm, seed=1237) {; library (expm); # nons=number of observations, covm = givenGenerate normal variables with POPULATION covariance matrix; nvar <- ncol(covm); tot <- nvarnobs; dat <- matrix(rnorm(tot), ncol=nvar); m2 <- sqrtm(covm); dat2 <- dat %% m2; rc <- cov(dat2); }; cm <- matrix(c(1,0.5,0.1,0.5,1,0.5,0.1,0.5,1),ncol=3); cm; res <- covpop(10,cm); res as given

covpop <- function(nobs,covm, seed=1237) {; 
          library (expm); 
          # nons=number of observations, covm = given covariance matrix;
          nvar <- ncol(covm); 
          tot <- nvar*nobs;  
          dat <- matrix(rnorm(tot), ncol=nvar); 
          m2 <- sqrtm(covm);
          dat2 <- dat %*% m2;  
          rc <- cov(dat2); }; 
          cm <- matrix(c(1,0.5,0.1,0.5,1,0.5,0.1,0.5,1),ncol=3);
          cm; 
          res <- covpop(10,cm); 
          res

generate normal variables with SAMPLING covariance matrix as given

covsam <- function(nobs,covm, seed=1237) {; library (expm);

nons=number of observations, covm = given covariance matrix ; nvar <- ncol(covm); tot <- nvar*nobs;

dat <- matrix(rnorm(tot), ncol=nvar); covmat <- cov(dat); a2 <- sqrtm(solve(covmat)); m2 <- sqrtm(covm); dat2 <- dat %% a2 %% m2 ; rc <- cov(dat2);};cm <- matrix(c(1,0.5,0.1,0.5,1,0.5,0.1,0.5,1),ncol=3); cm;res <- covsam(10,cm) ;res;

generate normal variables with POPULATION covariance matrix as given

covpop <- function(nobs,covm, seed=1237) {; library (expm); # nons=number of observations, covm = given covariance matrix; nvar <- ncol(covm); tot <- nvarnobs; dat <- matrix(rnorm(tot), ncol=nvar); m2 <- sqrtm(covm); dat2 <- dat %% m2; rc <- cov(dat2); }; cm <- matrix(c(1,0.5,0.1,0.5,1,0.5,0.1,0.5,1),ncol=3); cm; res <- covpop(10,cm); res

Generate normal variables with SAMPLING covariance matrix as given

covsam <- function(nobs,covm, seed=1237) {; 
          library (expm);
          # nons=number of observations, covm = given covariance matrix ; 
          nvar <- ncol(covm); 
          tot <- nvar*nobs;
          dat <- matrix(rnorm(tot), ncol=nvar); 
          covmat <- cov(dat); 
          a2 <- sqrtm(solve(covmat)); 
          m2 <- sqrtm(covm);
          dat2 <- dat %*% a2 %*% m2 ; 
          rc <- cov(dat2);};
          cm <- matrix(c(1,0.5,0.1,0.5,1,0.5,0.1,0.5,1),ncol=3);
          cm; 
          res <- covsam(10,cm)  ;
          res;

Generate normal variables with POPULATION covariance matrix as given

covpop <- function(nobs,covm, seed=1237) {; 
          library (expm); 
          # nons=number of observations, covm = given covariance matrix;
          nvar <- ncol(covm); 
          tot <- nvar*nobs;  
          dat <- matrix(rnorm(tot), ncol=nvar); 
          m2 <- sqrtm(covm);
          dat2 <- dat %*% m2;  
          rc <- cov(dat2); }; 
          cm <- matrix(c(1,0.5,0.1,0.5,1,0.5,0.1,0.5,1),ncol=3);
          cm; 
          res <- covpop(10,cm); 
          res
Source Link

generate normal variables with SAMPLING covariance matrix as given

covsam <- function(nobs,covm, seed=1237) {; library (expm);

nons=number of observations, covm = given covariance matrix ; nvar <- ncol(covm); tot <- nvar*nobs;

dat <- matrix(rnorm(tot), ncol=nvar); covmat <- cov(dat); a2 <- sqrtm(solve(covmat)); m2 <- sqrtm(covm); dat2 <- dat %% a2 %% m2 ; rc <- cov(dat2);};cm <- matrix(c(1,0.5,0.1,0.5,1,0.5,0.1,0.5,1),ncol=3); cm;res <- covsam(10,cm) ;res;

generate normal variables with POPULATION covariance matrix as given

covpop <- function(nobs,covm, seed=1237) {; library (expm); # nons=number of observations, covm = given covariance matrix; nvar <- ncol(covm); tot <- nvarnobs; dat <- matrix(rnorm(tot), ncol=nvar); m2 <- sqrtm(covm); dat2 <- dat %% m2; rc <- cov(dat2); }; cm <- matrix(c(1,0.5,0.1,0.5,1,0.5,0.1,0.5,1),ncol=3); cm; res <- covpop(10,cm); res