楼主: Eviewschen
2688 10

[休闲其它] 【独家发布】Statistical Computing with R [推广有奖]

  • 0关注
  • 2粉丝

已卖:362份资源

硕士生

46%

还不是VIP/贵宾

-

TA的文库  其他...

Nvivo(定性数据分析)

Observational Study

Mathematica NewOccidental

威望
0
论坛币
3804 个
通用积分
1.5037
学术水平
14 点
热心指数
9 点
信用等级
11 点
经验
1458 点
帖子
102
精华
1
在线时间
19 小时
注册时间
2006-5-13
最后登录
2016-12-10

楼主
Eviewschen 发表于 2014-11-10 10:26:53 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

http://personal.bgsu.edu/~mrizzo/SCR.htm
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Statistical statistica computing statistic Statist

本帖被以下文库推荐

沙发
Eviewschen 发表于 2014-11-10 10:27:46
  1.     #######################################################
  2.     ###       Statistical Computing with R              ###         
  3.     ###       Maria L. Rizzo                            ###
  4.     ###       Chapman & Hall / CRC                      ###
  5.     ###       ISBN 9781584885450                        ###
  6.     ###                                                 ###
  7.     ###       R code for Chapter 3 Examples             ###
  8.     #######################################################

  9. ### Example 3.2 (Inverse transform method, continuous case)

  10.     n <- 1000
  11.     u <- runif(n)
  12.     x <- u^(1/3)
  13.     hist(x, prob = TRUE, main = bquote(f(x)==3*x^2)) #density histogram of sample
  14.     y <- seq(0, 1, .01)
  15.     lines(y, 3*y^2)    #density curve f(x)


  16. ### Example 3.4 (Two point distribution)

  17.     n <- 1000
  18.     p <- 0.4
  19.     u <- runif(n)
  20.     x <- as.integer(u > 0.6)   #(u > 0.6) is a logical vector

  21.     mean(x)
  22.     var(x)


  23. ### Example 3.5 (Geometric distribution)

  24.     n <- 1000
  25.     p <- 0.25
  26.     u <- runif(n)
  27.     k <- ceiling(log(1-u) / log(1-p)) - 1
  28.    
  29.     # more efficient
  30.     k <- floor(log(u) / log(1-p))


  31. ### Example 3.6 (Logarithmic distribution)

  32.     rlogarithmic <- function(n, theta) {
  33.         #returns a random logarithmic(theta) sample size n
  34.         u <- runif(n)
  35.         #set the initial length of cdf vector
  36.         N <- ceiling(-16 / log10(theta))
  37.         k <- 1:N
  38.         a <- -1/log(1-theta)
  39.         fk <- exp(log(a) + k * log(theta) - log(k))
  40.         Fk <- cumsum(fk)
  41.         x <- integer(n)
  42.         for (i in 1:n) {
  43.             x[i] <- as.integer(sum(u[i] > Fk)) #F^{-1}(u)-1
  44.             while (x[i] == N) {
  45.                 #if x==N we need to extend the cdf
  46.                 #very unlikely because N is large
  47.                 logf <- log(a) + (N+1)*log(theta) - log(N+1)
  48.                 fk <- c(fk, exp(logf))
  49.                 Fk <- c(Fk, Fk[N] + fk[N+1])
  50.                 N <- N + 1
  51.                 x[i] <- as.integer(sum(u[i] > Fk))
  52.             }
  53.         }
  54.         x + 1
  55.     }

  56.     n <- 1000
  57.     theta <- 0.5
  58.     x <- rlogarithmic(n, theta)
  59.     #compute density of logarithmic(theta) for comparison
  60.     k <- sort(unique(x))
  61.     p <- -1 / log(1 - theta) * theta^k / k
  62.     se <- sqrt(p*(1-p)/n)   #standard error

  63.     round(rbind(table(x)/n, p, se),3)


  64. ### Example 3.7 (Acceptance-rejection method)

  65.     n <- 1000
  66.     k <- 0      #counter for accepted
  67.     j <- 0      #iterations
  68.     y <- numeric(n)

  69.     while (k < n) {
  70.         u <- runif(1)
  71.         j <- j + 1
  72.         x <- runif(1)  #random variate from g
  73.         if (x * (1-x) > u) {
  74.             #we accept x
  75.             k <- k + 1
  76.             y[k] <- x
  77.         }
  78.     }

  79.     j

  80.     #compare empirical and theoretical percentiles
  81.     p <- seq(.1, .9, .1)
  82.     Qhat <- quantile(y, p)   #quantiles of sample
  83.     Q <- qbeta(p, 2, 2)      #theoretical quantiles
  84.     se <- sqrt(p * (1-p) / (n * dbeta(Q, 2, 2)^2)) #see Ch. 2
  85.     round(rbind(Qhat, Q, se), 3)


  86. ### Example 3.8 (Beta distribution)

  87.     n <- 1000
  88.     a <- 3
  89.     b <- 2
  90.     u <- rgamma(n, shape=a, rate=1)
  91.     v <- rgamma(n, shape=b, rate=1)
  92.     x <- u / (u + v)

  93.     q <- qbeta(ppoints(n), a, b)
  94.     qqplot(q, x, cex=0.25, xlab="Beta(3, 2)", ylab="Sample")
  95.     abline(0, 1)


  96. ### Example 3.9 (Logarithmic distribution, version 2)

  97.     n <- 1000
  98.     theta <- 0.5
  99.     u <- runif(n)  #generate logarithmic sample
  100.     v <- runif(n)
  101.     x <- floor(1 + log(v) / log(1 - (1 - theta)^u))
  102.     k <- 1:max(x)  #calc. logarithmic probs.
  103.     p <- -1 / log(1 - theta) * theta^k / k
  104.     se <- sqrt(p*(1-p)/n)
  105.     p.hat <- tabulate(x)/n

  106.     print(round(rbind(p.hat, p, se), 3))

  107.     # The following function is a simple replacement for
  108.     # rlogarithmic in Example 3.6

  109.     rlogarithmic <- function(n, theta) {
  110.         stopifnot(all(theta > 0 & theta < 1))
  111.         th <- rep(theta, length=n)
  112.         u <- runif(n)
  113.         v <- runif(n)
  114.         x <- floor(1 + log(v) / log(1 - (1 - th)^u))
  115.         return(x)
  116.     }


  117. ### Example 3.10 (Chisquare)

  118.     n <- 1000
  119.     nu <- 2
  120.     X <- matrix(rnorm(n*nu), n, nu)^2 #matrix of sq. normals
  121.     #sum the squared normals across each row: method 1
  122.     y <- rowSums(X)
  123.     #method 2
  124.     y <- apply(X, MARGIN=1, FUN=sum)  #a vector length n
  125.     mean(y)
  126.     mean(y^2)


  127. ### Example 3.11 (Convolutions and mixtures)

  128.     n <- 1000
  129.     x1 <- rgamma(n, 2, 2)
  130.     x2 <- rgamma(n, 2, 4)
  131.     s <- x1 + x2              #the convolution
  132.     u <- runif(n)
  133.     k <- as.integer(u > 0.5)  #vector of 0's and 1's
  134.     x <- k * x1 + (1-k) * x2  #the mixture

  135.     par(mfcol=c(1,2))         #two graphs per page
  136.     hist(s, prob=TRUE)
  137.     hist(x, prob=TRUE)
  138.     par(mfcol=c(1,1))         #restore display


  139. ### Example 3.12 (Mixture of several gamma distributions)
  140.     # density estimates are plotted

  141.     n <- 5000
  142.     k <- sample(1:5, size=n, replace=TRUE, prob=(1:5)/15)
  143.     rate <- 1/k
  144.     x <- rgamma(n, shape=3, rate=rate)

  145.     #plot the density of the mixture
  146.     #with the densities of the components
  147.     plot(density(x), xlim=c(0,40), ylim=c(0,.3),
  148.         lwd=3, xlab="x", main="")
  149.     for (i in 1:5)
  150.         lines(density(rgamma(n, 3, 1/i)))


  151. ### Example 3.13 (Mixture of several gamma distributions)

  152.     n <- 5000
  153.     p <- c(.1,.2,.2,.3,.2)
  154.     lambda <- c(1,1.5,2,2.5,3)
  155.     k <- sample(1:5, size=n, replace=TRUE, prob=p)
  156.     rate <- lambda[k]
  157.     x <- rgamma(n, shape=3, rate=rate)


  158. ### Example 3.14 (Plot density of mixture)

  159.     f <- function(x, lambda, theta) {
  160.         #density of the mixture at the point x
  161.         sum(dgamma(x, 3, lambda) * theta)
  162.     }

  163.     p <- c(.1,.2,.2,.3,.2)
  164.     lambda <- c(1,1.5,2,2.5,3)

  165.     x <- seq(0, 8, length=200)
  166.     dim(x) <- length(x)  #need for apply

  167.     #compute density of the mixture f(x) along x
  168.     y <- apply(x, 1, f, lambda=lambda, theta=p)

  169.     #plot the density of the mixture
  170.     plot(x, y, type="l", ylim=c(0,.85), lwd=3, ylab="Density")

  171.     for (j in 1:5) {
  172.         #add the j-th gamma density to the plot
  173.         y <- apply(x, 1, dgamma, shape=3, rate=lambda[j])
  174.         lines(x, y)
  175.     }


  176. ### Example 3.15 (Poisson-Gamma mixture)

  177.     #generate a Poisson-Gamma mixture
  178.     n <- 1000
  179.     r <- 4
  180.     beta <- 3
  181.     lambda <- rgamma(n, r, beta) #lambda is random

  182.     #now supply the sample of lambda's as the Poisson mean
  183.     x <- rpois(n, lambda)        #the mixture

  184.     #compare with negative binomial
  185.     mix <- tabulate(x+1) / n
  186.     negbin <- round(dnbinom(0:max(x), r, beta/(1+beta)), 3)
  187.     se <- sqrt(negbin * (1 - negbin) / n)

  188.     round(rbind(mix, negbin, se), 3)
  189.    
  190.    
  191. ### Example 3.16 (Spectral decomposition method)

  192.     # mean and covariance parameters
  193.     mu <- c(0, 0)
  194.     Sigma <- matrix(c(1, .9, .9, 1), nrow = 2, ncol = 2)

  195.     rmvn.eigen <-
  196.     function(n, mu, Sigma) {
  197.         # generate n random vectors from MVN(mu, Sigma)
  198.         # dimension is inferred from mu and Sigma
  199.         d <- length(mu)
  200.         ev <- eigen(Sigma, symmetric = TRUE)
  201.         lambda <- ev$values
  202.         V <- ev$vectors
  203.         R <- V %*% diag(sqrt(lambda)) %*% t(V)
  204.         Z <- matrix(rnorm(n*d), nrow = n, ncol = d)
  205.         X <- Z %*% R + matrix(mu, n, d, byrow = TRUE)
  206.         X
  207.     }

  208.     # generate the sample
  209.     X <- rmvn.eigen(1000, mu, Sigma)

  210.     plot(X, xlab = "x", ylab = "y", pch = 20)
  211.     print(colMeans(X))
  212.     print(cor(X))


  213. ### Example 3.17 (SVD method)

  214.     rmvn.svd <-
  215.     function(n, mu, Sigma) {
  216.         # generate n random vectors from MVN(mu, Sigma)
  217.         # dimension is inferred from mu and Sigma
  218.         d <- length(mu)
  219.         S <- svd(Sigma)
  220.         R <- S$u %*% diag(sqrt(S$d)) %*% t(S$v) #sq. root Sigma
  221.         Z <- matrix(rnorm(n*d), nrow=n, ncol=d)
  222.         X <- Z %*% R + matrix(mu, n, d, byrow=TRUE)
  223.         X
  224.     }
  225.    

  226. ### Example 3.18 (Choleski factorization method)

  227.     rmvn.Choleski <-
  228.     function(n, mu, Sigma) {
  229.         # generate n random vectors from MVN(mu, Sigma)
  230.         # dimension is inferred from mu and Sigma
  231.         d <- length(mu)
  232.         Q <- chol(Sigma) # Choleski factorization of Sigma
  233.         Z <- matrix(rnorm(n*d), nrow=n, ncol=d)
  234.         X <- Z %*% Q + matrix(mu, n, d, byrow=TRUE)
  235.         X
  236.     }
  237.    
  238.     #generating the samples according to the mean and covariance
  239.     #structure as the four-dimensional iris virginica data
  240.     y <- subset(x=iris, Species=="virginica")[, 1:4]
  241.     mu <- colMeans(y)
  242.     Sigma <- cov(y)
  243.     mu
  244.     Sigma

  245.     #now generate MVN data with this mean and covariance
  246.     X <- rmvn.Choleski(200, mu, Sigma)
  247.     pairs(X)

  248. ### Example 3.19 (Comparing performance of MVN generators)

  249.     library(MASS)
  250.     library(mvtnorm)
  251.     n <- 100          #sample size
  252.     d <- 30           #dimension
  253.     N <- 2000         #iterations
  254.     mu <- numeric(d)

  255.     set.seed(100)
  256.     system.time(for (i in 1:N)
  257.         rmvn.eigen(n, mu, cov(matrix(rnorm(n*d), n, d))))
  258.     set.seed(100)
  259.     system.time(for (i in 1:N)
  260.         rmvn.svd(n, mu, cov(matrix(rnorm(n*d), n, d))))
  261.     set.seed(100)
  262.     system.time(for (i in 1:N)
  263.         rmvn.Choleski(n, mu, cov(matrix(rnorm(n*d), n, d))))
  264.     set.seed(100)
  265.     system.time(for (i in 1:N)
  266.         mvrnorm(n, mu, cov(matrix(rnorm(n*d), n, d))))
  267.     set.seed(100)
  268.     system.time(for (i in 1:N)
  269.         rmvnorm(n, mu, cov(matrix(rnorm(n*d), n, d))))
  270.     set.seed(100)
  271.     system.time(for (i in 1:N)
  272.         cov(matrix(rnorm(n*d), n, d)))

  273.     detach(package:MASS)
  274.     detach(package:mvtnorm)

  275. ### Example 3.20 (Multivariate normal mixture)

  276.     library(MASS)  #for mvrnorm
  277.     #ineffecient version loc.mix.0 with loops

  278.     loc.mix.0 <- function(n, p, mu1, mu2, Sigma) {
  279.         #generate sample from BVN location mixture
  280.         X <- matrix(0, n, 2)

  281.         for (i in 1:n) {
  282.             k <- rbinom(1, size = 1, prob = p)
  283.             if (k)
  284.                 X[i,] <- mvrnorm(1, mu = mu1, Sigma) else
  285.                 X[i,] <- mvrnorm(1, mu = mu2, Sigma)
  286.             }
  287.         return(X)
  288.     }

  289.     #more efficient version
  290.     loc.mix <- function(n, p, mu1, mu2, Sigma) {
  291.         #generate sample from BVN location mixture
  292.         n1 <- rbinom(1, size = n, prob = p)
  293.         n2 <- n - n1
  294.         x1 <- mvrnorm(n1, mu = mu1, Sigma)
  295.         x2 <- mvrnorm(n2, mu = mu2, Sigma)
  296.         X <- rbind(x1, x2)            #combine the samples
  297.         return(X[sample(1:n), ])      #mix them
  298.     }

  299.     x <- loc.mix(1000, .5, rep(0, 4), 2:5, Sigma = diag(4))
  300.     r <- range(x) * 1.2
  301.     par(mfrow = c(2, 2))
  302.     for (i in 1:4)
  303.         hist(x[ , i], xlim = r, ylim = c(0, .3), freq = FALSE,
  304.         main = "", breaks = seq(-5, 10, .5))        

  305.     detach(package:MASS)
  306.     par(mfrow = c(1, 1))
  307.    

  308. ### Example 3.21 (Generating variates on a sphere)

  309.     runif.sphere <- function(n, d) {
  310.         # return a random sample uniformly distributed
  311.         # on the unit sphere in R ^d
  312.         M <- matrix(rnorm(n*d), nrow = n, ncol = d)
  313.         L <- apply(M, MARGIN = 1,
  314.                    FUN = function(x){sqrt(sum(x*x))})
  315.         D <- diag(1 / L)
  316.         U <- D %*% M
  317.         U
  318.     }

  319.     #generate a sample in d=2 and plot
  320.     X <- runif.sphere(200, 2)
  321.     par(pty = "s")
  322.     plot(X, xlab = bquote(x[1]), ylab = bquote(x[2]))
  323.     par(pty = "m")


  324. ### Example 3.22 (Poisson process)

  325.     lambda <- 2
  326.     t0 <- 3
  327.     Tn <- rexp(100, lambda)       #interarrival times
  328.     Sn <- cumsum(Tn)              #arrival times
  329.     n <- min(which(Sn > t0))      #arrivals+1 in [0, t0]


  330. ### Example 3.23 (Poisson process, cont.)

  331.     lambda <- 2
  332.     t0 <- 3
  333.     upper <- 100
  334.     pp <- numeric(10000)
  335.     for (i in 1:10000) {
  336.         N <- rpois(1, lambda * upper)
  337.         Un <- runif(N, 0, upper)      #unordered arrival times
  338.         Sn <- sort(Un)                #arrival times
  339.         n <- min(which(Sn > t0))      #arrivals+1 in [0, t0]
  340.         pp[i] <- n - 1                #arrivals in [0, t0]
  341.         }

  342.     #alternately, the loop can be replaced by replicate function
  343.     pp <- replicate(10000, expr = {
  344.         N <- rpois(1, lambda * upper)
  345.         Un <- runif(N, 0, upper)      #unordered arrival times
  346.         Sn <- sort(Un)                #arrival times
  347.         n <- min(which(Sn > t0))      #arrivals+1 in [0, t0]
  348.         n - 1  })                     #arrivals in [0, t0]

  349.     c(mean(pp), var(pp))


  350. ### Example 3.24 (Nonhomogeneous Poisson process)

  351.     lambda <- 3
  352.     upper <- 100
  353.     N <- rpois(1, lambda * upper)
  354.     Tn <- rexp(N, lambda)
  355.     Sn <- cumsum(Tn)
  356.     Un <- runif(N)
  357.     keep <- (Un <= cos(Sn)^2)    #indicator, as logical vector
  358.     Sn[keep]

  359.     round(Sn[keep], 4)


  360. ### Example 3.25 (Renewal process)

  361.     t0 <- 5
  362.     Tn <- rgeom(100, prob = .2)   #interarrival times
  363.     Sn <- cumsum(Tn)              #arrival times
  364.     n <- min(which(Sn > t0))      #arrivals+1 in [0, t0]

  365.     Nt0 <- replicate(1000, expr = {
  366.         Sn <- cumsum(rgeom(100, prob = .2))
  367.         min(which(Sn > t0)) - 1
  368.         })
  369.     table(Nt0)/1000
  370.     Nt0

  371.     t0 <- seq(0.1, 30, .1)
  372.     mt <- numeric(length(t0))

  373.     for (i in 1:length(t0)) {
  374.         mt[i] <- mean(replicate(1000,
  375.         {
  376.         Sn <- cumsum(rgeom(100, prob = .2))
  377.         min(which(Sn > t0[i])) - 1
  378.         }))
  379.     }
  380.     plot(t0, mt, type = "l", xlab = "t", ylab = "mean")
  381.     abline(0, .25)
  382.    

  383. ### Example 3.26 (Symmetric random walk)

  384.     n <- 400
  385.     incr <- sample(c(-1, 1), size = n, replace = TRUE)
  386.     S <- as.integer(c(0, cumsum(incr)))
  387.     plot(0:n, S, type = "l", main = "", xlab = "i")

  388. ### Example 3.27 (Generator for the time until return to origin)
  389.    
  390.     set.seed(12345)
  391.    
  392.     #compute the probabilities directly
  393.     n <- 1:10000
  394.     p2n <- exp(lgamma(2*n-1)
  395.               - log(n) - (2*n-1)*log(2) - 2*lgamma(n))
  396.     #or compute using dbinom
  397.     P2n <- (.5/n) * dbinom(n-1, size = 2*n-2, prob = 0.5)
  398.     pP2n <- cumsum(P2n)

  399.     #given n compute the time of the last return to 0 in (0,n]
  400.     n <- 200
  401.     sumT <- 0
  402.     while (sumT <= n) {
  403.         u <- runif(1)
  404.         s <- sum(u > pP2n)
  405.         if (s == length(pP2n))
  406.             warning("T is truncated")
  407.         Tj <- 2 * (1 + s)
  408.         #print(c(Tj, sumT))
  409.         sumT <- sumT + Tj
  410.         }
  411.     sumT - Tj
