Introductory Statistics with R: Simple Inferences for continuous data

Similar documents
Introductory Statistics with R: Linear models for continuous response (Chapters 6, 7, and 11)

Power and the computation of sample size

Power and sample size calculations

f Simulation Example: Simulating p-values of Two Sample Variance Test. Name: Example June 26, 2011 Math Treibergs

Statistical inference (estimation, hypothesis tests, confidence intervals) Oct 2018

STAT 135 Lab 5 Bootstrapping and Hypothesis Testing

Statistics in Stata Introduction to Stata

Hypothesis Testing. Gordon Erlebacher. Thursday, February 14, 13

probability George Nicholson and Chris Holmes 29th October 2008

Power and sample size calculations

STAT 513 fa 2018 Lec 02

Stat 135, Fall 2006 A. Adhikari HOMEWORK 6 SOLUTIONS

Confidence Intervals with σ unknown

Statistical Inference

Package jmuoutlier. February 17, 2017

Composite Hypotheses. Topic Partitioning the Parameter Space The Power Function

On Assumptions. On Assumptions

Stat 427/527: Advanced Data Analysis I

STAT 513 fa 2018 Lec 05

Lecture 2: Basic Concepts and Simple Comparative Experiments Montgomery: Chapter 2

Outline. Unit 3: Inferential Statistics for Continuous Data. Outline. Inferential statistics for continuous data. Inferential statistics Preliminaries

Chapter 7: Statistical Inference (Two Samples)

Business Statistics. Lecture 10: Course Review

Rank-Based Methods. Lukas Meier

probability George Nicholson and Chris Holmes 31st October 2008

z and t tests for the mean of a normal distribution Confidence intervals for the mean Binomial tests

Power and nonparametric methods Basic statistics for experimental researchersrs 2017

Chapter 7. Inference for Distributions. Introduction to the Practice of STATISTICS SEVENTH. Moore / McCabe / Craig. Lecture Presentation Slides

Biostatistics Quantitative Data

Outline The Rank-Sum Test Procedure Paired Data Comparing Two Variances Lab 8: Hypothesis Testing with R. Week 13 Comparing Two Populations, Part II

First steps of multivariate data analysis

Estimating a population mean

Matematisk statistik allmän kurs, MASA01:A, HT-15 Laborationer

The Welch-Satterthwaite approximation

Blue Not Blue

Contents 1. Contents

Chapter 7 Comparison of two independent samples

CHAPTER 17 CHI-SQUARE AND OTHER NONPARAMETRIC TESTS FROM: PAGANO, R. R. (2007)

Chapter 23: Inferences About Means

Comparing Means from Two-Sample

1 Introduction to Minitab

Data are sometimes not compatible with the assumptions of parametric statistical tests (i.e. t-test, regression, ANOVA)

(Re)introduction to Statistics Dan Lizotte

R Short Course Session 4

Package bpp. December 13, 2016

6 Single Sample Methods for a Location Parameter

Assignments. Statistics Workshop 1: Introduction to R. Tuesday May 26, Atoms, Vectors and Matrices

+ Specify 1 tail / 2 tail

Statistical Computing Session 4: Random Simulation

Review of Statistics 101

Analysis of Variance (ANOVA) Cancer Research UK 10 th of May 2018 D.-L. Couturier / R. Nicholls / M. Fernandes

Elementary Statistics

Multiple linear regression

Sociology 6Z03 Review II

Power Analysis. Introduction to Power

y ˆ i = ˆ " T u i ( i th fitted value or i th fit)

Lecture 3: Inference in SLR

One sample problem. sample mean: ȳ = . sample variance: s 2 = sample standard deviation: s = s 2. y i n. i=1. i=1 (y i ȳ) 2 n 1

Nonparametric tests, Bootstrapping

Review for Final. Chapter 1 Type of studies: anecdotal, observational, experimental Random sampling

Using R in 200D Luke Sonnet

Statistical Inference: Estimation and Confidence Intervals Hypothesis Testing

Math 475. Jimin Ding. August 29, Department of Mathematics Washington University in St. Louis jmding/math475/index.

Lecture 6 Exploratory data analysis Point and interval estimation

Chapter 3: The Normal Distributions

Analytical Graphing. lets start with the best graph ever made

Inference for Distributions Inference for the Mean of a Population

