Script started on Thu Sep 18 11:47:31 2003 charlie@linux:~> R R : Copyright 2003, The R Development Core Team Version 1.7.1 (2003-06-16) 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. R is a collaborative project with many contributors. Type `contributors()' for more information. Type `demo()' for some demos, `help()' for on-line help, or `help.start()' for a HTML browser interface to help. Type `q()' to quit R. > help.start() Making links in per-session dir ... If /usr/X11R6/bin/mozilla is already running, it is *not* restarted, and you must switch to its window. Otherwise, be patient ... > x <- rnorm(1000) > n <- length(x) > mean(x) [1] -0.01924949 > 1 / sqrt(n) [1] 0.03162278 > x <- rnorm(1000) > mean(x) [1] 0.01477046 > nsim <- 20 > xbar <- double(nsim) > xbar [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 > xbar <- rep(NA, nsim) > xbar [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA > typeof(xbar) [1] "logical" > xbar <- rep(as.double(NA), nsim) > Ptypeof(xbar) [1] "double" > for (i in 1:nsim) { + x <- rnorm(1000) + xbar[i] <- mean(x) + } > xbar [1] 0.027322385 0.014639452 0.018728844 0.007332647 0.075381699 [6] -0.001359719 0.047883283 0.001484629 -0.013770117 0.019035405 [11] -0.004482766 -0.040656347 0.012141740 -0.012281803 0.020878131 [16] -0.017306328 -0.022794408 0.013120918 0.024737451 -0.008563260 > sqrt(n) * xbar [1] 0.86400968 0.46294013 0.59225805 0.23187865 2.38377864 -0.04299810 [7] 1.51420237 0.04694808 -0.43544933 0.60195237 -0.14175751 -1.28566658 [13] 0.38395552 -0.38838470 0.66022448 -0.54727413 -0.72082247 0.41491986 [19] 0.78226687 -0.27079406 > > xbar <- apply(matrix(rnorm(n * nsim), nrow = nsim), 1, mean) > sqrt(n) * xbar [1] 1.7056982 0.7630410 0.2523016 -0.7452940 -0.1782290 0.7043279 [7] -1.5892250 1.5191294 -0.3958290 -0.1910529 0.2440044 -1.5032009 [13] 1.9923342 0.9281348 0.4945629 0.3686596 0.1920336 -0.1022817 [19] -0.7826228 1.5551621 > > hist(x) > hist(x, freq = FALSE) > > y <- x > rm(x) > > curve(dnorm(x), add = TRUE) > hist(y, freq = FALSE) > curve(dnorm, add = TRUE) > hist(y, freq = FALSE) > curve(dnorm(x), add = TRUE) > > qqnorm(y) > qqline(y) > > y <- rt(n, df = 20) > qqnorm(y) > qqline(y) > abline(0, 1, col = "red") > hist(y, freq = FALSE) > curve(dnorm(x), add = TRUE) > y <- rgamma(n, shape = 5) > qqnorm(y) > qqline(y) > > > > > > n <- 15 > x <- rnorm(n) > t.test(x) One Sample t-test data: x t = 0.4981, df = 14, p-value = 0.6262 alternative hypothesis: true mean is not equal to 0 95 percent confidence interval: -0.3410829 0.5474206 sample estimates: mean of x 0.1031689 > out <- t.test(x) > names(out) [1] "statistic" "parameter" "p.value" "conf.int" "estimate" [6] "null.value" "alternative" "method" "data.name" > out$conf.int [1] -0.3410829 0.5474206 attr(,"conf.level") [1] 0.95 > attr(out$conf.int, "conf.level") [1] 0.95 > inter <- matrix(NA, nsim, 2) > for (i in 1:nsim) { + x <- rnorm(n) + inter[i, ] <- t.test(x)$conf.int + } > inter [,1] [,2] [1,] -0.6230075 0.652322383 [2,] -0.1002086 0.959255131 [3,] -0.5465631 0.653911442 [4,] -0.7949883 0.450526669 [5,] -0.7995636 0.075958774 [6,] -0.7951414 0.038211460 [7,] -0.4825687 0.445009165 [8,] -0.8012332 0.465262601 [9,] -0.5665515 0.383956605 [10,] -0.3932760 0.678685713 [11,] -0.5677305 0.352725605 [12,] -0.5260319 0.658156540 [13,] -0.4451224 0.723730375 [14,] -0.9924843 0.008796995 [15,] -0.7386977 0.218947968 [16,] -0.4153280 0.730036821 [17,] -0.7728200 0.230006484 [18,] -0.1733477 0.978763581 [19,] -1.0584811 0.294379054 [20,] -0.9506326 0.220727668 > nsim <- 1000 > inter <- matrix(NA, nsim, 2) > for (i in 1:nsim) { + x <- rnorm(n) + inter[i, ] <- t.test(x)$conf.int + } > inter > mean(apply(inter, 1, function(xx) xx[1] < 0 & xx[2] > 0)) [1] 0.948 > sum(apply(inter, 1, function(xx) xx[1] < 0 & xx[2] > 0)) [1] 948 > prop.test(948, 1000) 1-sample proportions test with continuity correction data: 948 out of 1000, null probability 0.5 X-squared = 801.025, df = 1, p-value = < 2.2e-16 alternative hypothesis: true p is not equal to 0.5 95 percent confidence interval: 0.9318847 0.9605632 sample estimates: p 0.948 > prop.test(948, 1000, p = 0.95) 1-sample proportions test with continuity correction data: 948 out of 1000, null probability 0.95 X-squared = 0.0474, df = 1, p-value = 0.8277 alternative hypothesis: true p is not equal to 0.95 95 percent confidence interval: 0.9318847 0.9605632 sample estimates: p 0.948 > x <- rnorm(1000) > help(ks.test) help() for ks.test is shown in browser /usr/X11R6/bin/mozilla ... Use help( ks.test , htmlhelp=FALSE) or options(htmlhelp = FALSE) to revert. > ks.test(x, "pnorm") One-sample Kolmogorov-Smirnov test data: x D = 0.0239, p-value = 0.6183 alternative hypothesis: two.sided > ks.test(x, "pnorm", mean = mean(x), sd = sd(x)) One-sample Kolmogorov-Smirnov test data: x D = 0.017, p-value = 0.9334 alternative hypothesis: two.sided > out <- ks.test(x, "pnorm", mean = mean(x), sd = sd(x)) > names(out) [1] "statistic" "p.value" "alternative" "method" "data.name" > pobs <- out$p.value > pobs [1] 0.9334424 > sim <- 20 > pval <- double(nsim) > for (i in 1:nsim) { + xsim <- rnorm(length(x)) + pval[i] <- ks.test(xsim, "pnorm", mean = mean(xsim), + sd = sd(xsim))$p.value + } > pval [1] 0.8878497 0.8739063 0.9917189 0.9104869 0.9994813 0.8862057 0.9349086 [8] 0.4846153 0.9884739 0.9108062 0.4836826 0.5743762 0.6646885 0.9955119 [15] 0.8999997 0.8572471 0.9759040 0.9598516 0.8557397 0.5474920 > pobs > pval [1] TRUE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE TRUE TRUE [13] TRUE FALSE TRUE TRUE FALSE FALSE TRUE TRUE > pobs <= pval [1] FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE [13] FALSE TRUE FALSE FALSE TRUE TRUE FALSE FALSE > if (x > 0) { + print("foo!") + } [1] "foo!" Warning message: the condition has length > 1 and only the first element will be used in: if (x > 0) { > y <- ifelse(x > 0, x, 0) > y[1:20] [1] 0.3203178 0.0000000 0.0000000 0.0000000 1.4363970 0.0000000 0.0000000 [8] 1.3286626 3.5888480 1.1689323 0.6903404 1.5677232 0.0000000 1.0432395 [15] 0.0000000 0.0000000 1.0174696 0.0000000 0.4290021 0.3817566 > q() Save workspace image? [y/n/c]: exit Save workspace image? [y/n/c]: n charlie@linux:~> exit exit Script done on Thu Sep 18 13:01:02 2003