9/9/2020
Chapter 3 - Basic Programming
- Notational conventions
- Variables - simple variables, vectors, matrices, and arrays
- Objects - variables and user-definied functions
- Branching with if and if-else --- syntax matters, be careful
- sums - with loops or sum(x)
- products - with loops or prod(1:n) or factorial(n)
- Memory use - changing the size of an array takes time
- preallocation - creating a vector
- redimensioning - changing the size of a vector
- While loops - used when we do not know how many times we will need to execute a loop
- Vector based programming - vector opearations are more efficient than looping
- ifelse - very powerful function - e.g., ifelse(x>0, "Pos", "Neg")
- pmin() and pmax() are vectorized versions of min() and max()
- e.g., pmin(c(1,2,3), c(3,2,1), c(2,2,2))
- Program flow - charting the flow of a program is extremely helpful for verifying your logic (i.e., is the program doing what you want?) and finding errors in your code.
- Debugging
- Tip 1 - Use cat() to examing values of your variables at various stages throughout your program
- Tip 2 - Use the KISS principle
- Tip 3 - Use dry runs where you know the answer to test your code
R functions - see RVs.R in our Google Drive folder
- What is the difference between the R commands ddist, pdist, qdist, and rdist?
Chapter 16 - Discrete Random Variables
Breakout room activities
- Create an R Script or Markdown file to replicate the graphs shown in Figure 16.1 on page 313.
- Create an R Script or Markdown file to replicate the graphs shown in Figure 16.2 on page 315.
- Create an R Script or Markdown file to replicate the graphs shown in Figure 16.3 on page 317.
- Create an R Script or Markdown file to replicate the graphs shown in Figure 16.4 on page 319.
- Modify the code on p. 292 (see expex.r in scripts folder - Google Drive\Stat226-Statistical Computing-F2020\!spuR supplements 2.0\resources\scripts) to approximate the mean of a geom(.5) random variable. Clearly define your random varaible and interpret the mean.
We will have a problem session on Friday.