Data Analysis and Statistical Methods Statistics 651

Chapter 3. Measuring data

Glossary. The ISI glossary of statistical terms provides definitions in a number of different languages:

Solution: anti-fungal treatment exercise

Analysis of variance (ANOVA) Comparing the means of more than two groups

Independent Samples t tests. Background for Independent Samples t test

The Normal Distribution

Ch 7 : One-Sample Test of Hypothesis

Turning a research question into a statistical question.

STP 420 INTRODUCTION TO APPLIED STATISTICS NOTES

Non-parametric Inference and Resampling

One-Way ANOVA. Some examples of when ANOVA would be appropriate include:

1 Introduction to One-way ANOVA

The Chi-Square and F Distributions

Inference for Single Proportions and Means T.Scofield

Week 7.1--IES 612-STA STA doc

Expectation, Variance and Standard Deviation for Continuous Random Variables Class 6, Jeremy Orloff and Jonathan Bloom

Solutions exercises of Chapter 7

Math 141. Lecture 16: More than one group. Albyn Jones 1. jones/courses/ Library 304. Albyn Jones Math 141

16.400/453J Human Factors Engineering. Design of Experiments II

Descriptive Statistics-I. Dr Mahmoud Alhussami

Gov 2000: 6. Hypothesis Testing

Power. Week 8: Lecture 1 STAT: / 48

Regression and correlation

3. Shrink the vector you just created by removing the first element. One could also use the [] operators with a negative index to remove an element.

1 Probability Distributions

Exam 2 (KEY) July 20, 2009

Robustness and Distribution Assumptions

2.1: Inferences about β 1

ph: 5.2, 5.6, 5.8, 6.4, 6.5, 6.8, 6.9, 7.2, 7.5 sample mean = sample sd = sample size, n = 9

MSc / PhD Course Advanced Biostatistics. dr. P. Nazarov

MODULE 9 NORMAL DISTRIBUTION

4.1. Introduction: Comparing Means

Transcription:

Introductory Statistics with R: Simple Inferences for continuous data Statistical Packages STAT 1301 / 2300, Fall 2014 Sungkyu Jung Department of Statistics University of Pittsburgh E-mail: sungkyu@pitt.edu http://www.stat.pitt.edu/sungkyu/stat1301/ 1 / 28

1 Data Exploration and Graphics 2 One- and Two-Sample Tests 3 Power and the computation of sample size 2 / 28

Section 1 Data Exploration and Graphics 3 / 28

Summary Statistics Data analysis ALWAYS begins with an inspection of the data entry (e.g., using head(dataset.name) and str(dataset.name)) To calculate the mean, standard deviation, variance, median and quantiles. > x <- rnorm (50) > mean (x) [1] 0.03301363 > sd( x) [1] 1.069454 > var (x) [1] 1.143731 > median (x) [1] -0.08682795 > quantile ( x) 0% 25% 50% 75% 100% -2.60741896-0.54495849-0.08682795 0.70018536 2.98872414 Exercise: Obtain all deciles and percentiles. 4 / 28

Summary Statistics use na.rm argument (if not available, then remove) for missing values > attach ( juul ) > mean ( igf1 ) [1] NA > mean (igf1,na.rm=t) [1] 340.168 summary function: to obtain summaries of any objects > summary(igf1) Min. 1st Qu. Median Mean 3rd Qu. Max. NA s 25.0 202.2 313.5 340.2 462.8 915.0 321 > summary(juul) age menarche sex igf1 Min. : 0.170 Min. :1.000 Min. :1.000 Min. : 25.0 1st Qu.: 9.053 1st Qu.:1.000 1st Qu.:1.000 1st Qu.:202.2 Median :12.560 Median :1.000 Median :2.000 Median :313.5 Mean :15.095 Mean :1.476 Mean :1.534 Mean :340.2 3rd Qu.:16.855 3rd Qu.:2.000 3rd Qu.:2.000 3rd Qu.:462.8 Max. :83.000 Max. :2.000 Max. :2.000 Max. :915.0 NA s :5 NA s :635 NA s :5 NA s :321 tanner testvol Min. :1.00 Min. : 1.000 1st Qu.:1.00 1st Qu.: 1.000 Median :2.00 Median : 3.000 Mean :2.64 Mean : 7.896 3rd Qu.:5.00 3rd Qu.:15.000 Max. :5.00 Max. :30.000 5 / 28