复制代码


藤椅
Eviewschen 发表于 2014-11-10 10:28:59
  1.     #######################################################
  2.     ###       Statistical Computing with R              ###
  3.     ###       Maria L. Rizzo                            ###
  4.     ###       Chapman & Hall / CRC                      ###
  5.     ###       ISBN 9781584885450                        ###
  6.     ###                                                 ###
  7.     ###       R code for Chapter 4 Examples             ###
  8.     #######################################################


  9. ### Example 4.1 (Scatterplot matrix)

  10.     data(iris)
  11.     #virginica data in first 4 columns of the last 50 obs.
  12.    
  13.     # not shown in text
  14.     pairs(iris[101:150, 1:4])

  15.     panel.d <- function(x, ...) {
  16.         usr <- par("usr")
  17.         on.exit(par(usr))
  18.         par(usr = c(usr[1:2], 0, .5))
  19.         lines(density(x))
  20.      }
  21.    
  22.     # Fig. 4.1
  23.     x <- scale(iris[101:150, 1:4])
  24.     r <- range(x)
  25.     pairs(x, diag.panel = panel.d, xlim = r, ylim = r)

  26.     library(lattice)
  27.     splom(iris[101:150, 1:4])    #plot 1

  28.     #for all 3 at once, in color, plot 2
  29.     splom(iris[,1:4], groups = iris$Species)

  30.     # Fig. 4.2
  31.     #for all 3 at once, black and white, plot 3
  32.     splom(~iris[1:4], groups = Species, data = iris,
  33.         col = 1, pch = c(1, 2, 3),  cex = c(.5,.5,.5))


  34. ### Example 4.2 (Plot bivariate normal density)

  35.     #the standard BVN density
  36.     f <- function(x,y) {
  37.         z <- (1/(2*pi)) * exp(-.5 * (x^2 + y^2))
  38.         }

  39.     y <- x <- seq(-3, 3, length= 50)
  40.     z <- outer(x, y, f)   #compute density for all (x,y)

  41.     persp(x, y, z)        #the default plot

  42.     persp(x, y, z, theta = 45, phi = 30, expand = 0.6,
  43.           ltheta = 120, shade = 0.75, ticktype = "detailed",
  44.           xlab = "X", ylab = "Y", zlab = "f(x, y)")


  45. ### Example 4.3 (Add elements to perspective plot)

  46.      #store viewing transformation in M
  47.      persp(x, y, z, theta = 45, phi = 30,
  48.            expand = .4, box = FALSE) -> M

  49.      #add some points along a circle
  50.      a <- seq(-pi, pi, pi/16)
  51.      newpts <- cbind(cos(a), sin(a)) * 2
  52.      newpts <- cbind(newpts, 0, 1)  #z=0, t=1
  53.      N <- newpts %*% M
  54.      points(N[,1]/N[,4], N[,2]/N[,4], col=2)

  55.      #add lines
  56.      x2 <- seq(-3, 3, .1)
  57.      y2 <- -x2^2 / 3
  58.      z2 <- dnorm(x2) * dnorm(y2)
  59.      N <- cbind(x2, y2, z2, 1) %*% M
  60.      lines(N[,1]/N[,4], N[,2]/N[,4], col=4)

  61.      #add text
  62.      x3 <- c(0, 3.1)
  63.      y3 <- c(0, -3.1)
  64.      z3 <- dnorm(x3) * dnorm(y3) * 1.1
  65.      N <- cbind(x3, y3, z3, 1) %*% M
  66.      text(N[1,1]/N[1,4], N[1,2]/N[1,4], "f(x,y)")
  67.      text(N[2,1]/N[2,4], N[2,2]/N[2,4], bquote(y==-x^2/3))


  68. ### Example 4.4 (Surface plot using wireframe(lattice))

  69.     library(lattice)
  70.     x <- y <- seq(-3, 3, length= 50)

  71.     xy <- expand.grid(x, y)
  72.     z <- (1/(2*pi)) * exp(-.5 * (xy[,1]^2 + xy[,2]^2))
  73.     wireframe(z ~ xy[,1] * xy[,2])


  74. ### Example 4.5 (3D scatterplot)

  75.     library(lattice)
  76.     attach(iris)
  77.     #basic 3 color plot with arrows along axes
  78.     print(cloud(Petal.Length ~ Sepal.Length * Sepal.Width,
  79.           data=iris, groups=Species))

  80.     print(cloud(Sepal.Length ~ Petal.Length * Petal.Width,
  81.         data = iris, groups = Species, main = "1", pch=1:3,
  82.         scales = list(draw = FALSE), zlab = "SL",
  83.         screen = list(z = 30, x = -75, y = 0)),
  84.         split = c(1, 1, 2, 2), more = TRUE)

  85.     print(cloud(Sepal.Width ~ Petal.Length * Petal.Width,
  86.         data = iris, groups = Species, main = "2", pch=1:3,
  87.         scales = list(draw = FALSE), zlab = "SW",
  88.         screen = list(z = 30, x = -75, y = 0)),
  89.         split = c(2, 1, 2, 2), more = TRUE)

  90.     print(cloud(Petal.Length ~ Sepal.Length * Sepal.Width,
  91.         data = iris, groups = Species, main = "3", pch=1:3,
  92.         scales = list(draw = FALSE), zlab = "PL",
  93.         screen = list(z = 30, x = -55, y = 0)),
  94.         split = c(1, 2, 2, 2), more = TRUE)

  95.     print(cloud(Petal.Width ~ Sepal.Length * Sepal.Width,
  96.         data = iris, groups = Species, main = "4", pch=1:3,
  97.         scales = list(draw = FALSE), zlab = "PW",
  98.         screen = list(z = 30, x = -55, y = 0)),
  99.         split = c(2, 2, 2, 2))
  100.     detach(iris)


  101. ### Example 4.6 (Contour plot)


  102.      #contour plot with labels
  103.      contour(volcano, asp = 1, labcex = 1)

  104.      #another version from lattice package
  105.      library(lattice)
  106.      contourplot(volcano) #similar to above


  107. ### Example 4.7 (Filled contour plots)

  108.      image(volcano, col = terrain.colors(100), axes = FALSE)
  109.      contour(volcano, levels = seq(100,200,by = 10), add = TRUE)

  110.      filled.contour(volcano, color = terrain.colors, asp = 1)
  111.      levelplot(volcano, scales = list(draw = FALSE),
  112.                xlab = "", ylab = "")
  113.                

  114. ### Example 4.8 (2D histogram)

  115.     library(hexbin)
  116.     x <- matrix(rnorm(4000), 2000, 2)
  117.     plot(hexbin(x[,1], x[,2]))


  118. ### Example 4.9 (Andrews curves)

  119.     library(DAAG)
  120.     attach(leafshape17)

  121.     f <- function(a, v) {
  122.         #Andrews curve f(a) for a data vector v in R^3
  123.         v[1]/sqrt(2) + v[2]*sin(a) + v[3]*cos(a)
  124.     }


  125.     #scale data to range [-1, 1]
  126.     x <- cbind(bladelen, petiole, bladewid)
  127.     n <- nrow(x)
  128.     mins <- apply(x, 2, min)  #column minimums
  129.     maxs <- apply(x, 2, max)  #column maximums
  130.     r <- maxs - mins          #column ranges
  131.     y <- sweep(x, 2, mins)    #subtract column mins
  132.     y <- sweep(y, 2, r, "/")  #divide by range
  133.     x <- 2 * y - 1            #now has range [-1, 1]

  134.     #set up plot window, but plot nothing yet
  135.     plot(0, 0, xlim = c(-pi, pi), ylim = c(-3,3),
  136.         xlab = "t", ylab = "Andrews Curves",
  137.         main = "", type = "n")

  138.     #now add the Andrews curves for each observation
  139.     #line type corresponds to leaf architecture
  140.     #0=orthotropic, 1=plagiotropic
  141.     a <- seq(-pi, pi, len=101)
  142.     dim(a) <- length(a)
  143.     for (i in 1:n) {
  144.         g <- arch[i] + 1
  145.         y <- apply(a, MARGIN = 1, FUN = f, v = x[i,])
  146.         lines(a, y, lty = g)
  147.     }
  148.     legend(3, c("Orthotropic", "Plagiotropic"), lty = 1:2)
  149.     detach(leafshape17)
  150.    

  151. ### Example 4.10 (Parallel coordinates)

  152.     library(MASS)
  153.     library(lattice)
  154.     trellis.device(color = FALSE) #black and white display
  155.     x <- crabs[seq(5, 200, 5), ]  #get every fifth obs.
  156.     parallel(~x[4:8] | sp*sex, x)

  157.     trellis.device(color = FALSE)    #black and white display
  158.     x <- crabs[seq(5, 200, 5), ]     #get every fifth obs.
  159.     a <- x$CW * x$CL                 #area of carapace
  160.     x[4:8] <- x[4:8] / sqrt(a)       #adjust for size
  161.     parallel(~x[4:8] | sp*sex, x)


  162. ### Example 4.11 (Segment plot)

  163.     #segment plot
  164.     library(MASS)  #for crabs data
  165.     attach(crabs)
  166.     x <- crabs[seq(5, 200, 5), ]         #get every fifth obs.
  167.     x <- subset(x, sex == "M")           #keep just the males
  168.     a <- x$CW * x$CL                     #area of carapace
  169.     x[4:8] <- x[4:8] / sqrt(a)           #adjust for size

  170.     #use default color palette or other colors
  171.     palette(gray(seq(.4, .95, len = 5))) #use gray scale
  172.     #palette(rainbow(6))                 #or use color
  173.     stars(x[4:8], draw.segments = TRUE,
  174.           labels = x$sp, nrow = 4,
  175.           ylim = c(-2,10), key.loc = c(3,-1))

  176.     #after viewing, restore the default colors
  177.     palette("default")
  178.     detach(crabs)

  179.    
