← R EnglishChapter 10 of 13

Basic Statistics

## Learning Objectives - Calculate descriptive statistics - Perform correlation and regression - Understand hypothesis testing basics - Conduct t-tests and ANOVA ## Descriptive Statistics ### Central Tendency ```r x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) # Mean - average mean(x) # 5.5 # Median - middle value median(x) # 5.5 # For odd number of values y <- c(1, 2, 3, 4, 5) median(y) # 3 ``` ### Spread/Dispersion ```r x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) # Variance var(x) # 9.166667 # Standard deviation sd(x) # 3.02765 # Range range(x) # 1 10 max(x) - min(x) # 9 # Interquartile range quantile(x, 0.75) - quantile(x, 0.25) # 5 # Variance and SD for population var(x) * (length(x) - 1) / length(x) # 8.25 (population var) ``` ### Quantiles ```r x <- 1:100 # Quartiles quantile(x) # 0% 25% 50% 75% 100% # 1 26 50 75 100 quantile(x, probs = c(0.1, 0.9)) # 10th and 90th percentiles # Median (50th percentile) median(x) # 50.5 quantile(x, 0.5) # 50.5 ``` ### Summary ```r # Quick summary summary(x) # Min/Max min(x) # 1 max(x) # 10 # Sum sum(x) # 5050 ``` ## Frequency Analysis ### Tables ```r # Simple frequency x <- c("A", "B", "A", "C", "B", "A") table(x) # x # A B C # 3 2 1 # Proportions prop.table(table(x)) # x # A B C # 0.5 0.333333 0.166667 ``` ### Cross-tabulation ```r df <- data.frame( gender = c("M", "F", "M", "F", "M"), response = c("Yes", "No", "Yes", "Yes", "No") ) table(df$gender, df$response) # No Yes # F 1 1 # M 1 2 # Using table() with(df, table(gender, response)) # Proportions prop.table(table(df$gender, df$response), margin = 1) # row proportions prop.table(table(df$gender, df$response), margin = 2) # column proportions ``` ## Correlation ### Pearson Correlation ```r # Two variables x <- c(1, 2, 3, 4, 5) y <- c(2, 4, 3, 5, 6) cor(x, y) # 0.949 cor.test(x, y) # On dataframe df <- data.frame(x = 1:10, y = 2 * (1:10) + rnorm(10)) cor(df) ``` ### Correlation Methods ```r x <- c(1, 2, 3, 4, 5) y <- c(5, 4, 3, 2, 1) # Pearson (linear) cor(x, y) # -1 # Spearman (rank) cor(x, y, method = "spearman") # -1 # Kendall (rank) cor(x, y, method = "kendall") # -1 ``` ### Correlation Matrix ```r df <- mtcars[, c("mpg", "disp", "hp", "wt")] cor(df) ``` ## Linear Regression ### Simple Linear Regression ```r # Create data df <- data.frame( x = c(1, 2, 3, 4, 5), y = c(2.1, 4.3, 5.8, 8.2, 10.5) ) # Fit model model <- lm(y ~ x, data = df) # Summary summary(model) # Coefficients coef(model) # (Intercept) x # 0.26 2.04 # Predicted values fitted(model) # Residuals residuals(model) ``` ### Multiple Linear Regression ```r df <- mtcars[, c("mpg", "disp", "hp", "wt")] model <- lm(mpg ~ disp + hp + wt, data = df) summary(model) ``` ### Model Formulas ```r # y depends on x lm(y ~ x, data = df) # y depends on x1 and x2 lm(y ~ x1 + x2, data = df) # y depends on x and x squared lm(y ~ x + I(x^2), data = df) # y depends on all other variables lm(y ~ ., data = df) # Interaction lm(y ~ x1 * x2, data = df) # No intercept lm(y ~ -1 + x, data = df) ``` ### Prediction ```r # New data new_data <- data.frame(x = c(6, 7, 8)) # Predict predict(model, newdata = new_data) ``` ## Hypothesis Testing ### One-Sample t-test ```r # One-sample t-test (test if mean equals mu) x <- c(98, 102, 95, 100, 99, 101, 97, 103, 100, 99) # Test if mean = 100 t.test(x, mu = 100) ``` ### Two-Sample t-test ```r # Two independent groups group1 <- c(85, 90, 92, 88, 91) group2 <- c(78, 82, 85, 79, 84) # Two-sample t-test t.test(group1, group2) # Paired t-test (same subjects) before <- c(100, 102, 98, 105, 101) after <- c(95, 98, 92, 99, 96) t.test(before, after, paired = TRUE) ``` ### ANOVA ```r # One-way ANOVA df <- data.frame( group = rep(c("A", "B", "C"), each = 5), value = c(10, 12, 11, 13, 12, 20, 22, 21, 19, 23, 30, 28, 31, 29, 32) ) anova_result <- aov(value ~ group, data = df) summary(anova_result) ``` ### Chi-Square Test ```r # Observed frequencies observed <- matrix(c(30, 20, 25, 25), nrow = 2) # Chi-square test chisq.test(observed) # Expected vs observed chi_result <- chisq.test(observed) chi_result$expected # Expected frequencies chi_result$observed # Observed (same as input) chi_result$residuals # Pearson residuals ``` ## Confidence Intervals ### t-distribution CI ```r x <- c(98, 102, 95, 100, 99, 101, 97, 103, 100, 99) # 95% confidence interval t.test(x)$conf.int # [1] 97.158 102.042 # attr(,"conf.level") # [1] 0.95 # 99% CI t.test(x, conf.level = 0.99)$conf.int ``` ### Bootstrap CI ```r # Bootstrap confidence interval library(boot) # Boot function boot_mean <- function(data, indices) { mean(data[indices]) } # Bootstrap boot_result <- boot(x, boot_mean, R = 1000) boot.ci(boot_result, type = "perc") ``` ## Normality Tests ### Shapiro-Wilk Test ```r x <- rnorm(100, mean = 5, sd = 2) # Test normality shapiro.test(x) # p-value > 0.05 means normal ``` ### Visual Assessment ```r # Q-Q plot qqnorm(x) qqline(x) # Histogram with normal curve hist(x, prob = TRUE) curve(dnorm(x, mean = mean(x), sd = sd(x)), add = TRUE) ``` ## Non-parametric Tests ### Mann-Whitney U Test ```r # Non-parametric alternative to t-test group1 <- c(85, 90, 92, 88, 91) group2 <- c(78, 82, 85, 79, 84) wilcox.test(group1, group2) ``` ### Kruskal-Wallis Test ```r # Non-parametric alternative to ANOVA df <- data.frame( group = rep(c("A", "B", "C"), each = 5), value = c(10, 12, 11, 13, 12, 20, 22, 21, 19, 23, 30, 28, 31, 29, 32) ) kruskal.test(value ~ group, data = df) ``` ## Statistical Functions Reference | Function | Purpose | |----------|---------| | mean() | Arithmetic mean | | median() | Median value | | sd() | Standard deviation | | var() | Variance | | cov() | Covariance | | cor() | Correlation | | cov2cor() | Correlation matrix | | lm() | Linear regression | | predict() | Predictions | | t.test() | T-test | | aov() | ANOVA | | chisq.test() | Chi-square test | | shapiro.test() | Normality test | | wilcox.test() | Mann-Whitney U | | kruskal.test() | Kruskal-Wallis | ## Statistics Summary - Descriptive stats: mean, median, sd, var, quantile, summary - Frequency analysis: table(), prop.table() - Correlation: cor() for linear relationships - Regression: lm() for linear models, predict() for new data - t-test: one-sample, two-sample, paired - ANOVA: aov() for comparing multiple groups - Chi-square: chisq.test() for categorical data - Non-parametric tests when data isn't normal - Always check assumptions before testing

Comments

Comments powered by Giscus

To enable comments, add your Giscus embed code here.

Learn more about Giscus →