Summary Statistics summary function for factor data type The data set has menarche, sex, and tanner coded as numeric variables even though they are clearly categorical. This can be mended as follows: > detach(juul) > juul$sex <- factor(juul$sex,labels=c("m","f")) > juul$menarche <- factor(juul$menarche,labels=c("no","yes")) > juul$tanner <- factor(juul$tanner, + labels=c("i","ii","iii","iv","v")) > attach(juul) > summary(juul) age menarche sex igf1 tanner Min. : 0.170 No :369 M :621 Min. : 25.0 I :515 1st Qu.: 9.053 Yes :335 F :713 1st Qu.:202.2 II :103 Median :12.560 NA s:635 NA s: 5 Median :313.5 III : 72 Mean :15.095 Mean :340.2 IV : 81 3rd Qu.:16.855 3rd Qu.:462.8 V :328 Max. :83.000 Max. :915.0 NA s:240 NA s :5 NA s :321 Exercise: Use the transform function (or within) to convert menarche, sex, and tanner variables in the juul data set as factors. 6 / 28

Graphical display of distributions Histograms: hist function > hist ( numeric. vector ) > hist ( numeric. vector,...) option breaks = n, you get approximately n cells (bars) option breaks = vector, vector giving the breakpoints between histogram cells option freq = TRUE, you get frequencies (counts), freq = FALSE, you get densities (count/n) 7 / 28

Graphical display of distributions Smoothed Histograms: density function Smoothed Histgram is an estimate of the true density function > xd = density ( juul $igf1,na.rm = T) > str (xd) List of 7 $ x : num [1:512] -90.6-88.4-86.2-84 -81.8... $ y : num [1:512] 4.84e -07 5.84e -07 7.03e -07 8.43e -07 1.01 $ bw : num 38.5 $ n : int 1018 $ call : language density. default ( x = juul $igf1, na. rm = T) $ data. name : chr " juul $ igf1 " $ has. na : logi FALSE - attr (*, " class ")= chr " density " > plot ( xd$x, xd$y) 8 / 28

Comparing Distributions Quantile-Quantile plot: To directly compare data dist n with theoretical normal dist n > qqnorm(numeric.vector) Exercise: Compare 100 random normal (or exponential) numbers with the normal distribution. Box plots: Useful for side-by-side comparison > par(mfrow=c(1,2)) > boxplot(juul$igf1) > boxplot(log(juul$igf1)) > par(mfrow=c(1,1)) > boxplot(iris[,1:4]) 9 / 28

Summary Statistics by Groups Grouped Data Numeric data grouped (categorized) by levels of factors Typical for data aimed for two-sample t-test and ANOVA > str ( red. cell. folate ) data. frame : 22 obs. of 2 variables : $ folate : num 243 251 275 291 347 354 380 392 206 210... $ ventilation : Factor w/ 3 levels " N2O +O2,24 h"," N2O +O2, op",..: 1 tapply function Work with numeric (or integer) data type as a grouping variable > attach ( red. cell. folate ); attach ( juul ) > tapply ( folate, ventilation, mean ) N2O +O2,24 h N2O +O2,op O2,24 h 316.6250 256.4444 278.0000 > tapply ( age, tanner, mean, na. rm=t) 1 2 3 4 5 8.195456 12.316019 12.989306 14.746667 17.878872 10 / 28

Summary Statistics by Groups aggregate function Splits the data into subsets, computes summary statistics for each, and returns the result in a convenient form. > aggregate ( object, by = list. object, FUN,...) > aggregate ( formula, data = data. frame, FUN,...) object: a vector or data frame list.object: a list or data frame of grouping elements FUN: mean, median, etc. formula: R formula, see example below. > aggregate (juul, list ( Sex = sex, Tanner = tanner ), mean, na. rm = T) > aggregate ( igf1 ~ sex + tanner, data = juul, mean, na. rm = T) 11 / 28

Summary Statistics by Groups by function: Apply a Function to a Data Frame Split by Factors > by( data. frame, INDICES, FUN,...) INDICES: factor indices (need not to be factor data type) FUN: function name, which can be applied to the data frame Examples: by(juul, as. factor ( tanner ), summary ) by( juul, tanner, summary ) by(juul, juul [" tanner "], summary ) 12 / 28

