R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # gnbxml.r November 2007 > # > # generate normal random variable via Box-Muller method > # > set.seed(5151917) > # > N <- 1000 # how many to do > # > U <- runif(N) > V <- runif(N) > X <- cos(2*pi*U)*sqrt(-2*log(V)) > Y <- sin(2*pi*U)*sqrt(-2*log(V)) # note reverse roles > # some simple analysis > XY <- t(rbind(X,Y)) # get right shape > # one way > mean(XY) [1] 0.04549968 > # second way > apply(XY,2,mean) X Y 0.05509137 0.03590799 > # one way > cov(XY) X Y X 0.90163753 -0.02383551 Y -0.02383551 0.90987914 > # second way > apply(XY,2,var) X Y 0.9016375 0.9098791 > # third way > C <- var(XY) > C X Y X 0.90163753 -0.02383551 Y -0.02383551 0.90987914 > # test whether X and Y are correlated > r <- C[1,2]/sqrt(C[1,1]*C[2,2]) > t <- r*sqrt(N/(1-r*r)) > c(r,t) [1] -0.0263158 -0.8324670 > # > # analyze X's then Y's using Kolmogorov-Smirnov & Anderson-Darling > # > sX <- sort(X) > w <- (2*(1:N)-1)/N > # first K-S > f <- pnorm(sX) > Dnm <- max(f-((1:N)-1)/N) # Dn- > Dnp <- max((1:N)/N-f) # Dn+ > c(Dnp,Dnm) # print them [1] 0.01144788 0.03855941 > sqrt(N)*max(Dnp,Dnm) # standardized statistic [1] 1.219356 > # now A-D > w <- (2*(1:N)-1)/N > lf <- pnorm(sX,log.p=TRUE) > # reverse the order > lomf <- pnorm(rev(sX),lower.tail=F,log.p=TRUE) > A2X <- -N - sum(w*(lf+lomf)) > A2X [1] 2.284703 > # repeat for Y > sY <- sort(Y) > # first K-S > f <- pnorm(sY) > Dnm <- max(f-((1:N)-1)/N) # Dn- > Dnp <- max((1:N)/N-f) # Dn+ > c(Dnp,Dnm) # print them [1] 0.008809552 0.034118552 > sqrt(N)*max(Dnp,Dnm) # standardized statistic [1] 1.078923 > # now A-D > lf <- pnorm(sY,log.p=TRUE) > # reverse the order > lomf <- pnorm(rev(sY),lower.tail=F,log.p=TRUE) > A2Y <- -N - sum(w*(lf+lomf)) > A2Y [1] 1.371618 > # done > rm(list=ls()) > q()