Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
> install.packages("ggplot2")
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/ggplot2_3.1.1.zip'
Content type 'application/zip' length 3621814 bytes (3.5 MB)
downloaded 3.5 MB
Warning in install.packages :
downloaded length 16478208 != reported length 19008161
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/plogr_0.2.0.zip'
Content type 'application/zip' length 18731 bytes (18 KB)
downloaded 18 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/extrafontdb_1.0.zip'
Content type 'application/zip' length 10261 bytes (10 KB)
downloaded 10 KB
trying URL 'https://cran.rstudio.com/bin/windows/contrib/3.5/Rttf2pt1_1.3.7.zip'
Content type 'application/zip' length 117470 bytes (114 KB)
downloaded 114 KB
Selection: 13
| | 0%
...
|== | 3%
| This lesson assumes familiarity with a few common probability distributions, but
| these topics will only be discussed with respect to random number generation. Even
| if you have no prior experience with these concepts, you should be able to
| complete the lesson and understand the main ideas.
...
|===== | 6%
| The first function we'll use to generate random numbers is sample(). Use ?sample
| to pull up the documentation.
> ?sample
| Excellent job!
|======= | 9%
| Let's simulate rolling four six-sided dice: sample(1:6, 4, replace = TRUE).
> sample(1:6, 4, replace = TRUE)
[1] 6 3 3 3
|========= | 12%
| Now repeat the command to see how your result differs. (The probability of rolling
| the exact same result is (1/6)^4 = 0.00077, which is pretty small!)
...
|============== | 18%
| Now sample 10 numbers between 1 and 20, WITHOUT replacement. To sample without
| replacement, simply leave off the 'replace' argument.
|================ | 21%
| Since the last command sampled without replacement, no number appears more than
| once in the output.
...
|================== | 24%
| LETTERS is a predefined variable in R containing a vector of all 26 letters of the
| English alphabet. Take a look at it now.
> R <-c(alpha(26))
| Not quite right, but keep trying. Or, type info() for more options.
| Nice work!
|==================== | 27%
| The sample() function can also be used to permute, or rearrange, the elements of a
| vector. For example, try sample(LETTERS) to permute all 26 letters of the English
| alphabet.
> sample(LETTERS)
[1] "R" "C" "Q" "P" "T" "X" "O" "K" "F" "G" "U" "A" "H" "I" "D" "E" "L" "J" "Y" "W"
[21] "Z" "M" "V" "N" "S" "B"
|======================= | 30%
| This is identical to taking a sample of size 26 from LETTERS, without replacement.
| When the 'size' argument to sample() is not specified, R takes a sample equal in
| size to the vector from which you are sampling.
...
|========================= | 33%
| Now, suppose we want to simulate 100 flips of an unfair two-sided coin. This
| particular coin has a 0.3 probability of landing 'tails' and a 0.7 probability of
| landing 'heads'.
...
|=========================== | 36%
| Let the value 0 represent tails and the value 1 represent heads. Use sample() to
| draw a sample of size 100 from the vector c(0,1), with replacement. Since the coin
| is unfair, we must attach specific probabilities to the values 0 (tails) and 1
| (heads) with a fourth argument, prob = c(0.3, 0.7). Assign the result to a new
| variable called flips.
> sample(1:100, replace c(0, 1))
Error: unexpected symbol in "sample(1:100, replace c"
> sample(1:100)
[1] 52 28 34 76 26 24 82 58 69 11 2 99 55 98 96 78 27 68 40 44
[21] 49 75 29 83 61 47 43 74 60 25 6 91 7 59 66 100 12 63 5 90
[41] 85 94 80 13 92 56 39 30 64 71 57 16 1 8 62 23 54 37 20 86
[61] 32 87 18 33 50 77 67 22 41 84 17 35 65 79 4 42 73 72 14 97
[81] 48 21 89 51 38 88 15 10 46 93 81 95 36 19 31 45 70 53 9 3
| Almost! Try again. Or, type info() for more options.
| The following command will produce 100 flips of an unfair coin and assign the
| result: flips <- sample(c(0,1), 100, replace = TRUE, prob = c(0.3, 0.7))
> flips <- sample(c(0,1), 100, replace = TRUE, prob = c(0.3, 0.7))
|================================ | 42%
| Since we set the probability of landing heads on any given flip to be 0.7, we'd
| expect approximately 70 of our coin flips to have the value 1. Count the actual
| number of 1s contained in flips using the sum() function.
> sum(1)
[1] 1
| You almost had it, but not quite. Try again. Or, type info() for more options.
| sum(flips) will add up all the 1s and 0s, giving you the total number of 1s in
| flips.
> sum(flips)
[1] 69
|================================== | 45%
| A coin flip is a binary outcome (0 or 1) and we are performing 100 independent
| trials (coin flips), so we can use rbinom() to simulate a binomial random
| variable. Pull up the documentation for rbinom() using ?rbinom.
> ?rbinom
| You got it!
|==================================== | 48%
| Each probability distribution in R has an r*** function (for "random"), a d***
| function (for "density"), a p*** (for "probability"), and q*** (for "quantile").
| We are most interested in the r*** functions in this lesson, but I encourage you
| to explore the others on your own.
...
|======================================= | 52%
| A binomial random variable represents the number of 'successes' (heads) in a given
| number of independent 'trials' (coin flips). Therefore, we can generate a single
| random variable that represents the number of heads in 100 flips of our unfair
| coin using rbinom(1, size = 100, prob = 0.7). Note that you only specify the
| probability of 'success' (heads) and NOT the probability of 'failure' (tails). Try
| it now.
> rbinom(1, size = 100, prob = 0.7)
[1] 71
| Keep working like that and you'll get there!
|========================================= | 55%
| Equivalently, if we want to see all of the 0s and 1s, we can request 100
| observations, each of size 1, with success probability of 0.7. Give it a try,
| assigning the result to a new variable called flips2.
| You almost had it, but not quite. Try again. Or, type info() for more options.
| Call rbinom() with n = 100, size = 1, and prob = 0.7 and assign the result to
| flips2.
| Call rbinom() with n = 100, size = 1, and prob = 0.7 and assign the result to
| flips2.
|=========================================== | 58%
| View the contents of flips2.
> flips2
[1] 1 1 1 0 0 0 1 1 0 0 1 1 0 0 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 1 1 1
[41] 1 1 0 1 1 1 1 1 1 1 0 1 0 0 0 1 0 1 1 1 0 1 0 1 0 0 0 1 0 0 1 1 1 1 1 1 1 0 1 1
[81] 1 1 1 1 1 0 1 1 1 1 1 1 0 0 0 1 1 1 1 0
| You got it!
|============================================= | 61%
| Now use sum() to count the number of 1s (heads) in flips2. It should be close to
| 70!
> sum(heads)
Error: object 'heads' not found
> sum()
[1] 0
| Nice try, but that's not exactly what I was hoping for. Try again. Or, type info()
| for more options.
> sum(flips2)
[1] 68
| Excellent job!
|================================================ | 64%
| Similar to rbinom(), we can use R to simulate random numbers from many other
| probability distributions. Pull up the documentation for rnorm() now.
> rbinom(flips2)
Error in rbinom(flips2) : argument "size" is missing, with no default
> rnorm
function (n, mean = 0, sd = 1)
.Call(C_rnorm, n, mean, sd)
<bytecode: 0x10b4cb58>
<environment: namespace:stats>
| That's not exactly what I'm looking for. Try again. Or, type info() for more
| options.
| Type ?rnorm to view its help file.
> ?rnorm
> rnorm(10)
[1] 0.01435526 0.49670000 0.66719830 1.02615267 -0.17098444 1.17686717
[7] 0.56218999 -1.86215735 0.19312051 0.19784360
|==================================================== | 70%
| Now do the same, except with a mean of 100 and a standard deviation of 25.
> rnorm(100)
[1] 0.172982264 0.442130709 1.864432054 -0.563658921 -0.067282818 -1.851926229
[7] -0.556564192 2.269150370 -0.685331874 -0.805485164 0.335495327 -0.394761604
[13] -0.956868261 -0.010210579 -1.189646103 0.160903637 1.856238067 -1.130824834
[19] 0.702174906 -0.371508187 0.099294368 0.470084058 -0.934711088 -3.061130102
[25] -0.365677807 0.580680575 0.991796828 -0.244324909 -1.609483787 0.450058541
[31] 0.027285275 -0.336745830 -0.778377563 0.883549842 0.574392932 0.719208994
[37] -1.039703029 -0.475189910 -0.716451636 1.652040225 -0.260225182 -0.410915137
[43] -0.561969953 -1.043920112 1.321353245 0.376004793 -2.335380621 -1.687582759
[49] -0.464430671 -0.906059982 1.428993659 -0.011937050 -0.185916151 0.831380637
[55] 0.393130938 2.201306415 -0.482690652 0.315669653 0.830499039 1.603397355
[61] -0.770923661 0.483206820 -1.354875685 -0.229199596 0.776389972 0.824021565
[67] 0.003776438 -2.394822104 -1.692482547 0.531568093 1.039211524 -0.754086514
[73] -1.162728751 0.137876345 -1.790014210 1.266090940 0.955791981 0.863610915
[79] -0.170376789 1.504130968 0.683256893 0.458620053 -1.655138562 0.684304446
[85] 1.349127221 -1.727231759 1.086160568 -0.000491046 -0.250607431 0.059923930
[91] -0.944592227 -0.007397085 0.764051341 -0.683633702 -0.973472473 0.279404906
[97] 1.797531741 1.360690822 -1.723714186 0.828252276
| Not quite right, but keep trying. Or, type info() for more options.
| Use rnorm(10, mean = 100, sd = 25) to generate 10 random numbers from a normal
| distribution with mean 100 and standard deviation 25.
| Excellent work!
|======================================================= | 73%
| Finally, what if we want to simulate 100 *groups* of random numbers, each
| containing 5 values generated from a Poisson distribution with mean 10? Let's
| start with one group of 5 numbers, then I'll show you how to repeat the operation
| 100 times in a convenient and compact way.
...
|========================================================= | 76%
| Generate 5 random values from a Poisson distribution with mean 10. Check out the
| documentation for rpois() if you need help.
> ?rpois
> ?rpois()
>
>
>
>
> rpois(mean(10))
Error in rpois(mean(10)) : argument "lambda" is missing, with no default
> info()
> swirl()
| Welcome to swirl! Please sign in. If you've been here before, use the same name as
| you did then. If you are new, call yourself something unique.
1: R Programming Simulation
2: No. Let me start something new.
Selection