Summary Graphics by Groups Top-Bottom Histograms > attach ( energy ) > expend. lean <- expend [ stature ==" lean "] > expend. obese <- expend [ stature ==" obese "] > par ( mfrow =c (2,1)) > hist ( expend.lean, breaks =10, xlim =c (5,13), ylim =c(0,4), col =" white ") > hist ( expend. obese, breaks =10, xlim =c (5,13), ylim =c(0,4), col =" grey ") > par ( mfrow =c (1,1)) Parallel Boxplot > boxplot ( expend.lean, expend. obese ) > boxplot ( expend ~ stature, data = energy ) > boxplot ( expend ~ stature ) 13 / 28

Section 2 One- and Two-Sample Tests 14 / 28

One Sample Location Test One Sample T-Test: t.test function Assume X 1,..., X n i.i.d. N(µ, σ 2 ), > daily. intake <- c (5260,5470,5640,6180,6390,6515, + 6805,7515,7515,8230,8770) > t. test ( daily. intake ) > t. test ( daily. intake,mu =7725) > t. test ( daily. intake,mu =7725, alt = "l") The t tests are fairly robust against departures from the normal distribution especially in larger samples. For small sample size, assuming only that the distribution is symmetric around µ, Wilcoxon signed-rank test: wilcox.test function > wilcox. test ( daily. intake, mu =7725) > wilcox. test ( daily. intake, mu =7725, alt = "g") 15 / 28

Two-Sample Location Test Two-Sample t test: t.test, var.test functions For comparison of two vectors t. test ( expend.lean, expend. obese, var. equal = TRUE ) t. test ( expend.lean, expend. obese, var. equal = FALSE ) t. test ( expend.lean, expend. obese, var. equal = FALSE, alt = "g") var. test ( expend.lean, expend. obese ) Using the model formula t. test ( expend ~ stature, var. equal = T) var. test ( expend ~ stature ) Wilcoxon rank sum test: wilcox.test function wilcox. test ( expend.lean, expend. obese ) wilcox. test ( expend ~ stature ) 16 / 28

Paired difference test Paired tests are used when there are two measurements on the same experimental unit. The theory is essentially based on taking differences and thus reducing the problem to that of a one-sample test. The paired t test attach ( intake ) pre - post t. test (pre,post, paired = T) t. test (pre,post,mu = 1100, paired = T) t. test (pre,post,mu = 1100, paired = T, alternative = "g") t. test (pre -post, mu = 1100, alternative = "g") The matched-pairs Wilcoxon test wilcox. test (pre,post, paired = T) wilcox. test (pre,post, mu = 1100, paired = T) wilcox. test (pre,post, mu = 1100, paired = T, alternative = "g") 17 / 28

Saving the result of analysis > ttestresult <- t. test ( pre, post, paired = T) > ttestresult Paired t- test data : pre and post t = 11.9414, df = 10, p- value = 3.059e -07 alternative hypothesis : true difference in means is not equal to 0 95 percent confidence interval : 1074.072 1566.838 sample estimates : mean of the differences 1320.455 > ttestresult $ statistic t 11.94139 > ttestresult $p. value [1] 3.059021 e -07 18 / 28

Section 3 Power and the computation of sample size 19 / 28

The principles of power calculations When designing experiments, the experimenter should try to ensure that a sufficient amount of data are collected to be reasonably sure that a difference of a specified size will be detected. A simple example: Z-test Test H 0 : µ = µ 0 based on the sample X 1,..., X n N(µ, 1), σ = 1 known. With alternative H 1 : µ > µ 0, a sensible test is the Z-test, where H 0 is rejected if n( X µ 0) > z 1 α Then Prob. of Type I error: P µ0 ( n( X µ 0) > z 1 α) = α Power at µ > µ 0 (π(µ)): Probability that the test detects the difference when the true difference is µ π(µ) = P µ( n( X µ 0) > z 1 α) = P(Z > z 1 α n(µ µ 0)), Z N(0, 1) = 1 Φ(z 1 α n(µ µ 0)) Notice that the power depends on µ µ 0 (effect size), n (sample size) and α (significance level). 20 / 28

