|
| 1 | +# Statistical Programming |
| 2 | + |
| 3 | +## Setup: Installing R and Tools |
| 4 | + |
| 5 | +```r |
| 6 | +install.packages("ggplot2") |
| 7 | +``` |
| 8 | + |
| 9 | +## Statistical Computing |
| 10 | +- Statistics → modeling, inference, probability |
| 11 | +- Computer Science → algorithms, efficiency |
| 12 | +- Data → storage, processing, transformation |
| 13 | +- Programming → automation and reproducibility |
| 14 | +- Graphics → visualization and communication |
| 15 | +- Simulation → e.g., Monte Carlo methods |
| 16 | + |
| 17 | +Statistical computing is the use of computational methods to perform statistical analysis, simulation, and data visualization. |
| 18 | + |
| 19 | +## Main Components of Statistical Computing |
| 20 | + |
| 21 | +1. Data Manipulation |
| 22 | +```r |
| 23 | +subset(cars, speed > 10) |
| 24 | +``` |
| 25 | +2. Programming |
| 26 | +```r |
| 27 | +square <- function(x) { |
| 28 | + x^2 |
| 29 | +} |
| 30 | +``` |
| 31 | +3. Graphics |
| 32 | +```r |
| 33 | +plot(cars) |
| 34 | +``` |
| 35 | + |
| 36 | +## Object-Oriented Nature of `R` |
| 37 | +```r |
| 38 | +class(cars) |
| 39 | +summary(cars) |
| 40 | +plot(cars) |
| 41 | + |
| 42 | +``` |
| 43 | +- Regression Example |
| 44 | +```r |
| 45 | +out <- lm(dist ~ speed, data = cars) |
| 46 | +class(out) |
| 47 | +summary(out) |
| 48 | +plot(out) |
| 49 | +``` |
| 50 | + |
| 51 | +## Command Line Options and Helps |
| 52 | +- `-save` or `-no-save` |
| 53 | +- `-restore` or `-no-restore` |
| 54 | + |
| 55 | +```r |
| 56 | +help(search) |
| 57 | +``` |
| 58 | + |
| 59 | +## Syntax and Grammar |
| 60 | + |
| 61 | +A complete expression is any typed expression that falls into one of the following seven classes: |
| 62 | + |
| 63 | +- Literals |
| 64 | +- Calls |
| 65 | +- Assignments |
| 66 | +- Conditionals: comparison, logical |
| 67 | +- Loops |
| 68 | +- Control flow statements |
| 69 | +- Grouping statements |
| 70 | + |
| 71 | +## R Sessions |
| 72 | +`setwd` and `getwd` |
| 73 | + |
| 74 | +Use `?Syntax` to find operator syntax |
| 75 | + |
| 76 | +If unsure of operator precedence, use parentheses. |
| 77 | + |
| 78 | +## R Objects |
| 79 | + |
| 80 | +All objects created in command line (`RStudio` calls it console) are saved |
| 81 | +in `.GlobalEnv`. |
| 82 | + |
| 83 | +`R` treats everything as an object |
| 84 | +- data objects |
| 85 | +- function objects |
| 86 | +- list objects |
| 87 | +- `NULL` objects |
| 88 | +- `...` object |
| 89 | +- language objects |
| 90 | +- symbol objects |
| 91 | +- expression objects |
| 92 | + |
| 93 | +```r |
| 94 | +ls() |
| 95 | +objects() |
| 96 | +ls.str() |
| 97 | +find("lm") |
| 98 | +help.search("lm") |
| 99 | +rm(x) |
| 100 | +rm(x, y, z) |
| 101 | +rm(list = ls()) |
| 102 | +``` |
| 103 | + |
| 104 | +### Class, Type of Length of an Object |
| 105 | +```r |
| 106 | +class(cars) |
| 107 | +typeof(cars) |
| 108 | +length(cars) |
| 109 | +attributes(cars) |
| 110 | +names(cars) |
| 111 | +dim(cars) |
| 112 | +``` |
0 commit comments