Course: Statistics and Probability for Data Science (DS14270). Source deck: Samer El Kababji, SD01 — Statistical Thinking. Equations and teaching simulations below extend the deck’s concepts; they are not additional observations from the deck. Each file runs independently using base R, with knitr/R Markdown needed only for rendering.
Rows are observations; columns are variables
For \(n\) observations and \(p\) numeric variables, a data matrix has entries
\[
X=(x_{ij})\in\mathbb{R}^{n\times p},\qquad
x_{ij}=\text{value of variable }j\text{ for observation }i.
\]
Worked example — reading a matrix entry. With the three numeric columns used below, there are \(n=4\) rows and \(p=3\) columns, so \(X\) is a \(4\times3\) matrix. The entry \(x_{21}=25000\) is the loan amount in the second displayed record; \(x_{22}=9.43\) is that record’s interest rate.
An R data frame can also contain categorical columns. The next cell transcribes the four loan records visible in the deck (original row labels 1, 2, 3, 50). These are an excerpt, not the complete loan50 dataset or a random sample.
loan_amount interest_rate term grade state total_income homeownership
1 7500 7.34 36 A MD 70000 rent
2 25000 9.43 60 B OH 254000 mortgage
3 14500 6.08 36 A MO 80000 mortgage
50 3000 7.96 36 A CA 34000 rent
Code
loans["2", ] # one observational unit
loan_amount interest_rate term grade state total_income homeownership
2 25000 9.43 60 B OH 254000 mortgage
Code
loans$term # one variable
[1] 36 60 36 36
Code
X <-as.matrix(loans[c("loan_amount", "interest_rate", "total_income")])dim(X)
[1] 4 3
Code
X[2, 1] # x_21: second displayed record, first variable
[1] 25000
Do not convert the entire mixed-type data frame to a numeric matrix: factor codes would not represent meaningful amounts or distances.
Numeric, nominal, and ordinal variables
A discrete variable takes values in a countable set, such as a whole number of months. A continuous measurement is modeled on an interval of real numbers, even when recorded with rounding. Nominal categories have no inherent order; ordinal categories have an order without necessarily equal spacing.
variable R_class statistical_type
loan_amount loan_amount numeric numerical (amount)
interest_rate interest_rate numeric numerical (rate)
term term integer numerical discrete
grade grade ordered/factor categorical ordinal
state state factor categorical nominal
total_income total_income numeric numerical (amount)
homeownership homeownership factor categorical nominal
Code
is.ordered(loans$grade)
[1] TRUE
Code
levels(loans$homeownership)
[1] "mortgage" "rent"
Summarize a column according to its type
For numeric values, the sample mean is \(\bar x=n^{-1}\sum_i x_i\). For category \(c\), its relative frequency is \(\widehat p_c=n^{-1}\sum_i\mathbf{1}(x_i=c)\).
Worked examples using the four displayed records. For loan amount, the summation means adding the four amounts and dividing by four:
Worked example — one small set of paired values. Suppose \(x=(1,2,3)\) and \(y=(2,4,3)\), so \(\bar x=2\) and \(\bar y=3\). The deviations are \((-1,0,1)\) and \((-1,1,0)\). Expanding each sum gives
The numerator adds paired deviation products; the denominator uses the sum of squared deviations for each variable.
The deck’s sunscreen and ice cream example can arise from a shared cause: temperature. The following fictional sales include small day-to-day changes \(u\) and \(v\) in addition to the temperature effect:
\[
X=20+3T+u,\qquad Y=10+4T+v.
\]
Worked example — substitute one day’s values. For \(T=20\), \(u=2\), and \(v=-4\):
\[
X=20+3(20)+2=82,\qquad Y=10+4(20)-4=86.
\]
A warmer day raises both predicted sales through \(T\), even though neither formula contains the other sales variable.
Neither sales equation uses the other product’s sales.
A strong correlation alone cannot tell us whether changing sunscreen sales would change ice cream sales. Also, zero correlation does not generally imply independence; correlation describes linear association.