The principles of power calculations For fixed sample size n = 15, the power π(µ) as a function of µ > µ 0 = 0 can be evaluated as follows: n = 15 mu0 = 0 alpha = 0.05 mu = seq (0,1.5, by = 0.1) power.z = (1 - pnorm ( qnorm (1 - alpha ) -sqrt (n)*(mu - mu0 ))) plot (mu, power.z, type = "l") 21 / 28

The principles of power calculations Power as a function of sample size For a fixed (desired) effect size µ, power is also a function the sample size n. mu = 0.5 mu0 = 0 alpha = 0.05 n = 1:50 power.z = (1 - pnorm ( qnorm (1 - alpha ) -sqrt (n)*(mu - mu0 ))) plot (n, power.z, type = " l", ylim = c (0,1)) 22 / 28

The principles of power calculations Sample size computation For a fixed (desired) effect size µ µ 0, how large n is needed to ensure that the power is greater than (say) 0.9? π(µ) = 1 Φ(z 1 α n(µ µ 0)) > 0.9 Φ(z 1 α n(µ µ 0)) < 0.1 z 1 α n(µ µ 0) < Φ 1 (0.1) = z 0.1 ( ) 2 z1 α z 0.1 n > µ 1 µ 0 > mu = 0.5 > mu0 = 0 > alpha = 0.05 > nn <-(( - qnorm (1-0.9)+ qnorm (1 - alpha ))/(mu - mu0 ))^2 > ceiling ( nn) [1] 35 23 / 28

Power and Sample Size Calculation for t test One sample t test Test H 0 : µ = µ 0 based on the sample X 1,..., X n N(µ, σ 2 ), σ unknown. With alternative H 1 : µ > µ 0, the one-sample t test rejects H 0 if Then X µ 0 s/ n > tn 1,1 α Power at µ > µ 0 (π(δ)): Probability that the test detects the positive effect size δ = µ mu 0. ( ) X µ0 π(δ) = P δ s/ n > tn 1,1 α ( ) ( X µ) + δ = P s/ > t n 1,1 α, n where T = ( X µ)+δ s/ n has the non-central t distribution with d.f. n 1 and noncentrality parameter ν = nδ/σ. Notice that the power depends on δ (effect size), σ (population s.d.), n (sample size) and α (significance level). 24 / 28

Power and Sample Size Calculation for t test For fixed sample size n = 15, the power π(δ) as a function of δ > 0 can be evaluated as follows: n = 15 mu0 = 0 alpha = 0.05 mu = seq (0,1, by = 0.05) sigma = 1; t. ncp <- sqrt (n)*(mu - mu0 )/ sigma power.t = 1- pt( qt (1 - alpha,n -1), n -1,ncp = t. ncp ) plot (mu, power.t, type = "l", ylim = c (0,1)) 25 / 28

Power calculation for one- and two-sample t test power.t.test function power. t. test ( n = NULL, delta = NULL, sd = 1, sig. level = 0.05, power = NULL, type = c(" two. sample ", " one. sample ", " paired "), alternative = c(" two. sided ", " one. sided ")) Exactly one of the parameters n, delta, power, sd and sig.level must be passed as NULL, and that parameter is determined from the others. Example: power. t. test ( n = 15, delta = 1, sd = 1, type = " one. sample ", alternative = " one. sided ") power. t. test ( power = 0.9, delta = 1, sd = 1, type = " one. sample ", alternative = " two. sided ") power. t. test ( power = 0.9, delta = 1, sd = 3, type = " paired ", alternative = " two. sided ") 26 / 28

Exercise 1 Use power.t.test function to reproduce the plot in page 26. 2 Use power.t.test function to draw the graph of one-sample t-test power as a function of sample size. 3 This example is from Altman (1991, p. 457) and concerns the influence of milk on growth. Two groups are to be given different diets, and their growth will be measured. We wish to compute the sample size that, with a power of 90%, a two-sided test at the 1% level can find a difference of 0.5 cm in a distribution with a standard deviation of 2 cm. 4 Under which assumptions is the use of power.t.test function valid? 27 / 28

Remark on Power calculation Basic R distribution offers two more power calculation functions. Find out by referring to their help pages. >? power. prop. test >? power. anova. test Power and sample size calculation in general cases may be handled by using independent packages (such as pwr package), or by using other specialized software (such as PASS Software, NCSS, LLC.) 28 / 28