Summary of Statistical Analysis with R
Statistical Analysis with R: A Student's Guide to Models & Data
Introduction
This material provides an overview of fundamental concepts and tools in the R programming language, designed for self-study by a distance learning student. We cover working with data, basic functions, data frames, exploratory data analysis (EDA), and basic graphics. The goal is to provide practical examples that can be run immediately in R.
Basic Functions and Operations
R includes a variety of built-in functions for calculations and working with vectors.
Definition: Functions are named operations that accept arguments in parentheses and return a value.
Common Mathematical and Transformation Functions
- trigonometric: sin, cos, tan, asin, acos, atan
- logarithmic: log, log2, log10 (in LaTeX: log is the \log function)
- other: sqrt, exp, abs, sum, prod
Practical Notes:
- Function and variable names are case sensitive.
- The underscore "_" is not used in names.
- Vectors can be numeric, character, or logical.
- Function arguments can be specified by name or implicitly by order.
Example of Scaling and Centering
Definition: Centering means subtracting the mean; scaling means dividing by the standard deviation.
Example of calculation and using scale():
mean(y)
var(y)
y2 <- scale(y)
attr(y2, "scaled:center")
attr(y2, "scaled:scale")
mean(y2)
var(y2)
Explanation: After applying scale(), the mean of the transformed vector $y2$ will be $0$ and the variance will be $1$ (if default settings are used).
Data Frames (data.frame)
Data frames in R are a fundamental structure for tabular data, where columns represent variables and rows represent observations.
Definition: A data frame is a table where all columns have the same length; non-numeric columns can be converted to a factor (category).
Creation and Import:
- creation: data.frame, rep, factor, levels, relevel
- export: write.table
- import from Excel via clipboard: read.delim("clipboard")
Rules:
- The first row often contains variable names (without spaces).
- Missing data are denoted as NA.
- All columns must have the same number of rows.
Functions for Data Manipulation:
- is.na, $, attach, names
Example Table (Data Frame Content):
| soil | field | distance | amount |
|---|---|---|---|
| moist | pasture | 12 | 0.22 |
| moist | pasture | 22 | 0.11 |
| moist | pasture | 43 | 0.29 |
| moist | pasture | 23 | 0.33 |
| moist | rape | 32 | 0.19 |
| moist | rape | 67 | 0.39 |
| moist | rape | 54 | 0.18 |
| moist | rape | NA | 0.29 |
| dry | pasture | 11 | 1.16 |
| dry | pasture | 33 | 1.03 |
| dry | pasture | 45 | 1.11 |
| dry | pasture | NA | 1.33 |
| dry | rape | 55 | 1.02 |
| dry | rape | 41 | 1.23 |
| dry | rape | 14 | 1.05 |
| dry | rape | 27 | 1.12 |
Example of Working with Factors:
size <- rep(c("small","medium","large"), c(4,3,4))
dat <- data.frame(x, y, size)
is.factor(size)
size <- factor(size)
levels(size)
size1 <- relevel(size, ref="medium")
levels(size1)
Explanation: factor() converts a character vector into a categorical variable with levels; relevel() sets the preferred reference level.
Exploratory Data Analysis (EDA)
EDA is a visual and tabular data analysis used to uncover errors, inform model design, verify assumptions, and find unexpected trends.
Goals of EDA:
- check for data errors
- gain insight into distribution and variability
- suggest appropriate statistical methods
- formulate hypotheses
Definition: The Expected Value $E(y)$ is the theoretical long-run average of a variable; the empirical estimate is the arithmetic mean.
Measures of Central Tendency and Robust Estimates
- mean: mean()
- median: median() — robust to asymmetry
- trimmed mean: mean(y, trim=) — removes $\alpha n$ observations from each tail
Example:
mean(amount)
median(amount)
mean(amount, trim=0.1)
Variance and Standard Deviation
- variance: var()
- standard deviation: sd()
- range: range(), min(), max()
- quantiles: quantile()
Definition: The Standard Error of the Mea
Already have an account? Sign in
R Programming - Fundamentals
Klíčové pojmy: Names in R are case-sensitive, The scale() function performs centering and scaling (mean 0, SD 1), Data frames have columns of equal length, and missing values are NA, Factors: creating with factor(), changing the reference level with relevel(), Mean(), median(), mean(trim=) for measures of central tendency, Var(), sd(), SEM = $s/\sqrt{n}$ for variability, Tapply() and table() for summaries by group, Plot(), points(), par() for basic graphics, qqnorm() and qqline() for checking normality, summary() provides a quick overview of variables