复制代码


板凳
Eviewschen 发表于 2014-11-10 10:32:51
  1.     #######################################################
  2.     ###       Statistical Computing with R              ###
  3.     ###       Maria L. Rizzo                            ###
  4.     ###       Chapman & Hall / CRC                      ###
  5.     ###       ISBN 9781584885450                        ###
  6.     ###                                                 ###
  7.     ###       R code for Chapter 5 Examples             ###
  8.     #######################################################


  9. ### Example 5.1 (Simple Monte Carlo integration)

  10.     m <- 10000
  11.     x <- runif(m)
  12.     theta.hat <- mean(exp(-x))
  13.     print(theta.hat)
  14.     print(1 - exp(-1))


  15. ### Example 5.2 (Simple Monte Carlo integration, cont.)

  16.     m <- 10000
  17.     x <- runif(m, min=2, max=4)
  18.     theta.hat <- mean(exp(-x)) * 2
  19.     print(theta.hat)
  20.     print(exp(-2) - exp(-4))


  21. ### Example 5.3 (Monte Carlo integration, unbounded interval)

  22.     x <- seq(.1, 2.5, length = 10)
  23.     m <- 10000
  24.     u <- runif(m)
  25.     cdf <- numeric(length(x))
  26.     for (i in 1:length(x)) {
  27.         g <- x[i] * exp(-(u * x[i])^2 / 2)
  28.         cdf[i] <- mean(g) / sqrt(2 * pi) + 0.5
  29.     }

  30.     Phi <- pnorm(x)
  31.     print(round(rbind(x, cdf, Phi), 3))


  32. ### Example 5.4 (Example 5.3, cont.)

  33.     x <- seq(.1, 2.5, length = 10)
  34.     m <- 10000
  35.     z <- rnorm(m)
  36.     dim(x) <- length(x)
  37.     p <- apply(x, MARGIN = 1,
  38.              FUN = function(x, z) {mean(z < x)}, z = z)

  39.     Phi <- pnorm(x)
  40.     print(round(rbind(x, p, Phi), 3))


  41. ### Example 5.5 (Error bounds for MC integration)

  42.     x <- 2
  43.     m <- 10000
  44.     z <- rnorm(m)
  45.     g <- (z < x)  #the indicator function
  46.     v <- mean((g - mean(g))^2) / m
  47.     cdf <- mean(g)
  48.     c(cdf, v)
  49.     c(cdf - 1.96 * sqrt(v), cdf + 1.96 * sqrt(v))


  50. ### Example 5.6 (Antithetic variables)

  51.     MC.Phi <- function(x, R = 10000, antithetic = TRUE) {
  52.         u <- runif(R/2)
  53.         if (!antithetic) v <- runif(R/2) else
  54.             v <- 1 - u
  55.         u <- c(u, v)
  56.         cdf <- numeric(length(x))
  57.         for (i in 1:length(x)) {
  58.             g <- x[i] * exp(-(u * x[i])^2 / 2)
  59.             cdf[i] <- mean(g) / sqrt(2 * pi) + 0.5
  60.         }
  61.         cdf
  62.     }


  63.     x <- seq(.1, 2.5, length=5)
  64.     Phi <- pnorm(x)
  65.     set.seed(123)
  66.     MC1 <- MC.Phi(x, anti = FALSE)
  67.     set.seed(123)
  68.     MC2 <- MC.Phi(x)
  69.     print(round(rbind(x, MC1, MC2, Phi), 5))


  70.     m <- 1000
  71.     MC1 <- MC2 <- numeric(m)
  72.     x <- 1.95
  73.     for (i in 1:m) {
  74.         MC1[i] <- MC.Phi(x, R = 1000, anti = FALSE)
  75.         MC2[i] <- MC.Phi(x, R = 1000)
  76.     }

  77.     print(sd(MC1))
  78.     print(sd(MC2))
  79.     print((var(MC1) - var(MC2))/var(MC1))


  80. ### Example 5.7 (Control variate)

  81.     m <- 10000
  82.     a <- - 12 + 6 * (exp(1) - 1)
  83.     U <- runif(m)
  84.     T1 <- exp(U)                  #simple MC
  85.     T2 <- exp(U) + a * (U - 1/2)  #controlled

  86.     mean(T1)   
  87.     mean(T2)   
  88.     (var(T1) - var(T2)) / var(T1)


  89. ### Example 5.8 (MC integration using control variates)

  90.     f <- function(u)
  91.         exp(-.5)/(1+u^2)

  92.     g <- function(u)
  93.         exp(-u)/(1+u^2)

  94.     set.seed(510) #needed later
  95.     u <- runif(10000)
  96.     B <- f(u)
  97.     A <- g(u)

  98.     cor(A, B)
  99.     a <- -cov(A,B) / var(B)    #est of c*
  100.     a

  101.     m <- 100000
  102.     u <- runif(m)
  103.     T1 <- g(u)
  104.     T2 <- T1 + a * (f(u) - exp(-.5)*pi/4)

  105.     c(mean(T1), mean(T2))
  106.     c(var(T1), var(T2))
  107.     (var(T1) - var(T2)) / var(T1)


  108. ### Example 5.9 (Control variate and regression)

  109.     set.seed(510)
  110.     u <- runif(10000)
  111.     f <- exp(-.5)/(1+u^2)
  112.     g <- exp(-u)/(1+u^2)
  113.     c.star <-  - lm(g ~ f)$coeff[2]   # beta[1]
  114.     mu <- exp(-.5)*pi/4

  115.     c.star

  116.     u <- runif(10000)
  117.     f <- exp(-.5)/(1+u^2)
  118.     g <- exp(-u)/(1+u^2)
  119.     L <- lm(g ~ f)
  120.     theta.hat <- sum(L$coeff * c(1, mu))  #pred. value at mu

  121.     theta.hat
  122.     summary(L)$sigma^2
  123.     summary(L)$r.squared


  124. ### Example 5.10 (Choice of the importance function)
  125.     #code for plot is at the end of the file

  126.     m <- 10000
  127.     theta.hat <- se <- numeric(5)
  128.     g <- function(x) {
  129.         exp(-x - log(1+x^2)) * (x > 0) * (x < 1)
  130.         }

  131.     x <- runif(m)     #using f0
  132.     fg <- g(x)
  133.     theta.hat[1] <- mean(fg)
  134.     se[1] <- sd(fg)

  135.     x <- rexp(m, 1)   #using f1
  136.     fg <- g(x) / exp(-x)
  137.     theta.hat[2] <- mean(fg)
  138.     se[2] <- sd(fg)

  139.     x <- rcauchy(m)   #using f2
  140.     i <- c(which(x > 1), which(x < 0))
  141.     x[i] <- 2  #to catch overflow errors in g(x)
  142.     fg <- g(x) / dcauchy(x)
  143.     theta.hat[3] <- mean(fg)
  144.     se[3] <- sd(fg)

  145.     u <- runif(m)     #f3, inverse transform method
  146.     x <- - log(1 - u * (1 - exp(-1)))
  147.     fg <- g(x) / (exp(-x) / (1 - exp(-1)))
  148.     theta.hat[4] <- mean(fg)
  149.     se[4] <- sd(fg)

  150.     u <- runif(m)    #f4, inverse transform method
  151.     x <- tan(pi * u / 4)
  152.     fg <- g(x) / (4 / ((1 + x^2) * pi))
  153.     theta.hat[5] <- mean(fg)
  154.     se[5] <- sd(fg)

  155.     rbind(theta.hat, se)
  156.    

  157. ### Example 5.11 (Example 5.10, cont.)

  158.     M <- 20   #number of replicates
  159.     T2 <- numeric(4)
  160.     estimates <- matrix(0, 10, 2)

  161.     g <- function(x) {
  162.         exp(-x - log(1+x^2)) * (x > 0) * (x < 1) }

  163.     for (i in 1:10) {
  164.         estimates[i, 1] <- mean(g(runif(M)))
  165.         T2[1] <- mean(g(runif(M/4, 0, .25)))
  166.         T2[2] <- mean(g(runif(M/4, .25, .5)))
  167.         T2[3] <- mean(g(runif(M/4, .5, .75)))
  168.         T2[4] <- mean(g(runif(M/4, .75, 1)))
  169.         estimates[i, 2] <- mean(T2)
  170.     }
  171.    
  172.     estimates
  173.     apply(estimates, 2, mean)
  174.     apply(estimates, 2, var)


  175. ### Example 5.12 (Examples 5.10-5.11, cont.)

  176.     M <- 10000  #number of replicates
  177.     k <- 10     #number of strata
  178.     r <- M / k  #replicates per stratum
  179.     N <- 50     #number of times to repeat the estimation
  180.     T2 <- numeric(k)
  181.     estimates <- matrix(0, N, 2)

  182.     g <- function(x) {
  183.         exp(-x - log(1+x^2)) * (x > 0) * (x < 1)
  184.         }

  185.     for (i in 1:N) {
  186.         estimates[i, 1] <- mean(g(runif(M)))
  187.         for (j in 1:k)
  188.             T2[j] <- mean(g(runif(M/k, (j-1)/k, j/k)))
  189.         estimates[i, 2] <- mean(T2)
  190.     }

  191.     apply(estimates, 2, mean)
  192.     apply(estimates, 2, var)
  193.    


  194. ### Plot importance functions in Figures 5.1(a) and 5.1.(b)

  195.     #par(ask = TRUE) #uncomment to pause between graphs
  196.    
  197.     x <- seq(0, 1, .01)
  198.     w <- 2
  199.     f1 <- exp(-x)
  200.     f2 <- (1 / pi) / (1 + x^2)
  201.     f3 <- exp(-x) / (1 - exp(-1))
  202.     f4 <- 4 / ((1 + x^2) * pi)
  203.     g <- exp(-x) / (1 + x^2)

  204.     #for color change lty to col

  205.     #figure (a)
  206.     plot(x, g, type = "l", main = "", ylab = "",
  207.          ylim = c(0,2), lwd = w)
  208.     lines(x, g/g, lty = 2, lwd = w)
  209.     lines(x, f1, lty = 3, lwd = w)
  210.     lines(x, f2, lty = 4, lwd = w)
  211.     lines(x, f3, lty = 5, lwd = w)
  212.     lines(x, f4, lty = 6, lwd = w)
  213.     legend("topright", legend = c("g", 0:4),
  214.            lty = 1:6, lwd = w, inset = 0.02)

  215.     #figure (b)
  216.     plot(x, g, type = "l", main = "", ylab = "",
  217.         ylim = c(0,3.2), lwd = w, lty = 2)
  218.     lines(x, g/f1, lty = 3, lwd = w)
  219.     lines(x, g/f2, lty = 4, lwd = w)
  220.     lines(x, g/f3, lty = 5, lwd = w)
  221.     lines(x, g/f4, lty = 6, lwd = w)
  222.     legend("topright", legend = c(0:4),
  223.            lty = 2:6, lwd = w, inset = 0.02)
