Knowledge / Statistics / Statistical computing with R

Statistical computing with R

Statistics (B.Sc) · Computing · free study notes for exams, placements and interviews

In short

R is a free, open-source language and environment built specifically for statistical computing and graphics, distributed through CRAN with more than 20,000 contributed packages. Everything in R is an object and almost every operation is vectorised, so a whole column is processed in one expression instead of a loop. Indian B.Sc/M.Sc Statistics syllabuses (UGC CBCS) examine R in a compulsory practical paper — descriptive statistics, plots, distributions, testing of hypotheses, regression and ANOVA done on the console with output interpreted. It is used in pharma CROs and clinical trials, market research, credit risk and analytics teams at Indian banks, ICMR/NIE research, MoSPI and NSS survey analysis, and is a standard screening topic in data-analyst interviews.

Explained

The environment, objects and modes

Data structures (the standard exam list)

Reading data, control flow and functions

Descriptive statistics and graphics

Probability distributions and simulation

Inference, regression and models

Why R, and how it compares

Key points

Formulas and examples

Sample mean: x̄ = Σxᵢ / n — R: mean(x)
Sample variance: s² = Σ(xᵢ − x̄)² / (n − 1); s = √s² — R: var(x), sd(x)
Coefficient of variation: CV = (s / x̄) × 100 % — R: 100*sd(x)/mean(x)
One-sample t: t = (x̄ − μ₀) / (s/√n), df = n − 1 — R: t.test(x, mu = mu0)
Two-sample pooled t: t = (x̄₁ − x̄₂) / √(s_p²(1/n₁ + 1/n₂)), s_p² = ((n₁−1)s₁² + (n₂−1)s₂²)/(n₁+n₂−2), df = n₁+n₂−2
Chi-square: χ² = Σ (O − E)² / E; df = k − 1 (goodness of fit) or (r−1)(c−1) (independence)
Regression slope: b = Sxy / Sxx, intercept a = ȳ − b x̄, where Sxy = Σ(x−x̄)(y−ȳ), Sxx = Σ(x−x̄)²
Correlation: r = Sxy / √(Sxx · Syy); R² = r² for simple linear regression
ANOVA F = MSB / MSW = (SSB/(k−1)) / (SSW/(N−k))

Symbols: n = sample size, x̄ = mean, s = standard deviation, O/E = observed/expected frequency, k = number of classes or groups, N = total observations. Units are those of the data (₹, kg, marks); CV, R² and p-values are dimensionless.

Example 1 — descriptive statistics by hand and in R. Marks: 12, 15, 18, 20, 25.

x <- c(12, 15, 18, 20, 25)
mean(x); var(x); sd(x); median(x); summary(x)
100 * sd(x) / mean(x)        # 27.49832

Example 2 — one-sample t-test on the same data, H₀: μ = 15 against H₁: μ ≠ 15, α = 0.05.

t.test(x, mu = 15)

Example 3 — simple linear regression. x = 1,2,3,4,5; y = 2,4,5,4,5.

fit <- lm(y ~ x); summary(fit); predict(fit, data.frame(x = 6))

Example 4 — chi-square goodness of fit (fair die, 60 throws). Observed 8, 10, 12, 9, 11, 10; expected 60/6 = 10 each.

chisq.test(c(8,10,12,9,11,10))

Example 5 — binomial probability and a check by simulation. n = 10, p = 0.3, find P(X ≤ 2).

set.seed(1)
mean(rbinom(1e6, 10, 0.3) <= 2)   # ≈ 0.3828, matches the exact value
TaskBase Rtidyverse
Read a CSVread.csv()readr::read_csv()
Keep rowsdf[df$age > 30, ]filter(df, age > 30)
New columndf$bmi <- w/h^2mutate(df, bmi = w/h^2)
Group meanstapply(y, g, mean)group_by(g) %>% summarise(mean(y))
Scatter plotplot(x, y)ggplot(df, aes(x,y)) + geom_point()

Where it is used

Interview questions

Common mistakes

Related topics

Python for data analysis: NumPy and pandas · Testing of hypotheses · Regression and correlation analysis · Design of experiments and ANOVA · Sampling theory and survey methods · Probability distributions

Open in the Knowledge reader →Find jobs →