复制代码


报纸
Eviewschen 发表于 2014-11-10 10:34:36
  1.     #######################################################
  2.     ###       Statistical Computing with R              ###
  3.     ###       Maria L. Rizzo                            ###
  4.     ###       Chapman & Hall / CRC                      ###
  5.     ###       ISBN 9781584885450                        ###
  6.     ###                                                 ###
  7.     ###       R code for Chapter 6 Examples             ###
  8.     #######################################################


  9. ### Example 6.1 (Basic Monte Carlo estimation)

  10.     m <- 1000
  11.     g <- numeric(m)
  12.     for (i in 1:m) {
  13.         x <- rnorm(2)
  14.         g[i] <- abs(x[1] - x[2])
  15.     }
  16.     est <- mean(g)
  17.     est


  18. ### Example 6.2 (Estimating the MSE of a trimmed mean)

  19.     n <- 20
  20.     m <- 1000
  21.     tmean <- numeric(m)
  22.     for (i in 1:m) {
  23.         x <- sort(rnorm(n))
  24.         tmean[i] <- sum(x[2:(n-1)]) / (n-2)
  25.         }
  26.     mse <- mean(tmean^2)
  27.     mse
  28.     sqrt(sum((tmean - mean(tmean))^2)) / m    #se

  29.     n <- 20
  30.     m <- 1000
  31.     tmean <- numeric(m)
  32.     for (i in 1:m) {
  33.         x <- sort(rnorm(n))
  34.         tmean[i] <- median(x)
  35.         }
  36.     mse <- mean(tmean^2)
  37.     mse
  38.     sqrt(sum((tmean - mean(tmean))^2)) / m    #se


  39. ### Example 6.3 (MSE of a trimmed mean, cont.)

  40.      set.seed(522)
  41.      n <- 20
  42.      K <- n/2 - 1
  43.      m <- 1000
  44.      mse <- matrix(0, n/2, 6)

  45.      trimmed.mse <- function(n, m, k, p) {
  46.          #MC est of mse for k-level trimmed mean of
  47.          #contaminated normal pN(0,1) + (1-p)N(0,100)
  48.          tmean <- numeric(m)
  49.          for (i in 1:m) {
  50.              sigma <- sample(c(1, 10), size = n,
  51.                  replace = TRUE, prob = c(p, 1-p))
  52.              x <- sort(rnorm(n, 0, sigma))
  53.              tmean[i] <- sum(x[(k+1):(n-k)]) / (n-2*k)
  54.              }
  55.          mse.est <- mean(tmean^2)
  56.          se.mse <- sqrt(mean((tmean-mean(tmean))^2)) / sqrt(m)
  57.          return(c(mse.est, se.mse))
  58.      }

  59.     for (k in 0:K) {
  60.         mse[k+1, 1:2] <- trimmed.mse(n=n, m=m, k=k, p=1.0)
  61.         mse[k+1, 3:4] <- trimmed.mse(n=n, m=m, k=k, p=.95)
  62.         mse[k+1, 5:6] <- trimmed.mse(n=n, m=m, k=k, p=.9)
  63.     }

  64. ### Example 6.4 (Confidence interval for variance)

  65.     n <- 20
  66.     alpha <- .05
  67.     x <- rnorm(n, mean=0, sd=2)
  68.     UCL <- (n-1) * var(x) / qchisq(alpha, df=n-1)


  69. ### Example 6.5 (MC estimate of confidence level)

  70.     n <- 20
  71.     alpha <- .05
  72.     UCL <- replicate(1000, expr = {
  73.         x <- rnorm(n, mean = 0, sd = 2)
  74.         (n-1) * var(x) / qchisq(alpha, df = n-1)
  75.         } )
  76.     #count the number of intervals that contain sigma^2=4
  77.     sum(UCL > 4)
  78.     #or compute the mean to get the confidence level
  79.     mean(UCL > 4)


  80. ### Example 6.6 (Empirical confidence level)

  81.     n <- 20
  82.     alpha <- .05
  83.     UCL <- replicate(1000, expr = {
  84.         x <- rchisq(n, df = 2)
  85.         (n-1) * var(x) / qchisq(alpha, df = n-1)
  86.         } )
  87.     sum(UCL > 4)
  88.     mean(UCL > 4)


  89. ### Example 6.7 (Empirical Type I error rate)

  90.     n <- 20
  91.     alpha <- .05
  92.     mu0 <- 500
  93.     sigma <- 100

  94.     m <- 10000          #number of replicates
  95.     p <- numeric(m)     #storage for p-values
  96.     for (j in 1:m) {
  97.         x <- rnorm(n, mu0, sigma)
  98.         ttest <- t.test(x, alternative = "greater", mu = mu0)
  99.         p[j] <- ttest$p.value
  100.         }

  101.     p.hat <- mean(p < alpha)
  102.     se.hat <- sqrt(p.hat * (1 - p.hat) / m)
  103.     print(c(p.hat, se.hat))


  104. ### Example 6.8 (Skewness test of normality)

  105.     n <- c(10, 20, 30, 50, 100, 500) #sample sizes
  106.     cv <- qnorm(.975, 0, sqrt(6/n))  #crit. values for each n

  107.     sk <- function(x) {
  108.         #computes the sample skewness coeff.
  109.         xbar <- mean(x)
  110.         m3 <- mean((x - xbar)^3)
  111.         m2 <- mean((x - xbar)^2)
  112.         return( m3 / m2^1.5 )
  113.     }

  114.     #n is a vector of sample sizes
  115.     #we are doing length(n) different simulations

  116.     p.reject <- numeric(length(n)) #to store sim. results
  117.     m <- 10000                     #num. repl. each sim.

  118.     for (i in 1:length(n)) {
  119.         sktests <- numeric(m)       #test decisions
  120.         for (j in 1:m) {
  121.             x <- rnorm(n[i])
  122.             #test decision is 1 (reject) or 0
  123.             sktests[j] <- as.integer(abs(sk(x)) >= cv[i] )
  124.             }
  125.         p.reject[i] <- mean(sktests) #proportion rejected
  126.     }

  127.     p.reject


  128. ### Example 6.9 (Empirical power)

  129.     n <- 20
  130.     m <- 1000
  131.     mu0 <- 500
  132.     sigma <- 100
  133.     mu <- c(seq(450, 650, 10))  #alternatives
  134.     M <- length(mu)
  135.     power <- numeric(M)
  136.     for (i in 1:M) {
  137.         mu1 <- mu[i]
  138.         pvalues <- replicate(m, expr = {
  139.             #simulate under alternative mu1
  140.             x <- rnorm(n, mean = mu1, sd = sigma)
  141.             ttest <- t.test(x,
  142.                      alternative = "greater", mu = mu0)
  143.             ttest$p.value  } )
  144.         power[i] <- mean(pvalues <= .05)
  145.     }

  146.     par(ask = TRUE)
  147.     library(Hmisc)  #for errbar
  148.     plot(mu, power)
  149.     abline(v = mu0, lty = 1)
  150.     abline(h = .05, lty = 1)

  151.     #add standard errors
  152.     se <- sqrt(power * (1-power) / m)
  153.     errbar(mu, power, yplus = power+se, yminus = power-se,
  154.         xlab = bquote(theta))
  155.     lines(mu, power, lty=3)
  156.     detach(package:Hmisc)
  157.     par(ask = FALSE)


  158. ### Example 6.10 (Power of the skewness test of normality)

  159.     alpha <- .1
  160.     n <- 30
  161.     m <- 2500
  162.     epsilon <- c(seq(0, .15, .01), seq(.15, 1, .05))
  163.     N <- length(epsilon)
  164.     pwr <- numeric(N)
  165.     #critical value for the skewness test
  166.     cv <- qnorm(1-alpha/2, 0, sqrt(6*(n-2) / ((n+1)*(n+3))))

  167.     for (j in 1:N) {           #for each epsilon
  168.         e <- epsilon[j]
  169.         sktests <- numeric(m)
  170.         for (i in 1:m) {       #for each replicate
  171.             sigma <- sample(c(1, 10), replace = TRUE,
  172.                 size = n, prob = c(1-e, e))
  173.             x <- rnorm(n, 0, sigma)
  174.             sktests[i] <- as.integer(abs(sk(x)) >= cv)
  175.             }
  176.         pwr[j] <- mean(sktests)
  177.         }
  178.     #plot power vs epsilon
  179.     plot(epsilon, pwr, type = "b",
  180.          xlab = bquote(epsilon), ylim = c(0,1))
  181.     abline(h = .1, lty = 3)
  182.     se <- sqrt(pwr * (1-pwr) / m)  #add standard errors
  183.     lines(epsilon, pwr+se, lty = 3)
  184.     lines(epsilon, pwr-se, lty = 3)


  185. ### Example 6.11 (Power comparison of tests of normality)

  186.     #only one loop, for epsilon=0.1, was shown in the text
  187.     #the simulation below takes several minutes to run
  188.    
  189.     # initialize input and output
  190.     library(energy)
  191.     alpha <- .1
  192.     n <- 30
  193.     m <- 500        #try small m for a trial run
  194.     test1 <- test2 <- test3 <- numeric(m)

  195.     #critical value for the skewness test
  196.     cv <- qnorm(1-alpha/2, 0, sqrt(6*(n-2) / ((n+1)*(n+3))))
  197.     sim <- matrix(0, 11, 4)

  198.     # estimate power
  199.     for (i in 0:10) {
  200.         epsilon <- i * .1
  201.         for (j in 1:m) {
  202.             e <- epsilon
  203.             sigma <- sample(c(1, 10), replace = TRUE,
  204.                 size = n, prob = c(1-e, e))
  205.             x <- rnorm(n, 0, sigma)
  206.             test1[j] <- as.integer(abs(sk(x)) >= cv)
  207.             test2[j] <- as.integer(
  208.                         shapiro.test(x)$p.value <= alpha)
  209.             test3[j] <- as.integer(
  210.                         mvnorm.etest(x, R=200)$p.value <= alpha)
  211.             }
  212.         print(c(epsilon, mean(test1), mean(test2), mean(test3)))  
  213.         sim[i+1, ] <- c(epsilon, mean(test1), mean(test2), mean(test3))
  214.     }   
  215.     detach(package:energy)

  216.     # plot the empirical estimates of power
  217.     plot(sim[,1], sim[,2], ylim = c(0, 1), type = "l",
  218.         xlab = bquote(epsilon), ylab = "power")
  219.     lines(sim[,1], sim[,3], lty = 2)
  220.     lines(sim[,1], sim[,4], lty = 4)
  221.     abline(h = alpha, lty = 3)
  222.     legend("topright", 1, c("skewness", "S-W", "energy"),
  223.         lty = c(1,2,4), inset = .02)


  224. ### Example 6.12 (Count Five test statistic)

  225.     x1 <- rnorm(20, 0, sd = 1)
  226.     x2 <- rnorm(20, 0, sd = 1.5)
  227.     y <- c(x1, x2)

  228.     group <- rep(1:2, each = length(x1))
  229.     boxplot(y ~ group, boxwex = .3, xlim = c(.5, 2.5), main = "")
  230.     points(group, y)

  231.     # now identify the extreme points
  232.     range(x1)
  233.     range(x2)

  234.     i <- which(x1 < min(x2))
  235.     j <- which(x2 > max(x1))

  236.     x1[i]
  237.     x2[j]

  238.     out1 <- sum(x1 > max(x2)) + sum(x1 < min(x2))
  239.     out2 <- sum(x2 > max(x1)) + sum(x2 < min(x1))
  240.     max(c(out1, out2))


  241. ### Example 6.13 (Count Five test statistic, cont.)

  242.     maxout <- function(x, y) {
  243.         X <- x - mean(x)
  244.         Y <- y - mean(y)
  245.         outx <- sum(X > max(Y)) + sum(X < min(Y))
  246.         outy <- sum(Y > max(X)) + sum(Y < min(X))
  247.         return(max(c(outx, outy)))
  248.     }

  249.     n1 <- n2 <- 20
  250.     mu1 <- mu2 <- 0
  251.     sigma1 <- sigma2 <- 1
  252.     m <- 1000

  253.     # generate samples under H0
  254.     stat <- replicate(m, expr={
  255.         x <- rnorm(n1, mu1, sigma1)
  256.         y <- rnorm(n2, mu2, sigma2)
  257.         maxout(x, y)
  258.         })
  259.     print(cumsum(table(stat)) / m)
  260.     print(quantile(stat, c(.8, .9, .95)))


  261. ### Example 6.14 (Count Five test)

  262.     count5test <- function(x, y) {
  263.         X <- x - mean(x)
  264.         Y <- y - mean(y)
  265.         outx <- sum(X > max(Y)) + sum(X < min(Y))
  266.         outy <- sum(Y > max(X)) + sum(Y < min(X))
  267.         # return 1 (reject) or 0 (do not reject H0)
  268.         return(as.integer(max(c(outx, outy)) > 5))
  269.     }

  270.     n1 <- n2 <- 20
  271.     mu1 <- mu2 <- 0
  272.     sigma1 <-  sigma2 <- 1
  273.     m <- 10000
  274.     tests <- replicate(m, expr = {
  275.         x <- rnorm(n1, mu1, sigma1)
  276.         y <- rnorm(n2, mu2, sigma2)
  277.         x <- x - mean(x)  #centered by sample mean
  278.         y <- y - mean(y)
  279.         count5test(x, y)
  280.         } )

  281.     alphahat <- mean(tests)
  282.     print(alphahat)


  283. ### Example 6.15 (Count Five test, cont.)

  284.     n1 <- 20
  285.     n2 <- 30
  286.     mu1 <- mu2 <- 0
  287.     sigma1 <- sigma2 <- 1
  288.     m <- 10000

  289.     alphahat <- mean(replicate(m, expr={
  290.         x <- rnorm(n1, mu1, sigma1)
  291.         y <- rnorm(n2, mu2, sigma2)
  292.         x <- x - mean(x)  #centered by sample mean
  293.         y <- y - mean(y)
  294.         count5test(x, y)
  295.         }))

  296.     print(alphahat)


  297. ### Example 6.16 (Count Five, cont.)

  298.     # generate samples under H1 to estimate power
  299.     sigma1 <- 1
  300.     sigma2 <- 1.5

  301.     power <- mean(replicate(m, expr={
  302.         x <- rnorm(20, 0, sigma1)
  303.         y <- rnorm(20, 0, sigma2)
  304.         count5test(x, y)
  305.         }))

  306.     print(power)
复制代码


地板
jiangqing001 发表于 2014-11-21 16:57:55
牛啊!!

7
lyslz 发表于 2014-11-24 22:16:49
zap na er ne ?

8
潭生.经济学笔记 发表于 2014-12-1 16:10:36
【独家发布】Statistical Computing with R [

9
潭生.经济学笔记 发表于 2014-12-1 16:59:29
Statistical Computing with R

10
Elodie1120 发表于 2016-10-24 15:04:54
提示: 作者被禁止或删除 内容自动屏蔽

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2025-12-23 00:54