Mangiafico, S.S. 2016. RHandbookProgramEvaluation
Mangiafico, S.S. 2016. RHandbookProgramEvaluation
OF EXTENSION
EDUCATION PROGRAM
EVALUATION IN R
SALVATORE S. MANGIAFICO
Rutgers Cooperative Extension
New Brunswick, NJ
VERSION 1.6.5
i
©2016 by Salvatore S. Mangiafico.
If you use the code or information in this site in a published work, please cite it as a
source. Also, if you are an instructor and use this book in your course, please let me know.
[email protected]
Mangiafico, S.S. 2016. Summary and Analysis of Extension Program Evaluation in R, version
1.6.5.
rcompanion.org/documents/RHandbookProgramEvaluation.pdf .
ii
List of Chapters
List of Chapters
List of Chapters.......................................................................................................................... iii
Table of Contents ...................................................................................................................... vi
Introduction ...............................................................................................................................1
Purpose of This Book.......................................................................................................................... 1
About the Author of this Book ............................................................................................................ 2
Using R .............................................................................................................................................. 2
Statistics Textbooks and Other Resources ........................................................................................... 9
Statistics for Educational Program Evaluation ........................................................................... 13
Why Statistics? ................................................................................................................................ 13
Evaluation Tools and Surveys ........................................................................................................... 14
Variables, Descriptive Statistics, and Plots ................................................................................ 20
Types of Variables ............................................................................................................................ 20
Descriptive Statistics ........................................................................................................................ 24
Confidence Intervals ........................................................................................................................ 56
Basic Plots ....................................................................................................................................... 67
Understanding Statistics and Hypothesis Testing .................................................................... 105
Hypothesis Testing and p-values......................................................................................................105
Reporting Results of Data and Analyses ...........................................................................................121
Choosing a Statistical Test ...............................................................................................................126
Independent and Paired Values .......................................................................................................134
Likert Data ............................................................................................................................. 140
Introduction to Likert Data ..............................................................................................................140
Descriptive Statistics for Likert Data ................................................................................................143
Descriptive Statistics with the likert Package ...................................................................................164
Confidence Intervals for Medians ....................................................................................................169
Converting Numeric Data to Categories ...........................................................................................177
Traditional Nonparametric Tests............................................................................................. 188
Introduction to Traditional Nonparametric Tests .............................................................................188
One-sample Wilcoxon Signed-rank Test ...........................................................................................190
Sign Test for One-sample Data ........................................................................................................195
iii
Two-sample Mann–Whitney U Test .................................................................................................197
Mood’s Median Test for Two-sample Data ......................................................................................203
Two-sample Paired Rank-sum Test ..................................................................................................205
Sign Test for Two-sample Paired Data..............................................................................................212
Kruskal–Wallis Test .........................................................................................................................215
Mood’s Median Test .......................................................................................................................226
Friedman Test .................................................................................................................................231
Quade Test .....................................................................................................................................242
Nonparametric Regression ..............................................................................................................250
Nonparametric Regression for Time Series ......................................................................................259
Permutation Tests for Ordinal Data ........................................................................................ 266
Introduction to Permutation Tests...................................................................................................266
One-way Permutation Test of Independence for Ordinal Data..........................................................270
One-way Permutation Test of Symmetry for Paired Ordinal Data .....................................................279
Concepts for Linear Models ..................................................................................................... 289
Introduction to Linear Models .........................................................................................................289
Using Random Effects in Models......................................................................................................292
What are Least Square Means? .......................................................................................................297
Least Square Means for Multiple Comparisons ................................................................................299
Factorial ANOVA: Main Effects, Interaction Effects, and Interaction Plots .........................................303
p-values and R-square Values for Models ........................................................................................315
Ordinal Tests with Cumulative Link Models ............................................................................. 328
Introduction to Cumulative Link Models (CLM) for Ordinal Data .......................................................328
Two-sample Ordinal Test with CLM .................................................................................................330
Two-sample Paired Ordinal Test with CLMM ...................................................................................334
One-way Ordinal ANOVA with CLM .................................................................................................339
One-way Repeated Ordinal ANOVA with CLMM ..............................................................................346
Two-way Ordinal ANOVA with CLM .................................................................................................355
Two-way Repeated Ordinal ANOVA with CLMM ..............................................................................371
Tests for Nominal Data ........................................................................................................... 385
Introduction to Tests for Nominal Variables .....................................................................................385
Confidence Intervals for Proportions ...............................................................................................390
Goodness-of-Fit Tests for Nominal Variables....................................................................................395
iv
Association Tests for Nominal Variables ..........................................................................................402
Tests for Paired Nominal Data .........................................................................................................410
Cochran–Mantel–Haenszel Test for 3-Dimensional Tables ................................................................420
Cochran’s Q Test for Paired Nominal Data .......................................................................................424
Association Tests for Ordinal Tables ................................................................................................434
Models for Nominal Data ................................................................................................................445
Parametric Tests..................................................................................................................... 463
Introduction to Parametric Tests .....................................................................................................463
One-sample t-test ...........................................................................................................................483
Two-sample t-test ...........................................................................................................................487
Paired t-test ....................................................................................................................................493
One-way ANOVA ............................................................................................................................499
One-way ANOVA with Blocks ..........................................................................................................509
One-way ANOVA with Random Blocks .............................................................................................518
Two-way ANOVA ............................................................................................................................527
Repeated Measures ANOVA ............................................................................................................538
Correlation and Linear Regression ...................................................................................................549
Advanced Parametric Methods .......................................................................................................567
Transforming Data ..........................................................................................................................588
Analysis of Count Data and Percentage Data .......................................................................... 607
Hermite and Poisson Regression for Count Data ..............................................................................607
Beta Regression for Percent and Proportion Data ............................................................................625
v
Table of Contents
Table of Contents
List of Chapters _________________________________________________________________ iii
Table of Contents _______________________________________________________________ vi
Introduction ____________________________________________________________________ 1
Purpose of This Book __________________________________________________________________ 1
Goals of this book ____________________________________________________________________________ 1
Specific learning goals _________________________________________________________________________ 1
Pre-requisites________________________________________________________________________________ 1
What this book will not cover ___________________________________________________________________ 1
Designing and conducting surveys _____________________________________________________________ 2
Advanced statistical analyses _________________________________________________________________ 2
About the Author of this Book __________________________________________________________ 2
Using R _____________________________________________________________________________ 2
R and RStudio________________________________________________________________________________ 2
Using the RStudio environment _________________________________________________________________ 3
Installation _______________________________________________________________________________ 3
Using the R Console environment ________________________________________________________________ 3
What if I don’t have my own computer? __________________________________________________________ 3
Portable installation on a usb drive ____________________________________________________________ 3
Using university computers __________________________________________________________________ 3
Using R online _____________________________________________________________________________ 3
apps.rutgers.edu___________________________________________________________________________ 3
Tests for package installation ___________________________________________________________________ 4
Required readings ____________________________________________________________________________ 5
About R __________________________________________________________________________________ 5
Obtaining R _______________________________________________________________________________ 6
A Few Notes to Get Started with R ____________________________________________________________ 6
Avoiding Pitfalls in R ________________________________________________________________________ 6
Help with R _______________________________________________________________________________ 6
R Tutorials ________________________________________________________________________________ 6
References for this chapter _____________________________________________________________________ 6
Exercises A __________________________________________________________________________________ 6
Statistics Textbooks and Other Resources _________________________________________________ 9
Some free general statistics books _______________________________________________________________ 9
Books on statistical analyses and tests ___________________________________________________________ 10
Analyses in R _______________________________________________________________________________ 10
Online resources __________________________________________________________________________ 10
Text books _______________________________________________________________________________ 10
Videos __________________________________________________________________________________ 10
Other useful resources for understanding statistics ________________________________________________ 11
Videos __________________________________________________________________________________ 11
Blogs ___________________________________________________________________________________ 11
Online Learning Modules and Massive Open Online Courses (MOOC’s) ________________________________ 11
vi
Statistics for Educational Program Evaluation ________________________________________ 13
Why Statistics? ______________________________________________________________________ 13
The importance of statistics in program evaluation ________________________________________________ 13
Applicability of statistical tests to other fields and situations _________________________________________ 14
Evaluation Tools and Surveys __________________________________________________________ 14
Extension programs __________________________________________________________________________ 14
Designing a questionnaire _____________________________________________________________________ 14
Some principles of questionnaire design for program evaluation ___________________________________ 15
Examples of evaluation tools ________________________________________________________________ 17
Optional Readings ___________________________________________________________________________ 18
Optional additional resources __________________________________________________________________ 19
References for this chapter ____________________________________________________________________ 19
Acknowledgements __________________________________________________________________________ 19
vii
Harmonic mean __________________________________________________________________________ 54
Exercises C _________________________________________________________________________________ 54
Confidence Intervals _________________________________________________________________ 56
Packages used in this chapter __________________________________________________________________ 56
Understanding confidence intervals _____________________________________________________________ 56
Populations and samples ___________________________________________________________________ 57
Interpretation of confidence intervals _________________________________________________________ 57
Confidence intervals as an alternative to some tests _____________________________________________ 57
Example for confidence intervals _______________________________________________________________ 58
Traditional confidence intervals for means _____________________________________________________ 59
Traditional confidence intervals for means for grouped data _______________ Error! Bookmark not defined.
Optional Analyses: confidence interval for the mean by bootstrapping ______________________________ 63
Required readings ___________________________________________________________________________ 64
Optional readings ___________________________________________________________________________ 64
References _________________________________________________________________________________ 64
Optional analyses ___________________________________________________________________________ 64
Confidence intervals for geometric mean ______________________________________________________ 64
Confidence intervals for geometric means for groups ____________________________________________ 65
Exercises D _________________________________________________________________________________ 66
Basic Plots__________________________________________________________________________ 67
Packages used in this chapter __________________________________________________________________ 67
The need to understand plots __________________________________________________________________ 67
Some advice on producing plots ________________________________________________________________ 67
Producing clear and informative plots _________________________________________________________ 68
Misleading plots __________________________________________________________________________ 68
Using software to produce plots _____________________________________________________________ 69
Exporting plots ___________________________________________________________________________ 69
Examples of basic plots for interval/ratio and ordinal data ___________________________________________ 69
Histogram _______________________________________________________________________________ 70
Box plots ________________________________________________________________________________ 74
Plot of means and interaction plots ___________________________________________________________ 78
Bar plot of means _________________________________________________________________________ 84
Scatter plot ______________________________________________________________________________ 88
Examples of basic plots for nominal data _________________________________________________________ 89
Bar plot for counts of a nominal variable ______________________________________________________ 90
Mosaic plot ______________________________________________________________________________ 92
Optional analyses: Describing histogram shapes ___________________________________________________ 93
Optional analyses: Misleading and disorienting plots _______________________________________________ 96
The need to include measures of variation _____________________________________________________ 96
For bar charts, start the y-axis at zero _________________________________________________________ 97
Avoid disorienting the audience _____________________________________________________________ 98
Use the best model_______________________________________________________________________ 100
References ________________________________________________________________________________ 102
Required readings __________________________________________________________________________ 102
Optional readings __________________________________________________________________________ 102
Exercises E ________________________________________________________________________________ 103
viii
The null and alternative hypotheses _________________________________________________________ 105
p-value definition ________________________________________________________________________ 106
Theory and practice of using p-values __________________________________________________________ 108
Wait, does this make any sense? ____________________________________________________________ 108
Statistics is like a jury? ____________________________________________________________________ 109
Errors in inference _______________________________________________________________________ 109
The 0.05 alpha value is not dogma __________________________________________________________ 110
Philosophy of statistical analyses ______________________________________________________________ 111
Statistics is not like a trial __________________________________________________________________ 111
Practical significance and statistical significance________________________________________________ 111
p-values and effect sizes __________________________________________________________________ 112
Preplanned tests _________________________________________________________________________ 113
Exploratory data analysis __________________________________________________________________ 114
p-value hacking __________________________________________________________________________ 114
A few of xkcd comics ________________________________________________________________________ 114
Significant ______________________________________________________________________________ 114
Null hypothesis __________________________________________________________________________ 114
P-values ________________________________________________________________________________ 115
Experiments, sampling, and causation __________________________________________________________ 115
Types of experimental designs ______________________________________________________________ 115
Quasi-experiment designs _________________________________________________________________ 115
Observational studies _____________________________________________________________________ 115
Sampling _______________________________________________________________________________ 115
Plan ahead and be consistent ______________________________________________________________ 116
Optional discussion: Alternative methods to the Null Hypothesis Significance Test ______________________ 116
The NHST controversy ____________________________________________________________________ 117
Alternatives to the NHST approach __________________________________________________________ 118
References and further reading _______________________________________________________________ 118
Exercises F ________________________________________________________________________________ 119
Reporting Results of Data and Analyses _________________________________________________ 121
Packages used in this chapter _________________________________________________________________ 122
Reporting analyses, results, and assumptions ____________________________________________________ 122
Procedures _____________________________________________________________________________ 122
Results _________________________________________________________________________________ 122
Notes on different data and analyses ________________________________________________________ 123
Advice on tables and plots ___________________________________________________________________ 123
Plots __________________________________________________________________________________ 123
Tables _________________________________________________________________________________ 123
Table headings and plot captions ___________________________________________________________ 124
Example description of statistical analysis and results _____________________________________________ 124
Procedures _____________________________________________________________________________ 124
Results _________________________________________________________________________________ 125
Optional technical note on effect sizes _______________________________________________________ 125
References ________________________________________________________________________________ 126
Choosing a Statistical Test ____________________________________________________________ 126
Plan your experimental design before you collect data ____________________________________________ 126
What is the hypothesis? _____________________________________________________________________ 127
What number and type of variables do you have? ________________________________________________ 127
References ________________________________________________________________________________ 134
Optional readings __________________________________________________________________________ 134
Independent and Paired Values _______________________________________________________ 134
ix
Packages used in this chapter _________________________________________________________________ 135
An example of paired and unpaired data ________________________________________________________ 135
Box plot and summary statistics by group _____________________________________________________ 136
Bar plot to show paired differences__________________________________________________________ 137
Paired t-test and unpaired test _____________________________________________________________ 138
Histogram of differences with normal curve ___________________________________________________ 138
x
Plots that treat Likert data like numeric data __________________________________________________ 168
References ________________________________________________________________________________ 169
Confidence Intervals for Medians ______________________________________________________ 169
Packages used in this chapter _________________________________________________________________ 169
Medians, quantiles, and confidence intervals for one-sample data ___________________________________ 170
Produce median with median, summary, and Summarize functions ________________________________ 171
Confidence interval for medians with the wilcox.test function ____________________________________ 172
Confidence interval for median by bootstrap __________________________________________________ 172
Median and confidence interval with the DescTools package _____________________________________ 173
groupwiseMedian function to produce medians and confidence intervals ___________________________ 173
Medians, quantiles, and confidence intervals for grouped data ______________________________________ 174
Summarize function in FSA package for grouped data ___________________________________________ 175
groupwiseMedian function to produce medians and confidence intervals for grouped data ____________ 175
References ________________________________________________________________________________ 176
Exercises H ________________________________________________________________________________ 176
Converting Numeric Data to Categories _________________________________________________ 177
Categorizing data by a range of values __________________________________________________________ 177
Categorizing data by percentiles _______________________________________________________________ 178
Categorizing data with clustering ______________________________________________________________ 178
Packages used in this chapter _________________________________________________________________ 178
Examples for converting numeric data to categories ______________________________________________ 178
Categorize data by range of values __________________________________________________________ 179
Categorize data by percentile ______________________________________________________________ 181
Categorize data by clustering _______________________________________________________________ 182
xi
Two-sample Mann–Whitney U test example _____________________________________________________ 199
Summarize data treating Likert scores as factors _______________________________________________ 200
Histograms of data by group _______________________________________________________________ 201
Summarize data treating Likert scores as numeric ______________________________________________ 201
Two-sample Mann–Whitney U test example __________________________________________________ 201
Exercises J ________________________________________________________________________________ 202
Mood’s Median Test for Two-sample Data ______________________________________________ 203
Packages used in this chapter _________________________________________________________________ 204
Example using the RVAideMemoire package _____________________________________________________ 204
Mood’s Median Test ________________________________________________________________________ 205
Two-sample Paired Rank-sum Test _____________________________________________________ 205
When to use this test _______________________________________________________________________ 205
Packages used in this chapter _________________________________________________________________ 206
Two-sample paired rank-sum test example ______________________________________________________ 206
Plot the paired data ______________________________________________________________________ 208
Two-sample paired sign-rank test ___________________________________________________________ 210
Exercises K ________________________________________________________________________________ 211
Sign Test for Two-sample Paired Data __________________________________________________ 212
Packages used in this chapter _________________________________________________________________ 212
Sign test for paired two-sample data example ___________________________________________________ 213
Two-sample sign test with BSDA package _____________________________________________________ 214
Two-sample sign test with DescTools package _________________________________________________ 214
Kruskal–Wallis Test _________________________________________________________________ 215
Packages used in this chapter _________________________________________________________________ 216
Kruskal–Wallis test example __________________________________________________________________ 216
Summarize data treating Likert scores as factors _______________________________________________ 218
Histograms by group _____________________________________________________________________ 218
Summarize data treating Likert scores as numeric ______________________________________________ 219
Kruskal–Wallis test example _______________________________________________________________ 219
Post-hoc test: Dunn test for multiple comparisons of groups _____________________________________ 220
Post-hoc test: pairwise Mann–Whitney U-tests for multiple comparisons ___________________________ 221
Plot of medians and confidence intervals _______________________________________________________ 223
Exercises L ________________________________________________________________________________ 224
Mood’s Median Test ________________________________________________________________ 226
Packages used in this chapter _________________________________________________________________ 227
Example using the RVAideMemoire package _____________________________________________________ 227
Mood’s median test ______________________________________________________________________ 228
Post-hoc test: pairwiseMedianTest function ___________________________________________________ 229
Post-hoc test: pairwiseMedianMatrix function _________________________________________________ 230
Friedman Test______________________________________________________________________ 231
Packages used in this chapter _________________________________________________________________ 233
Friedman test example ______________________________________________________________________ 233
Summarize data treating Likert scores as factors _______________________________________________ 235
Histograms by group _____________________________________________________________________ 235
Bar plots of differences between groups______________________________________________________ 236
pairwiseDifferences to produce bar plots of differences between all groups _________________________ 237
Summarize data treating Likert scores as numeric ______________________________________________ 238
Friedman test example ____________________________________________________________________ 239
Post-hoc test: pairwise sign test for multiple comparisons of groups _______________________________ 239
xii
Post-hoc Conover test ____________________________________________________________________ 241
Quade Test ________________________________________________________________________ 242
Packages used in this chapter _________________________________________________________________ 243
Quade test example ________________________________________________________________________ 244
Summarize data treating Likert scores as factors _______________________________________________ 245
Histograms by group _____________________________________________________________________ 246
Summarize data treating Likert scores as numeric ______________________________________________ 247
Quade test example ______________________________________________________________________ 248
Post-hoc test: pairwise two-sample paired rank-sum test for multiple comparisons ___________________ 248
Post-hoc test: Quade post-hoc test __________________________________________________________ 249
Nonparametric Regression ___________________________________________________________ 250
Packages used in this chapter _________________________________________________________________ 250
Nonparametric correlation ___________________________________________________________________ 251
Nonparametric regression examples ___________________________________________________________ 251
Kendall–Theil Sen Siegel nonparametric linear regression __________________________________________ 252
Plot with statistics________________________________________________________________________ 253
Quantile regression _________________________________________________________________________ 254
Plot with statistics________________________________________________________________________ 255
Local regression ____________________________________________________________________________ 256
Plot ___________________________________________________________________________________ 257
Generalized additive models__________________________________________________________________ 257
Plot with statistics________________________________________________________________________ 258
Nonparametric Regression for Time Series ______________________________________________ 259
Packages used in this chapter _________________________________________________________________ 260
Nonparametric regression examples ___________________________________________________________ 260
Decomposing time series objects ______________________________________________________________ 262
Mann–Kendall trend test ____________________________________________________________________ 263
Sen’s slope for time series data _______________________________________________________________ 264
Pettitt’s test for change in value _______________________________________________________________ 264
xiii
Comparison of methods for independence tests for ordinal data ____________________________________ 278
One-way Permutation Test of Symmetry for Paired Ordinal Data ____________________________ 279
When to use this test _______________________________________________________________________ 279
Packages used in this chapter _________________________________________________________________ 280
One-way ordinal permutation test of symmetry example __________________________________________ 280
Summarize data treating Likert scores as factors _______________________________________________ 282
Histograms by group _____________________________________________________________________ 282
Summarize data treating Likert scores as numeric ______________________________________________ 283
Permutation symmetry test ________________________________________________________________ 283
Post-hoc test: pairwise permutation tests ____________________________________________________ 284
Plot of medians and confidence intervals _____________________________________________________ 286
Comparison of methods for symmetry tests for ordinal data ________________________________________ 288
xiv
R-squared and pseudo R-squared ___________________________________________________________ 316
Packages used in this chapter _________________________________________________________________ 316
Example of model p-value, R-squared, and pseudo R-squared _______________________________________ 317
Linear model ____________________________________________________________________________ 319
Generalized linear model __________________________________________________________________ 320
Optional analyses: Confidence intervals for p-values and R-squared values ____________________________ 322
Linear model ____________________________________________________________________________ 323
Generalized linear model __________________________________________________________________ 325
References ________________________________________________________________________________ 328
xv
Two-way ordinal ANOVA example _____________________________________________________________ 357
Summarize data treating Likert scores as factors _______________________________________________ 358
Histograms by group _____________________________________________________________________ 359
Summarize data treating Likert scores as numeric ______________________________________________ 362
Produce interaction plot with medians and quantiles ___________________________________________ 363
Two-way ordinal ANOVA __________________________________________________________________ 364
Post-hoc tests with lsmeans for multiple comparisons of groups __________________________________ 365
Optional analyses __________________________________________________________________________ 368
Post-hoc test: pairwise ordinal tests for multiple comparisons of groups ____________________________ 368
Two-way Repeated Ordinal ANOVA with CLMM __________________________________________ 371
Packages used in this chapter _________________________________________________________________ 372
Two-way repeated ordinal ANOVA example _____________________________________________________ 373
Summarize data treating Likert scores as factors _______________________________________________ 374
Histograms by group _____________________________________________________________________ 375
Summarize data treating Likert scores as numeric ______________________________________________ 376
Plot Likert scores before and after___________________________________________________________ 377
Produce interaction plot with medians and confidence intervals __________________________________ 378
Two-way repeated ordinal ANOVA __________________________________________________________ 379
Post-hoc tests with lsmeans for multiple comparisons of groups __________________________________ 382
Interaction plot with group separation letters _________________________________________________ 383
xvi
Packages used in this chapter _________________________________________________________________ 404
Association tests for nominal variables example __________________________________________________ 404
Reading the data as a matrix _______________________________________________________________ 404
Fisher Exact test of association ________________________________________________________________ 404
Post-hoc analysis ________________________________________________________________________ 405
G-test of association ________________________________________________________________________ 407
Post-hoc analysis ________________________________________________________________________ 407
Chi-square test of association _________________________________________________________________ 408
Post-hoc analysis ________________________________________________________________________ 408
Optional readings __________________________________________________________________________ 409
References ________________________________________________________________________________ 409
Exercises M _______________________________________________________________________________ 409
Tests for Paired Nominal Data_________________________________________________________ 410
Packages used in this chapter _________________________________________________________________ 411
McNemar and McNemar–Bowker tests ______________________________________________________ 411
Exact tests ______________________________________________________________________________ 411
Example of tests for paired data nominal data ___________________________________________________ 412
Exact tests for symmetry __________________________________________________________________ 413
McNemar and McNemar–Bowker chi-square tests for symmetry __________________________________ 414
An example without repeated measures, comparing test of symmetry with test of association __________ 414
Optional analysis: a 4 x 4 example with several 0’s _____________________________________________ 415
Optional analyses: conducting exact tests for symmetry _________________________________________ 417
Exercises N ________________________________________________________________________________ 419
Cochran–Mantel–Haenszel Test for 3-Dimensional Tables __________________________________ 420
Packages used in this chapter _________________________________________________________________ 421
C–M–H test example: long-format with counts ___________________________________________________ 421
C–M–H test example: table format ____________________________________________________________ 424
Cochran’s Q Test for Paired Nominal Data _______________________________________________ 424
Packages used in this chapter _________________________________________________________________ 426
Cochran’s Q test example: long-format _________________________________________________________ 426
Cochran’s Q test example: short-format in matrix ________________________________________________ 432
Association Tests for Ordinal Tables ____________________________________________________ 434
Packages used in this chapter _________________________________________________________________ 434
Linear-by-linear test for ordered contingency tables ______________________________________________ 435
Example of linear-by-linear test 1 ___________________________________________________________ 435
Example of linear-by-linear test 2 ___________________________________________________________ 437
Extended Cochran–Armitage test ______________________________________________________________ 437
Example of extended Cochran–Armitage test 1 ________________________________________________ 438
Example of extended Cochran–Armitage test 2 ________________________________________________ 440
Long-form data and three-dimensional contingency tables _________________________________________ 441
Two-dimensional table example ____________________________________________________________ 443
Three-dimensional table example ___________________________________________________________ 443
References ________________________________________________________________________________ 444
Models for Nominal Data ____________________________________________________________ 445
Packages used in this chapter _________________________________________________________________ 445
Log-linear models for tests of association _______________________________________________________ 445
Log-linear model example _________________________________________________________________ 446
Logistic regression __________________________________________________________________________ 448
Logistic regression example ________________________________________________________________ 449
Multinomial logistic regression ________________________________________________________________ 453
xvii
VGAM package __________________________________________________________________________ 453
mlogit and nnet packages _________________________________________________________________ 453
Multinomial regression example ____________________________________________________________ 454
Mixed-effects logistic regression example _______________________________________________________ 457
Other tools for categorical analysis ____________________________________________________________ 461
xviii
Optional readings __________________________________________________________________________ 491
References ________________________________________________________________________________ 491
Exercises P ________________________________________________________________________________ 491
Paired t-test _______________________________________________________________________ 493
Packages used in this chapter _________________________________________________________________ 493
Paired t-test example _______________________________________________________________________ 493
Histogram of difference data _______________________________________________________________ 495
Plot the paired data ______________________________________________________________________ 495
Paired t-test ____________________________________________________________________________ 497
Optional readings __________________________________________________________________________ 498
References ________________________________________________________________________________ 498
Exercises Q ________________________________________________________________________________ 498
One-way ANOVA ___________________________________________________________________ 499
Packages used in this chapter _________________________________________________________________ 500
One-way ANOVA example ___________________________________________________________________ 500
Summarize data by group _________________________________________________________________ 502
Box plots for data by group ________________________________________________________________ 503
Plot of means and confidence intervals_______________________________________________________ 503
Define linear model ______________________________________________________________________ 504
Conduct analysis of variance _______________________________________________________________ 504
Histogram of residuals ____________________________________________________________________ 505
Post-hoc analysis: mean separation tests _____________________________________________________ 505
References ________________________________________________________________________________ 507
Exercises R ________________________________________________________________________________ 507
One-way ANOVA with Blocks _________________________________________________________ 509
Packages used in this chapter _________________________________________________________________ 510
One-way ANOVA with blocks example __________________________________________________________ 510
Use Summarize to check if cell sizes are balanced ______________________________________________ 512
Define linear model ______________________________________________________________________ 513
Conduct analysis of variance _______________________________________________________________ 513
Histogram and plot of residuals _____________________________________________________________ 513
Post-hoc analysis: mean separation tests _____________________________________________________ 514
Plot of LS means and confidence intervals ____________________________________________________ 515
Exercises S ________________________________________________________________________________ 516
One-way ANOVA with Random Blocks __________________________________________________ 518
Packages used in this chapter _________________________________________________________________ 519
One-way ANOVA with random blocks example ___________________________________________________ 519
Mixed-effects model with lmer _____________________________________________________________ 521
Mixed-effects model with nlme _____________________________________________________________ 524
Comparison of results from one-way ANOVA with blocks __________________________________________ 526
Two-way ANOVA ___________________________________________________________________ 527
Packages used in this chapter _________________________________________________________________ 528
Two-way ANOVA example ___________________________________________________________________ 528
Use Summarize to check if cell sizes are balanced ______________________________________________ 530
Define linear model ______________________________________________________________________ 530
Conduct analysis of variance _______________________________________________________________ 531
Histogram of residuals ____________________________________________________________________ 531
Post-hoc analysis: mean separation tests _____________________________________________________ 532
Plot of means and confidence intervals for main effects _________________________________________ 533
Plot of LS means and confidence intervals for interaction ________________________________________ 535
xix
Exercises T ________________________________________________________________________________ 537
Repeated Measures ANOVA __________________________________________________________ 538
Packages used in this chapter _________________________________________________________________ 539
Repeated measures ANOVA example___________________________________________________________ 539
Optional analysis: determining autocorrelation in residuals _________________________________________ 547
Optional discussion on specifying formulae for repeated measures analysis ___________________________ 548
Specifying random effects in models _________________________________________________________ 548
Indicating time and subject variables ________________________________________________________ 548
Correlation and Linear Regression _____________________________________________________ 549
Packages used in this chapter _________________________________________________________________ 550
Examples for correlation and linear regression ___________________________________________________ 550
Visualizing correlated variables _____________________________________________________________ 552
Pearson correlation ______________________________________________________________________ 555
Kendall correlation _______________________________________________________________________ 557
Spearman correlation _____________________________________________________________________ 557
Linear regression ________________________________________________________________________ 558
Polynomial regression ____________________________________________________________________ 561
A few of xkcd comics ________________________________________________________________________ 565
Correlation _____________________________________________________________________________ 565
Extrapolating ____________________________________________________________________________ 565
Cat proximity ___________________________________________________________________________ 565
Exercises U ________________________________________________________________________________ 565
References ________________________________________________________________________________ 567
Advanced Parametric Methods ________________________________________________________ 567
Packages used in this chapter _________________________________________________________________ 567
More complex experimental designs ___________________________________________________________ 568
Analysis of co-variance ______________________________________________________________________ 568
Nonlinear regression and curvilinear regression __________________________________________________ 568
Multiple regression _________________________________________________________________________ 569
Logistic regression __________________________________________________________________________ 569
Analysis of count data _______________________________________________________________________ 569
Robust techniques __________________________________________________________________________ 569
Example of robust linear regression _________________________________________________________ 570
Contrasts in Linear Models ___________________________________________________________________ 574
Linear plateau and quadratic plateau models ____________________________________________________ 574
Examples of linear plateau and quadratic plateau models ________________________________________ 574
Cate–Nelson analysis ________________________________________________________________________ 587
Transforming Data __________________________________________________________________ 588
Transforming data __________________________________________________________________________ 589
Packages used in this chapter _________________________________________________________________ 589
Example of transforming skewed data __________________________________________________________ 590
Square root transformation ________________________________________________________________ 591
Cube root transformation _________________________________________________________________ 591
Log transformation _______________________________________________________________________ 592
Tukey’s Ladder of Powers transformation _______________________________________________________ 593
Example of Tukey-transformed data in ANOVA ________________________________________________ 594
Box–Cox transformation _____________________________________________________________________ 599
Box–Cox transformation for a single variable __________________________________________________ 600
Example of Box–Cox transformation for ANOVA model __________________________________________ 601
Conclusions _______________________________________________________________________________ 606
xx
Analysis of Count Data and Percentage Data ________________________________________ 607
Hermite and Poisson Regression for Count Data __________________________________________ 607
Introduction _______________________________________________________________________________ 607
Count data _____________________________________________________________________________ 607
Regression approaches for count data _______________________________________________________ 607
Cautionary note _________________________________________________________________________ 607
Generalized linear regression ______________________________________________________________ 607
Packages used in this chapter _________________________________________________________________ 607
Hermite regression _________________________________________________________________________ 608
Hermite regression example _______________________________________________________________ 608
Hermite regression _______________________________________________________________________ 610
Histograms _____________________________________________________________________________ 611
Post-hoc analysis: Medians and confidence intervals ____________________________________________ 611
Post-hoc analysis: Pairwise models __________________________________________________________ 613
Poisson regression example __________________________________________________________________ 615
Negative binomial regression example _________________________________________________________ 616
Zero-inflated regression example ______________________________________________________________ 618
Robust Poisson regression example ____________________________________________________________ 619
Quasi-Poisson regression ____________________________________________________________________ 620
Optional code for chi-square goodness-of-fit test _________________________________________________ 621
Omnibus test ___________________________________________________________________________ 621
Post-hoc chi-square tests __________________________________________________________________ 622
Optional analysis: Vuong test to compare Poisson, negative binomial, and zero-inflated models ___________ 622
References ________________________________________________________________________________ 624
References for count data ____________________________________________________________________ 625
Beta Regression for Percent and Proportion Data _________________________________________ 625
Introduction _______________________________________________________________________________ 625
Proportion data _________________________________________________________________________ 625
Beta regression __________________________________________________________________________ 626
Packages used in this chapter _________________________________________________________________ 627
Beta regression example with discrete counts ___________________________________________________ 627
Beta regression example __________________________________________________________________ 628
Alternate logistic regression________________________________________________________________ 630
Beta regression example with inherently proportional data _________________________________________ 631
Beta regression __________________________________________________________________________ 633
References ________________________________________________________________________________ 637
xxi
PURPOSE OF THIS BOOK SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Introduction
The goal of this book is to introduce to students interested in extension education, outreach, and
public education to the quantitative methods used to assess the evaluation of these activities.
Extension education includes a diverse collection of subject matter, including environmental science,
home horticulture, agriculture, youth development, nutrition, and financial literacy.
Tools for evaluating educational programs may include in-class surveys that measure the knowledge
gain of students in a course or follow-up surveys to determine the behaviors adopted by course
participants. Evaluation may also include any number of measured variables, perhaps the age of youth
participants, the number of calories eaten daily by students in a nutrition program, or the organic
matter content of farm fields managed by participating farmers.
The examples and methods here are chosen specifically to be applicable to the evaluation of extension
education programs. That being said, these methods are some of the most common used in the
analysis of experiments—techniques used from diverse disciplines from manufacturing to
environmental science to psychology, though each of these disciplines has additional methods used in
specific situations.
One goal of this book is to give readers the skills and abilities to be able to understand the graphs and
statistics that you might encounter in a publication such as the Journal of Extension or other academic
reports of program results. As examples, students will be able to answer the questions: What can I
conclude from this bar plot? How do I interpret this p-value? What is an r-squared value?
A second goal is for readers to be able to design and analyze their own program evaluation
experiments in order to document the impacts of their extension teaching or research. What analysis
would I use to assess knowledge gain with before-and-after surveys? What statistics should I report to
convey the results of this analysis? Can I explain the results with a graph?
Pre-requisites
This book is written for students at the undergraduate level with no prior knowledge of the analysis of
experiments, and with no prior knowledge of computer programming. This being said, students with
no background in these areas will need to apply care and dedication in order to understand the
material and the computer code used in examples. These students may also need to explore the
optional readings to obtain a better foundation in statistical thinking and theory.
1
ABOUT THE AUTHOR OF THIS BOOK SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Likewise, discussion of the assumptions and theoretical considerations for the statistical analyses
included here is limited. Readers are encouraged to understand these considerations in more depth
when using these analyses.
I am neither a statistician nor an R programmer, so all advice and code in this book comes without
guarantee. I am happy to accept corrections or suggestions. Send correspondence to
[email protected].
Using R
R and RStudio
This book will use the software package R Project for Statistical Computing to create plots and conduct
statistical analyses. It is free to install on a Windows, Mac, or Linux computer. Although it is not
required, I also recommend using RStudio, which is also free.
Links to websites where the software can be obtained are included in the “Obtaining R” section in the
“Required Readings” below.
2
USING R SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
RStudio provides a nice work environment because it presents several windows on the screen that
make it easy to view code, results, and plots at once.
Program code can be worked on in the upper left Script window. If that window isn’t displayed, it can
be opened with File > New File > R Script. Code in the Script window is selected, and the Run button
is used to run the code. The results are reported in the Console window on the lower right. Code can
be saved as an .r or .R file, and those files should subsequently open automatically with RStudio.
The lower right window will usually show either plot results or help results.
Installation
As far as I know, R should be installed first. And then when RStudio is installed, you will tell RStudio
where R is installed on the machine. Links to obtain this software are in the Obtaining R in the
Required Readings below.
If you are not using RStudio, code is pasted directly into the R GUI Console. Results are produced in the
Console as code is entered, and plots will open in a separate window.
If you don’t have your own computer on which to install the software, there are a few options.
Using R online
There are websites on which you can run R in an online environment. r-fiddle.org is one such site.
However, I have had trouble using some R packages used in this book on r-fiddle.
apps.rutgers.edu
R and RStudio are also included in the apps.rutgers.edu environment. You can log in at
apps.rutgers.edu with your Rutgers Net ID. Then, Desktop > Menu > Development > R Studio. In
3
USING R SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
general, I have not found this to be the most convenient environment to work in. Working on a laptop,
I have found that I had to zoom out with the browser zoom to see the whole virtual desktop on my
screen. To transfer text to the desktop environment, paste the text into the clipboard using the
clipboard icon at the upper right of the screen. Also, I have had trouble trying to install additional R
packages in this environment.
If you are unsure if you can install additional R packages in the environment you are working in, try
the two examples below. The psych and FSA packages may take a while for their initial installation.
The code for you to run is in blue, and the output is in red. The output is truncated here.
Don’t worry too much about what the code is doing at this point. The main point is to see if you can
get output from both the psych and FSA packages.
The code assigns a vector of numbers to Score, and a vector of text strings to Student. It then combines
those two into a data frame called Data, which is then printed. The summary function counts the
values in Student, and determines the median and other statistics for Score. The psych package is
installed, then loaded with the library function, and then is used to output summary statistics for Score
for each Student. The same is then done with the FSA package.
Remember to run only the blue code. The red code is the (truncated) output R should produce.
Score = c(10, 9, 8, 7, 7, 8, 9, 10, 6, 5, 4, 9, 10, 9, 10)
Student = c("Bugs", "Bugs", "Bugs", "Bugs","Bugs",
"Daffy", "Daffy", "Daffy", "Daffy", "Daffy",
"Taz", "Taz", "Taz", "Taz", "Taz")
Data
Student Score
1 Bugs 10
2 Bugs 9
3 Bugs 8
4 Bugs 7
5 Bugs 7
6 Daffy 8
7 Daffy 9
8 Daffy 10
9 Daffy 6
10 Daffy 5
11 Taz 4
12 Taz 9
13 Taz 10
14 Taz 9
15 Taz 10
summary(Data)
4
USING R SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Student Score
Bugs :5 Min. : 4.000
Daffy:5 1st Qu.: 7.000
Taz :5 Median : 9.000
Mean : 8.067
3rd Qu.: 9.500
Max. :10.000
if(!require(psych)){install.packages("psych")}
library(psych)
describeBy(x = Score,
group = Student)
group: Bugs
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 5 8.2 1.3 8 8.2 1.48 7 10 3 0.26 -1.96 0.58
------------------------------------------------------------------------
group: Daffy
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 5 7.6 2.07 8 7.6 2.97 5 10 5 -0.11 -2.03 0.93
------------------------------------------------------------------------
group: Taz
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 5 8.4 2.51 9 8.4 1.48 4 10 6 -0.97 -1.04 1.12
if(!require(FSA)){install.packages("FSA")}
library(FSA)
Summarize(Score ~ Student,
data=Data)
Required readings
The following readings are required for this chapter. You can read them at the individual links below,
or as chapters in the pdf version of the R Companion to the Handbook of Biological Statistics
(rcompanion.org/documents/RCompanionBioStatistics.pdf).
About R
rcompanion.org/rcompanion/a_04.html
5
USING R SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Obtaining R
rcompanion.org/rcompanion/a_05.html
Avoiding Pitfalls in R
rcompanion.org/rcompanion/a_07.html
Help with R
rcompanion.org/rcompanion/a_08.html
R Tutorials
rcompanion.org/rcompanion/a_09.html
Mangiafico, S.S. 2015. An R Companion for the Handbook of Biological Statistics, version 1.09.
rcompanion.org/rcompanion/. (Pdf version:
rcompanion.org/documents/RCompanionBioStatistics.pdf.)
Exercises A
1. Install R and RStudio on your computer, or determine how you will access this software. Be sure
that you will be able to install additional R packages (as in “Tests for package installation” above).
2. Run the sample code snippets in the “Required readings” above, and in the “Tests for package
installation above”. Try understanding the code. Modify the data in the examples and examine the
output. You should be comfortable running short programs in R and examining the results before
continuing with this book.
3. If you haven't installed the FSA package, do so with the following command, or use Tools > Install
packages in RStudio.
if(!require(FSA)){install.packages("FSA")}
The package contains a data set called ChinookArg that has the lengths and weights of Chinook salmon
at three locations.
It is not necessary but the following will load the data set into memory.
library(FSA)
data(ChinookArg)
6
USING R SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
?ChinookArg
We can see the first and last rows of the data with
library(FSA)
headtail(ChinookArg)
Note that the variable tl is length, w is weight, and loc is location. This is explained in the help file
pulled up with
?ChinookArg
library(FSA)
Summarize(tl ~ loc,
data = ChinookArg)
This example uses formula notation, where tl is measurement variable and loc is the grouping variable,
and they are separated with a tilde. We could also think of tl as the dependent variable and loc as the
independent variable.
Some functions accept formula notation and some do not. Usually asking for help about the function
will help you determine what input is required and what other arguments can be passed to the
function, e.g
?Summarize
4. Install the ggplot2 package with following command, or use Tools > Install packages in RStudio.
7
USING R SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
if(!require(ggplot2)){install.packages("ggplot2")}
The following code will plot Chinook length vs. weight. It will add a smooth line to the plot.
library(ggplot2)
qplot(x = w,
y = tl,
data = ChinookArg,
geom = c("point", "smooth"),
xlab = "Weight (kg)",
ylab = "Length (cm)",
main = "Chinook plot by Sal")
In RStudio, in the Plot window, you can try the Export menu, and save as image, save as pdf, or copy to
clipboard.
I tend to use the pdf option and then edit the file with Photoshop or GIMP.
If you don't use RStudio, or would like to make custom image files, you can create an image file with
png(filename = "Rplot%03d.png",
width = 4,
height = 3,
units = "in",
res = 600)
qplot(x = w,
y = tl,
data = ChinookArg,
geom = c("point", "smooth"),
xlab = "Weight (kg)",
ylab = "Length (cm)",
main = "Chinook plot by Sal")
dev.off()
You may use the following to see where the file was saved, if you didn’t specify a path in the filename
argument.
getwd()
8
STATISTICS TEXTBOOKS AND OTHER RESOURCES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
5. In the qplot function above, switch the variable for weight, w, with the variable for location, loc.
Remove the whole line with the geom argument. And change the xlab argument to something
appropriate.
• What are the units for fish length in this data set?
Using this data set, summarize the length of whitefish (tl) by their age (scale1).
• Embed this plot with your assignment. Include appropriate axis labels and be sure the units
of length are correct.
Luckily there are free resources available that cover the key concepts in an introductory statistics
course. Some of these will be suggested as recommended reading in this book.
Diez, D.M., C.D. Barr, and M. Çetinkaya-Rundel. 2012. OpenIntro Statistics, 2nd ed. www.openintro.org/.
Types of data, plots, experimental design, sampling, probability, hypothesis testing, confidence
limits, t-test, analysis of variance, chi-square test, linear regression, multiple regression,
logistic regression.
9
STATISTICS TEXTBOOKS AND OTHER RESOURCES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Stockburger, D.W. 2013. Introductory Statistics: Concepts, Models, and Applications, 3rd web edition.
www.psychstat.missouristate.edu/IntroBook3/sbk.htm.
Distributions, regression, correlation, hypothesis testing, t-test, chi-square test, analysis of
variance.
Analyses in R
Online resources
Venables, W.N., D.M. Smith, and R Core Team. 2015. An Introduction to R. cran.r-
project.org/doc/manuals/r-release/R-intro.pdf.
Text books
Videos
10
STATISTICS TEXTBOOKS AND OTHER RESOURCES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Videos
Blogs
Learn and Teach Statistics and Operations Research (Articles by Dr. Nic).
learnandteachstatistics.wordpress.com/summary/.
Online learning modules and online courses offer another alternative to learning basic statistical
concepts, programming in R, or advanced statistical techniques.
Swirl
swirlstats.com/
Swirl has free, brief lessons for beginners to R programming and analysis. Topics include R
programming, data analysis, data visualization, data manipulation, statistical inference.
DataCamp
www.datacamp.com/
DataCamp has short online modules covering topics such as an introduction to R, data manipulation,
data visualization, and statistics with R. Limited parts of several courses are free. For the complete
courses, there is a subscription charge. There is a discount for students.
Coursera
www.coursera.org/
Coursera hosts MOOC’s from various universities on a range of academic topics, including introductory
statistics using R. Courses are free, though there is an option to pay for a certificate in a data science
specialization.
edX
www.edx.org/
edX also hosts free MOOC’s from universities on a variety of topics, including a course on introduction
to R programming.
Udemy
www.udemy.com/
Udemy hosts relatively inexpensive online courses on a variety of subjects, including programming
and statistics with R. Some are free.
11
STATISTICS TEXTBOOKS AND OTHER RESOURCES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
12
WHY STATISTICS? SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Why Statistics?
Statistics is a general term. Every time you report a mean, median, or standard deviation of your data,
you are reporting a statistic. Specifically, this type of statistics are called descriptive statistics. We can
also use statistical tests and report the statistics from these tests, such as p-values or r-squared values.
In order to convey the implications of these statistics and the underlying data, we might present them
in a plot or table.
There are several reasons why we want to conduct statistical analyses when looking at our program
evaluation data.
Communication
Descriptive statistics and statistical tests can be used to communicate with our colleagues. Once we
understand statistical tests—as well common statistics such as p-values, confidence intervals, and r-
squared values—we can communicate our results in a shared language. A single well designed plot
with appropriate statistics is an effective tool to convey our findings.
Communication is a two-way street. We also need to be able to understand others when they share
their results with us, whether it’s in a departmental presentation, a presentation at a professional
meeting, in extension literature, or in a peer-reviewed journal article.
Adding rigor
Sometimes educators will simply report the change in median scores before and after a course, or
report a best fit line for bivariate data. However, without applying a statistical tests and reporting p-
values or other appropriate statistics, it is not clear to the reader (or probably the author!) if the
reported effect is real.
Academic recognition
Correct and accepted statistical methods are usually required to get results published in academic
journals or proceedings from professional meetings.
13
EVALUATION TOOLS AND SURVEYS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Program evaluation
Appropriate statistics help to evaluate programs, for example determining if there was an increase in
student knowledge scores was statistically significant or if one teaching technique is better than
another. Such information can guide future programming efforts.
The statistical techniques presented in this book are applicable to a wide variety of disciplines, and are
some of the most common used across fields. While the analyses presented in this book are common
and relatively simple, understanding of these techniques will serve as a basis for more advanced
analyses.
Extension programs
An extension program is an organized educational endeavor delivered to some segment of the public,
such as famers, students in 4-H, or Master Gardeners. It is based on an assessments of needs for that
group for the specific topic of the program, and is based on objectives for what knowledge students
should improve, skills they should learn, or behaviors they should adopt.
An extension program will have multiple activities associated with these goals. The extension
educator may be organizing educational events, delivering lectures, writing factsheets, or reporting on
research.
Each of these components is evaluated in light of the objectives of the program and the objectives of
the individual activities. In each case objectives should be measureable.
In some cases, the evaluator can collect “hard data” about the results of some program: number of
attendees, number of downloads, measured differences in water quality.
In other cases evaluation tools such as surveys will be developed to evaluate how well program
objectives will be met. These might ask participants about an increase in their knowledge, a change in
their behavior, or adoption of specific practices, along with other relevant information.
Designing a questionnaire
14
EVALUATION TOOLS AND SURVEYS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
There is much to be learned about good questionnaire design, and good practices for conducting a
survey. This chapter will only scratch the surface of this topic.
Your questionnaire should be short enough and simple enough so that your respondents will be likely
to complete it, and complete it thoughtfully.
On the other hand, it should be long enough to gain valuable information. For example, it might not
want to ask simply, “Did you learn something?”, but ask how much was learned in each topic area.
Identifying areas where teaching was successful or not successful helps to improve the program in the
future.
It is also important to take into account your audience. Young children may be able to answer only a
few questions, simple in scope, and without too many options. On the other end of the spectrum,
expert audiences can much more critically assess programs and program component. Non-expert
adult audiences fall in the middle of this spectrum: They are capable of assessing their own changes in
knowledge, skills, attitudes, behaviors, and implemented practices. Still, questionnaires for them
should still be short and clear, with the possibility of some open-ended questions.
Types of impacts
A questionnaire may assess a single type of impact or several types depending on what is appropriate.
It may seek to assess knowledge gain, changes in attitude, gained skills, changes in behavior.
These can be assessed for an extension activity as short as single lecture, a day-long workshop, or after
a program lasting several years.
It is helpful if the same respondent can be identified on both tests, so that the before responses and
after responses can be paired for each respondent.
Personally, I find before-and-after assessment rather patronizing and annoying for adult audiences.
Instead, I prefer a self-assessment knowledge gain format, discussed below.
As an example, Bakacs et al. (2013) on Table 1, reported an increase in knowledge by respondents for
individual questions about using rain barrels, conserving water, and reducing stormwater runoff. For
example, they asked workshop attendees the following question, both before and after the workshop.
They asked respondents for their initials and favorite number for each test so that responses could be
paired for each respondent.
15
EVALUATION TOOLS AND SURVEYS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
One form of self-assessment question asks the respondent to assess their knowledge on a topic both
before and after the course, but is answered only once, after the course.
As an example, Mangiafico et al. (2011) (Table1) reported self-assessment changes in knowledge gain
by respondents for individual questions about environmentally-friendly lawn care. The survey was
done following a lecture, so that respondents reflected about their knowledge before and after the
lecture from the perspective of having just listened to the lecture.
Before After
A second method is to question participants at the conclusion of an event concerning their anticipated
changes in behaviors based on what they learned at the event. The obvious drawback to this approach
is that respondents are likely to anticipate that they will improve their behaviors, and this anticipation
may never materialize in reality. However, this method is often used because it is easier and allows
collecting data from a captive audience.
16
EVALUATION TOOLS AND SURVEYS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Follow-up surveys
Follow-up surveys are conducted at some time after a course or event, and are useful to assess if
specific practices were implemented or certain behaviors were changed, to give some time reflection
by respondents to consider changes in attitudes or knowledge.
As an example, Bakacs et al. (2013) on Table 3, used a follow-up survey to determine if workshop
participants installed rain barrels at their homes or businesses after building one at the workshop.
The follow-up survey in New Jersey was e-mailed to participants three to six months after the
workshops, and were completed online.
One drawback to this approach is that it is necessary to collect contact information from participants
and take the time re-survey participants. Another problem is that response rates may be lower than
response rates from in-classroom assessments.
Open-ended questions
Open-ended questions can be very valuable to solicit critical comments about a course or program, or
assess knowledge gain or other impacts that aren’t covered by other assessment questions.
Opened-ended questions can attempt to solicit critical comments—“What could be done to improve
this program?”; assess knowledge gain—“What did you learn from this program?”; or gain other
information—“Where did you learn about this course?”.
These questions can be very valuable, but you should also be cautious since responses may be sparse
and not representative of program participants. In particular, critical responses may be
unrepresentatively positive or negative. In general, participants may or may not put a lot effort into
answering open-ended questions, depending on a variety of factors not necessarily related to program
content or quality.
Demographic data
Finally, collecting demographic data of questionnaire respondents can be useful for interpreting and
understanding responses for other questions. Demographic data may include profession or
stakeholder group; current practices; or age, grade, sex, etc.
Forms of responses
Answers to questions may be in the form of yes/no, multiple choice, ordered response (Likert), or
open-ended.
The statistical analysis of these different forms of answers will vary. This will be unpacked over the
course of this book.
17
EVALUATION TOOLS AND SURVEYS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Rutgers Cooperative Extension Turf Management for a Healthier Lawn, Program Evaluation Form
rcompanion.org/documents/TurfProgramEvaluation.pdf
Notes:
• Questions 1–11 are self-assessment questions of knowledge gain and attitudes, using before-
and-after in a single sitting. 5-point Likert items.
• Questions 12–13 are ratings of information and educational materials. 10-point Likert items,
with two opt-out options (Don’t know, and Not applicable)
• Questions 17 – 20 are anticipated behaviors. 10-point Likert items, with two opt-out options
(Don’t know, and Not applicable)
Optional resource
Poling, R. L. 1999. Example Extension Program Evaluation Tools. Agricultural Extension
Service Institute of Agriculture University of Tennessee. web.utk.edu/~aee/evaltools.pdf.
Optional Readings
18
EVALUATION TOOLS AND SURVEYS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
www.youtube.com/watch?v=FkX-t0Pgzzs.
Barkman, S. J. 2001. A Field Guide to Designing Quantitative Instruments to Measure Program Impact.
Purdue University.
www.northskynonprofitnetwork.org/sites/default/files/documents/Field%20Guide%20to%20Devel
oping%20Quantiative%20Instruments.pdf.
Bakacs, M., M. Haberland, S.S. Mangiafico, A. Winquist, C.C Obropta, A. Boyajian A., and S. Mellor. 2013.
Rain Barrels: A Catalyst for Change? Journal of Extension 51(3), article 3RIB6.
www.joe.org/joe/2013june/rb6.php
Mangiafico, S.S., C.C. Obropta, and E. Rossi-Griffin. 2011. A Lawn Care Education Program to Address
Water Conservation and Water Pollution Prevention in New Jersey. Journal of the National Association
of County Agricultural Agents 4(2). www.nacaa.com/journal/index.php?jid=108.
Acknowledgements
I would like to thank Dan Kluchinski of Rutgers Cooperative Extension and the Rutgers Department of
Agricultural and Resource Management Agents for supplying some resources for this chapter.
19
TYPES OF VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Types of Variables
Organizing data: observations and variables
In general, collected raw data is organized according to observations and variables. Variables
represent a single measurement or characteristic for each observation. In the Statistics Learning
Center video in the Required Readings below, Dr. Nic gives an example of a survey where each
observation is a separate person, and the variables are age, sex, and chocolate preference for each
person.
In education evaluation, observations will commonly be a student or an instructor, but could also be
any other experimental unit, such as a single farmer’s field.
In reality, a single observation may be more specific. For example, the rating for one instructor by one
student on one date. For example, here is part of the example from the Friedman Test chapter with
some additional hypothetical data.
Each row represents an observation. So each observation contains the rating by a single student on a
single date for an instructor. The variables are Instructor, Date, Student, and Rating.
Instructor Date Student Rating
Bob Belcher 2015-01-01 a 4
Bob Belcher 2015-01-01 b 5
Bob Belcher 2015-01-01 c 4
Bob Belcher 2015-01-01 d 6
Bob Belcher 2015-02-05 e 6
Bob Belcher 2015-02-05 f 6
Bob Belcher 2015-02-05 g 10
Bob Belcher 2015-02-05 h 6
Linda Belcher 2015-01-01 a 8
Linda Belcher 2015-01-01 b 6
Linda Belcher 2015-01-01 c 8
Linda Belcher 2015-01-01 d 8
Linda Belcher 2015-02-05 e 8
Linda Belcher 2015-02-05 f 7
Linda Belcher 2015-02-05 g 10
Linda Belcher 2015-02-05 h 9
In general, it is best to keep data in long format for summary and analyses.
20
TYPES OF VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
However, to conduct certain analyses in R, or to produce certain plots, data will need to be in wide
format. The following is an example of the same data, translated into wide format, with a focus on the
ratings by date, and in this case ignoring student.
Instructor ——————— Rating ———————
2015-01-01 2015-02-05
Bob Belcher 4 6
Bob Belcher 5 6
Bob Belcher 4 10
Bob Belcher 6 6
Linda Belcher 8 8
Linda Belcher 6 7
Linda Belcher 8 10
Linda Belcher 8 9
Types of variables
The most common variables used in data analysis can be classified as one of three types of variables:
nominal, ordinal, and interval/ratio.
Understanding the differences in these types of variables is critical, since the variable type will
determine which statistical analysis will be valid for that data. In addition, the way we summarize
data with statistics and plots will be determined by the variable type.
Nominal data
Nominal variables are data whose levels are labels or descriptions, and which cannot be ordered.
Examples of nominal variables are sex, school, and yes/no questions. They are also called “categorical”
or “qualitative” variables, and the levels of a variable are sometimes called “classes” or “groups”.
The levels of categorical variables cannot be ordered. For the variable sex, it makes no sense to try to
put the levels “female”, “male”, and “other” in any numerical order. If levels are numbered for
convenience, the numbers are arbitrary, and the variable can’t be treated as a numeric variable.
Ordinal data
Ordinal variables can be ordered, or ranked in logical order, but the interval between levels of the
variables are not necessarily known. Subjective measurements are often ordinal variables. One
example would be having people rank four items by preference in order from one to four. A different
example would be having people assess several items based on a Likert ranking scale: “On a scale of
one to five, do you agree or disagree with this statement?” A third example is level of education for
adults, considering for example “less than high school”, “high school”, “associate’s degree”, etc.
Critically, in each case we can order the responses: My first favorite salad dressing is better than
second favorite, which is better than my third favorite. But we cannot know if the interval between
the levels is equal. For example, the distance between your favorite salad dressing and your second
favorite salad dressing may be small, where there may be a large gap between your second and third
choices.
21
TYPES OF VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
We can logically assign numbers to levels of an ordinal variable, and can treat them in order, but
shouldn’t treat them as numeric: “strongly agree” and “neutral” may not average out to an “agree.”
For the purposes of this book, we will consider such Likert data to be ordinal data under most
circumstances.
Interval/ratio data
Interval/ratio variables are measured or counted values: age, height, weight, number of students. The
interval between numbers is known to be equal: the interval between one kilogram and two
kilograms is the same as between three kilograms and four kilograms. Interval/ratio data are also
called “quantitative” data, although ordinal data are also quantitative.
Measurements where there is a natural zero, such as length or height, or where a zero can be honestly
defined, such as time since an event, are considered ratio data.
For the most part, ratio and interval data are considered together. In general, just be careful not to
make senseless statements with interval data, such as saying, “The mean temperature in Greenhouse 1
was twice the mean temperature of Greenhouse 2.”
Levels of measurement
In general it is advantageous to treat variables as the highest level of measurement for which they
qualify. That is, we could treat education level as a categorical variable, but usually we will want to
treat it as an ordinal variable. This is because treating it as an ordinal variable retains more of the
information carried in the data. If we were to reduce it to a categorical variable, we would lose the
order of the levels of the variable. By using a higher level of measurement, we will have more options
in the way we analyze, summarize, and present data.
This being said, there may be cases when it is advantageous to treat ordinal or count data as
categorical. One case is if there are few levels of the variable, or if it makes sense to condense the
variable into a couple of broad categories. Another example of when we choose a lower level of
measurement is when we use nonparametric statistical analyses which treat interval/ratio data as
ordinal, or ranked, data.
22
TYPES OF VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Required readings
[Video] “Types of Data: Nominal, Ordinal, Interval/Ratio” from Statistics Learning Center (Dr. Nic).
2011. www.youtube.com/watch?v=hZxnzfnt5v8.
Optional readings
“Frequency, frequency tables, and levels of measurement”, Chapter 1.3 in Openstax College. 2013.
Introductory Statistics. Rice University. openstaxcollege.org/textbooks/introductory-statistics.
“Data basics”, Chapter 1.2 in Diez, D.M., C.D. Barr , and M. Çetinkaya-Rundel. 2012. OpenIntro
Statistics, 2nd ed. www.openintro.org/.
Exercises B
1. For the following variables and levels identify the variable as nominal, ordinal, or ratio/interval.
• Political affiliation (very liberal, liberal, independent, conservative, very conservative)
• Political affiliation (Democrat, Republican, unaffiliated)
• Student age
• Favorite school subject (Science, History, Math, Physical Education, English Literature)
• How often do you complete your homework (never, sometimes, often, always)
• Sex (1-female, 2-male, 3-other)
• Level of education (1-elementary school, 2-junior high school, 3-high school)
• Dairy cow weight
• Number of meals and snacks eaten in a day
• (Yes, No)
• (Yes, No, Don’t know)
• Favorite salad dressing (1st, 2nd, 3rd, 4th)
• Favorite salad dressing (Italian, French, Caesar, etc.)
2. Are the following terms associated with nominal, ordinal, or ratio/interval data.
• Likert
• Categorical
• Continuous
• Qualitative
• Discrete
23
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Descriptive Statistics
Packages used in this chapter
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(DescTools)){install.packages("DescTools")}
if(!require(Rmisc)){install.packages("Rmisc")}
if(!require(FSA)){install.packages("FSA")}
if(!require(plyr)){install.packages("plyr")}
if(!require(boot)){install.packages("boot")}
Descriptive statistics
Descriptive statistics are used to summarize data in a way that provides insight into the information
contained in the data. This might include examining the mean or median of numeric data or the
frequency of observations for nominal data. Plots can be created that show the data and indicating
summary statistics.
Choosing which summary statistics are appropriate depend on the type of variable being examined.
Different statistics should be used for interval/ratio, ordinal, and nominal data.
In describing or examining data, you will typically be concerned with measures of location, variation,
and shape.
24
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Location is also called central tendency. It is a measure of the values of the data. For example, are the
values close to 10 or 100 or 1000? Measures of location include mean and median, as well as
somewhat more exotic statistics like M-estimators or Winsorized means.
Variation is also called dispersion. It is a measure of how far the data points lie from one another.
Common statistics include standard deviation and coefficient of variation. For data that aren’t
normally-distributed, percentiles or the interquartile range might be used.
Shape refers to the distribution of values. The best tools to evaluate the shape of data are histograms
and related plots. Statistics include skewness and kurtosis, though they are less useful than visual
inspection. We can describe data shape as normally-distributed, log-normal, uniform, skewed, bi-
modal, and others.
For this example, imagine that Ren and Stimpy have each held eight workshops educating the public
about water conservation at home. They are interested in how many people showed up to the
workshops.
Because the data are housed in a data frame, we can use the convention Data$Attendees to access the
variable Attendees within the data frame Data.
Input = ("
Instructor Location Attendees
Ren North 7
Ren North 22
Ren North 6
Ren North 15
Ren South 12
Ren South 13
Ren South 14
Ren South 16
Stimpy North 18
Stimpy North 17
Stimpy North 15
Stimpy North 9
Stimpy South 15
Stimpy South 11
Stimpy South 19
Stimpy South 23
")
Data = read.table(textConnection(Input),header=TRUE)
25
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
232
length(Data$Attendees)
16
Mean
The mean is the arithmetic average, and is a common statistic used with interval/ratio data. It is
simply the sum of the values divided by the number of values. The mean function in R will return the
mean.
sum(Data$Attendees) / length(Data$Attendees)
14.5
mean(Data$Attendees)
14.5
Caution should be used when reporting mean values with skewed data, as the mean may not be
representative of the center of the data. For example, imagine a town with 10 families, nine of whom
have an income of less than $50, 000 per year, but with one family with an income of $2,000,000 per
year. The mean income for families in the town would be $233,000, but this may not be a reasonable
way to summarize the income of the town.
Income = c(49000, 44000, 25000, 18000, 32000, 47000, 37000, 45000, 36000, 2000000)
mean(Income)
233300
Median
The median is defined as the value below which are 50% of the observations. To find this value
manually, you would order the observations, and separate the lowest 50% from the highest 50%. For
data sets with an odd number of observations, the median is the middle value. For data sets with an
even number of observations, the median falls half-way between the two middle values.
26
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The median is a robust statistic in that it is not affected by adding extreme values. For example, if we
changed Stimpy’s last Attendees value from 23 to 1000, it would not affect the median.
median(Data$Attendees)
15
### Note that in this case the mean and median are close in value
### to one another. The mean and median will be more different
### the more the data are skewed.
The median is appropriate for either skewed or unskewed data. The median income for the town
discussed above is $40,500. Half the families in the town have an income above this amount, and half
have an income below this amount.
Income = c(49000, 44000, 25000, 18000, 32000, 47000, 37000, 45000, 36000, 2000000)
median(Income)
40500
Note that medians are sometimes reported as the “average person” or “typical family”. Saying, “The
average American family earned $54,000 last year” means that the median income for families was
$54,000. The “average family” is that one with the median income.
Mode
The mode is a summary statistic that is used rarely in practice, but is normally included in any
discussion of mean and medians. When there are discreet values for a variable, the mode is simply the
value which occurs most frequently. For example, in the Statistics Learning Center video in the
Required Readings below, Dr. Nic gives an example of counting the number of pairs of shoes each
student owns. The most common answer was 10, and therefore 10 is the mode for that data set.
For our Ren and Stimpy example, the value 15 occurs three times and so is the mode.
Mode(Data$Attendees)
15
Standard deviation
The standard deviation is a measure of variation which is commonly used with interval/ratio data. It’s
a measurement of how close the observations in the data set are to the mean.
27
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
There’s a handy rule of thumb that—for normally distributed data—68% of data points fall within the
mean ± 1 standard deviation, 95% of data points fall within the mean ± 2 standard deviations, and
99.7% of data points fall within the mean ± 3 standard deviations.
Because the mean is often represented with the letter mu, and the standard deviation is represented
with the letter sigma, saying someone is “a few sigmas away from mu” indicates they are rather a rare
character. (I originally heard this joke on an episode of Car Talk, for which I cannot find a reference or
transcript.)
sd(Data$Attendees)
4.830459
The standard error is the standard deviation of a data set divided by the square root of the number of
observations. It can also be found in the output for the describe function in the psych package, labelled
se.
sd(Data$Attendees) /
sqrt(length(Data$Attendees))
1.207615
library(psych)
describe(Data$Attendees)
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 16 14.5 4.83 15 14.5 4.45 6 23 17 -0.04 -0.88 1.21
Standard error of the mean may not be appropriate for skewed data.
28
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
These values can also be called the minimum, 1st quartile, 2nd quartile, 3rd quartile, and maximum.
The five-number summary is a useful measure of variation for skewed interval/ratio data or for
ordinal data. 25% of values fall below the 1st quartile and 25% of values fall above the 3rd quartile.
This leaves the middle 50% of values between the 1st and 3rd quartiles, giving a sense of the range of
the middle half of the data. This range is called the interquartile range (IQR).
Percentiles and quartiles are relatively robust, as they aren’t affected much by a few extreme values.
They are appropriate for both skewed and unskewed data.
summary(Data$Attendees)
6 7 9 11 12 13 14 15 15 15 16 17 18 19 22 23
The answer is that there are several different methods to calculate percentiles, and they may give
slightly different answers. For details on the calculations, see ?quantiles.
For Attendees, the default type 7 calculation yields a 75th percentile value of 17.25, whereas the type 2
calculation simply splits the difference between 17 and 18 and yields 17.5. The type 1 calculation
doesn’t average the two values, and so just returns 17.
75%
17.25
75%
17.5
75%
17
29
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Percentiles other than the 25th, 50th, and 75th can be calculated with the quantiles function. For
example, to calculate the 95th percentile:
quantile(Data$Attendees, .95)
95%
22.25
Confidence intervals
Confidence intervals are covered in the next chapter.
Summarize in FSA
The Summarize function in the FSA package returns the number of observations, mean, standard
deviation, minimum, 1st quartile, median, 3rd quartile, and maximum for grouped data.
Note the use of formula notation: Attendees is the dependent variable (the variable you want to get the
statistics for); and Instructor is the independent variable (the grouping variable). Summarize allows
you to summarize over the combination of multiple independent variables by listing them to the right
of the ~ separated by a plus sign (+).
library(FSA)
Summarize(Attendees ~ Instructor,
data=Data)
summarySE in Rmisc
The summarySE function in the Rmisc package outputs the number of observations, mean, standard
deviation, standard error of the mean, and confidence interval for grouped data. The summarySE
30
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
function allows you to summarize over the combination of multiple independent variables by listing
them as a vector, e.g. c("Instructor", "Student").
library(Rmisc)
summarySE(data=Data,
"Attendees",
groupvars="Instructor",
conf.interval = 0.95)
Instructor N Attendees sd se ci
1 Ren 8 13.125 5.083236 1.797195 4.249691
2 Stimpy 8 15.875 4.454131 1.574773 3.723747
summarySE(data=Data,
"Attendees",
groupvars = c("Instructor", "Location"),
conf.interval = 0.95)
describeBy in psych
The describeBy function in the psych package returns the number of observations, mean, median,
trimmed means, minimum, maximum, range, skew, kurtosis, and standard error of the mean for
grouped data. describeBy allows you to summarize over the combination of multiple independent
variables by combining terms with a colon (:).
library(psych)
describeBy(Data$Attendees,
group = Data$Instructor,
digits= 4)
group: Ren
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 8 13.12 5.08 13.5 13.12 2.97 6 22 16 0.13 -1.08 1.8
-------------------------------------------------------------------------
group: Stimpy
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 8 15.88 4.45 16 15.88 3.71 9 23 14 -0.06 -1.26 1.57
describeBy(Data$Attendees,
group = Data$Instructor : Data$Location,
digits= 4)
31
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
group: Ren:North
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 4 12.5 7.51 11 12.5 6.67 6 22 16 0.26 -2.14 3.75
-------------------------------------------------------------------------
group: Ren:South
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 4 13.75 1.71 13.5 13.75 1.48 12 16 4 0.28 -1.96 0.85
-------------------------------------------------------------------------
group: Stimpy:North
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 4 14.75 4.03 16 14.75 2.22 9 18 9 -0.55 -1.84 2.02
-------------------------------------------------------------------------
group: Stimpy:South
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 4 17 5.16 17 17 5.93 11 23 12 0 -2.08 2.58
str(Data)
summary(Data)
32
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Max. :23.00
headTail(Data)
Note that factor variables are labeled with an asterisk (*), and the levels of the factors are coded as 1,
2, 3, etc.
library(psych)
describe(Data)
vars n mean sd median trimmed mad min max range skew kurtosis se
Instructor* 1 16 1.5 0.52 1.5 1.5 0.74 1 2 1 0.00 -2.12 0.13
Location* 2 16 1.5 0.52 1.5 1.5 0.74 1 2 1 0.00 -2.12 0.13
Attendees 3 16 14.5 4.83 15.0 14.5 4.45 6 23 17 -0.04 -0.88 1.21
By default, different functions in R will handle missing values in different ways. But most have options
to change how they treat missing data.
33
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
In general, you should scan your data for missing data, and think carefully about the best way to
handle observations with missing values.
Input = ("
Instructor Location Attendees
Ren North 7
Ren North 22
Ren North 6
Ren North 15
Ren South 12
Ren South 13
Ren South NA
Ren South 16
Stimpy North 18
Stimpy North 17
Stimpy North NA
Stimpy North 9
Stimpy South 15
Stimpy South 11
Stimpy South 19
Stimpy South 23
")
Data2 = read.table(textConnection(Input),header=TRUE)
Data2
Note that the na.rm option operates only on the data values actually passed to the function. In the
following example with median, only Attendees is passed to the function; if there were NA’s in other
variables, this would not affect the function.
Not all functions have the same default for the na.rm option. To determine the default, use e.g.
?median, ?mean, ?sd.
median(Data2$Attendees,
na.rm = FALSE)
NA
median(Data2$Attendees,
na.rm = TRUE)
34
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
15
### na.rm=TRUE. Drop observations with NA and then calculate the median.
Summarize(Attendees ~ Instructor,
data=Data2)
### This function removes missing values, but indicates the number of
### missing values by not including them in the count for nvalid.
describe(Data2$Attendees)
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 14 14.5 5.19 15 14.5 5.19 6 23 17 -0.04 -1.17 1.39
### Note that two NA’s were removed by default, reporting an n of 14.
35
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
summarySE(data=Data2,
"Attendees")
.id N Attendees sd se ci
1 <NA> 16 NA NA NA NA
library(Rmisc)
summarySE(data=Data2,
"Attendees",
na.rm=TRUE)
.id N Attendees sd se ci
1 <NA> 14 14.5 5.185038 1.38576 2.993752
Advanced techniques
Discarding subjects
Certain psychology studies use scales that are calculated from responses of several questions. Because
answers to all questions are needed to reliably calculate the scale, any observation (subject, person)
with missing answers will simply be discarded. Some types of analyses in other fields also follow this
approach.
Subjects can be deleted with the subset function. The following code creates a new data frame, Data3,
with all observations with NA in the variable Attendees removed from Data2.
Data3 = subset(Data2,
!is.na(Attendees))
Data3
Imputation of values
Missing values can be assigned a likely value through the process of imputation. An algorithm is used
that determines a value based on the values of other variables for that observation relative to values
for other variables. The mice package in R can perform imputation.
36
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The ! operator is a logical not. The brackets serve as a list of observations to include for the preceding
variable. So, the code essentially says, “Define a vector valid as the values of Attendees where the
values of Attendees are not NA.”
valid = Data2$Attendees[!is.na(Data2$Attendees)]
summary(valid)
Understanding skewness and kurtosis are important as they are ways in which a distribution of data
varies from a normal distribution. This will be important in assessing the assumptions of certain
statistical tests.
However, I rarely see skewness and kurtosis values reported. Instead, normality is usually assessed
visually with plot, or using certain statistical tests. One problem with using skewness and kurtosis
values is that there is not agreement in what values constitute meaningful deviations from the normal
curve.
Skewness
Skewness indicates the degree of asymmetry in a data set. If there are relatively more values that are
far greater than the mean, the distribution is positively skewed or right skewed, with a tail stretching
to the right. Negative or left skew is the opposite.
A symmetric distribution has a skewness of 0. The skewness value for a positively skewed distribution
is positive, and a negative value for a negatively skewed distribution. Sometimes a skew with an
absolute value greater than 1 or 1.5 or 2 is considered highly skewed. There is not agreement on this
interpretation.
There are different methods to calculate skew and kurtosis. The describe function in the psych package
has three options for how to calculate them.
library(psych)
describe(Data$Attendees,
type=3) ### Type of calculation for skewness and kurtosis
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 16 14.5 4.83 15 14.5 4.45 6 23 17 -0.04 -0.88 1.21
37
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For more information on the normal curve, see the video on “Normal Distribution” from Statistics
Learning Center in the “Optional Readings” section. Additional thoughts on the normal distribution
and real-world data distributions are in the article by Dr. Nic in the “Optional Readings” section.
Kurtosis
Kurtosis measures the degree to which the distribution is either more sharply peaked or more squat
relative to a normal distribution. In general, the higher the kurtosis, the sharper the peak and the
longer the tails. This is called leptokurtic, and is indicated by positive kurtosis values. The opposite—
platykurtosis—has negative kurtosis values. Sometimes a kurtosis with an absolute value greater than
2 or 3 is considered a high deviation from being mesokurtic. There is not agreement on this
interpretation.
Descriptive statistics for ordinal data are more limited than those for interval/ratio data. You’ll
remember that for ordinal data, the levels can be ordered, but we can’t say that the intervals between
the levels are equal. For example, we can’t say that an Associate’s degree and Master’s degree
38
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
somehow average out to a Bachelor’s degree. This concept is discussed in more detail in the chapters
on Likert data.
Because of this fact, several common descriptive statistics are usually inappropriate for use with
ordinal data. These include mean, standard deviation, and standard error of the mean.
Ordinal data can be described by either: 1) treating the data as numeric and using appropriate
statistics such as median and quartiles; or, 2) treating the data as nominal, and looking at counts of the
data for each level.
A more-complete discussion of descriptive statistics for ordinal data can be found in the Descriptive
Statistics for Likert Data chapter.
For this data, we have manually coded Education with numbers in a separate variable Ed.code. These
numbers list the education in order: High school < Associate’s < Bachelor’s < Master’s < Ph.D.
Of course we could have had R do this coding for us, but the code is slightly messy, so I did the coding
manually, and listed the optional R code below.
Ideally we would want to treat Education as an ordered factor variable in R. But unfortunately most
common functions in R won’t handle ordered factors well. Later in this book, ordered factor data will
be handled directly with cumulative link models (CLM), permutation tests, and tests for ordered
tables.
Input = ("
Date Instructor Student Sex County Education Ed.code
'2015-11-01' 'Arthur Read' a female 'Elwood' BA 3
'2015-11-01' 'Arthur Read' b female 'Bear Lake' PHD 5
'2015-11-01' 'Arthur Read' c male 'Elwood' BA 3
'2015-11-01' 'Arthur Read' d female 'Elwood' MA 4
'2015-11-01' 'Arthur Read' e male 'Elwood' HS 1
'2015-11-01' 'Arthur Read' f female 'Bear Lake' MA 4
'2015-11-01' 'Arthur Read' g male 'Elwood' HS 1
'2015-11-01' 'Arthur Read' h female 'Elwood' BA 3
'2015-11-01' 'Arthur Read' i female 'Elwood' BA 3
'2015-11-01' 'Arthur Read' j female 'Elwood' BA 3
'2015-12-01' 'Buster Baxter' k male 'Elwood' MA 4
'2015-12-01' 'Buster Baxter' l male 'Bear Lake' MA 4
'2015-12-01' 'Buster Baxter' m female 'Elwood' AA 2
'2015-12-01' 'Buster Baxter' n male 'Elwood' AA 2
'2015-12-01' 'Buster Baxter' o other 'Elwood' BA 3
39
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
Data
str(Data)
summary(Data)
rm(Input)
40
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
summary(Data$Education.ordered)
HS AA BA MA PHD
2 2 7 4 2
median(Data$Education.ordered)
[1] "BA"
median(Data$Ed.code)
41
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Summarize(Ed.code ~ Sex,
data=Data)
### Remember to ignore the mean and sd values for ordinal data.
library(FSA)
### Remember to ignore the mean and sd values for ordinal data.
The prop.table function translates a table into proportions. The margin=1 option indicates that the
proportions are calculated for each row.
First we will order the levels of Education, otherwise R with report results in alphabetical order.
Data$Education = factor(Data$Education,
levels = c("HS", "AA", "BA", "MA", "PHD"))
summary(Data$Education)
HS AA BA MA PHD
2 2 7 4 2
XT = xtabs( ~ Education,
data=Data)
XT
42
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Education
HS AA BA MA PHD
2 2 7 4 2
prop.table(XT)
Education
HS AA BA MA PHD
0.1176471 0.1176471 0.4117647 0.2352941 0.1176471
Grouped data
XT = xtabs( ~ Sex + Education,
data = Data)
XT
Education
Sex HS AA BA MA PHD
female 0 1 5 2 2
male 2 1 1 2 0
other 0 0 1 0 0
prop.table(XT,
margin = 1)
Education
Sex HS AA BA MA PHD
female 0.0000000 0.1000000 0.5000000 0.2000000 0.2000000
male 0.3333333 0.1666667 0.1666667 0.3333333 0.0000000
other 0.0000000 0.0000000 1.0000000 0.0000000 0.0000000
XT
Education
Sex HS AA BA MA PHD
female 0 0 0 1 2
43
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
male 0 0 0 1 0
other 0 0 0 0 0
, , County = Elwood
Education
Sex HS AA BA MA PHD
female 0 1 5 1 0
male 2 1 1 1 0
other 0 0 1 0 0
Descriptive statistics for nominal data consist of listing or plotting counts for levels of the nominal
data, often by levels of a grouping variable. Tables of this information are often called contingency
tables.
For this example, we will look again at Arthur and Buster’s data, but this time considering Sex to be the
dependent variable of interest.
If levels of a nominal variable are coded with numbers, remember that the numbers will be arbitrary.
For example, if you assign female = 1, and male = 2, and other = 3, it makes no sense to say that the
average sex in Arthur’s class was 1.3. Or that the average sex in Buster’s class was greater than that in
Arthur’s. It also makes no sense to say that the median sex was female.
Input = ("
Date Instructor Student Sex County Education Ed.code
'2015-11-01' 'Arthur Read' a female 'Elwood' BA 3
'2015-11-01' 'Arthur Read' b female 'Bear Lake' PHD 5
'2015-11-01' 'Arthur Read' c male 'Elwood' BA 3
'2015-11-01' 'Arthur Read' d female 'Elwood' MA 4
'2015-11-01' 'Arthur Read' e male 'Elwood' HS 1
'2015-11-01' 'Arthur Read' f female 'Bear Lake' MA 4
'2015-11-01' 'Arthur Read' g male 'Elwood' HS 1
'2015-11-01' 'Arthur Read' h female 'Elwood' BA 3
'2015-11-01' 'Arthur Read' i female 'Elwood' BA 3
'2015-11-01' 'Arthur Read' j female 'Elwood' BA 3
'2015-12-01' 'Buster Baxter' k male 'Elwood' MA 4
'2015-12-01' 'Buster Baxter' l male 'Bear Lake' MA 4
'2015-12-01' 'Buster Baxter' m female 'Elwood' AA 2
'2015-12-01' 'Buster Baxter' n male 'Elwood' AA 2
'2015-12-01' 'Buster Baxter' o other 'Elwood' BA 3
'2015-12-01' 'Buster Baxter' p female 'Elwood' BA 3
'2015-12-01' 'Buster Baxter' q female 'Bear Lake' PHD 5
")
Data = read.table(textConnection(Input),header=TRUE)
44
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data
str(Data)
summary(Data)
rm(Input)
The prop.table function translates a table into proportions. The margin=1 option indicates that the
proportions are calculated for each row.
One-sample data
summary(Data$Sex)
One-way data
xtabs(~ Date + Sex,
data=Data)
Sex
Date female male other
2015-11-01 7 3 0
2015-12-01 3 3 1
prop.table(XT,
margin = 1)
Sex
Date female male other
2015-11-01 0.7000000 0.3000000 0.0000000
2015-12-01 0.4285714 0.4285714 0.1428571
45
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
sum(XT)
[1] 17
rowSums(XT)
2015-11-01 2015-12-01
10 7
colSums(XT)
Two-way data
xtabs(~ County + Sex + Date,
data=Data)
, , Date = 2015-11-01
Sex
County female male other
Bear Lake 2 0 0
Elwood 5 3 0
, , Date = 2015-12-01
Sex
County female male other
Bear Lake 1 1 0
Elwood 2 2 1
The order of levels of factor variables are important because most functions, including plotting
functions, will handle levels of the factor in order. The order of the levels can be changed, for example,
to change the order that groups are plotted in a plot, or which groups are at the top of the table.
46
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
By default, the read.table function in R interprets character data as factor variables. And by default R
alphabetizes the levels of the factors.
Looking at the Arthur and Buster data, note that Instructor, Student, Sex, among other variables, are
treated as factor variables.
str(Data)
Note also that the levels of the factor variables were alphabetized by default. That is, even though
Elwood was found in the data before Bear Lake, R treats Bear Lake as the first level in the variable
County.
summary(Data)
We can order factor levels by the order in which they were read in the data frame.
Data$County = factor(Data$County,
levels=unique(Data$County))
levels(Data$County)
levels(Data$Sex)
47
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Note that in the actions above, we are not changing the order in the data frame, simply which level is
treated internally by the software as "1" or "2", and so on.
Required readings
[Video] “Understanding Summary statistics: Mean, Median, Mode” from Statistics Learning Center
(Dr. Nic). 2015. www.youtube.com/watch?v=rAN6DBctgJ0.
Optional readings
"The median outclasses the mean" from Dr. Nic. 2013. Learn and Teach Statistics &
Operations Research. learnandteachstatistics.wordpress.com/2013/04/29/median/.
“Measures of the Location of the Data”, Section 2.3 in Openstax. 2013. Introductory Statistics.
openstaxcollege.org/textbooks/introductory-statistics.
“Measures of the Center of the Data”, Section 2.5 in Openstax. 2013. Introductory Statistics.
openstaxcollege.org/textbooks/introductory-statistics.
“Skewness and the Mean, Median, and Mode”, Section 2.6 in Openstax. 2013. Introductory
Statistics. openstaxcollege.org/textbooks/introductory-statistics.
“Standard error of the mean” in McDonald, J.H. 2014. Handbook of Biological Statistics.
www.biostathandbook.com/standarderror.html.
“Measures of the Spread of the Data”, Section 2.7 in Openstax. 2013. Introductory Statistics.
openstaxcollege.org/textbooks/introductory-statistics.
[Video] “Normal Distribution” from Statistics Learning Center (Dr. Nic). 2016.
www.youtube.com/watch?v=mtH1fmUVkfE.
"The normal distribution – three tricky bits" from Dr. Nic. 2016. Learn and Teach Statistics &
Operations Research. learnandteachstatistics.wordpress.com/2016/01/18/the-normal-
distribution/.
48
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Every time we report a descriptive statistic or the result of a statistical test, we are condensing
information, which may have been a whole data set, into one or a few pieces of information. It is the
job of the analyst to choose the best way to present descriptive and graphical information, and to
choose the correct statistical test or method.
Our first instinct in assessing the course might be to look at the average (mean) change in score. The
result is a mean increase of 21 points, which could be considered a success, perhaps.
mean(Increase)
[1] 21.42857
But we would be remiss to not look at other summary statistics and plots.
Using the Summarize function in the FSA package, we find that the median increase was only 10 points.
The increase for the first quartile was negative, suggesting at least 25% of students got a lower score
afterwards.
library(FSA)
Summarize(Increase,
digits = 2)
A histogram of the changes in scores suggests a slight right skew to the data, but that the mass of the
data sits not far from zero.
hist(Increase,
col="gray")
49
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Finally, we’ll compute the 95% confidence intervals for the mean change in score. Looking at the
results for the percentile method, the confidence interval includes zero, suggesting the change in
scores for this course were not statistically different from zero.
library(rcompanion)
Data = data.frame(Increase)
groupwiseMean(data = Data,
var = "Increase",
group = NULL,
traditional = FALSE,
percentile = TRUE)
Based on the data exploration, it seems that it would be irresponsible or dishonest to simply report
that the average increase in test scores was 20 points.
For descriptive statistics, we might report the mean and 95% confidence interval. Or perhaps the 5-
point summary for the change in scores, or show the histogram of values.
Optional analyses
Robust estimators of central tendency are used to describe the location of a data set without undue
influence of extreme values. Robust estimators include trimmed means, Winsorized means, and other
estimates like M-estimators (not discussed here).
50
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Example
The New Jersey Envirothon is a statewide competition for high school students covering various
aspects of natural resources science. In the Team Presentation station, student teams are judged by
five judges.
Currently, scorekeepers drop the highest and lowest scores for each team to avoid the effect of
aberrant low or high scores. This is an example of a trimmed mean. In this case, the mean is trimmed
to 60% (with 20% removed from each side).
Another option to ameliorate the effect of extreme scores is using Winsorized means. A Winsorized
mean removes extreme observations, but replaces them with the closest observations in terms of
magnitude.
In this example, two teams received five scores for their presentations that have the same mean and
median. We will look at robust estimators to determine if one team should be scored higher than the
other.
median(Team.A)
[1] 80
mean(Team.A)
[1] 70
mean(Team.A,
trim = .20) # This trims to the inner 60% of observations
[1] 76.66667
library(psych)
winsor(Team.A,
trim = 0.20) # This Winsorizes to the inner 60% of observations
[1] 92 90 80 60 52
### Note that the Winsorized values at the extremes appear to be calculated
### with a function analogous to the quantile(x, probs, type = 7) function.
winsor.mean(Team.A,
trim = 0.20) # This Winsorizes to the inner 60% of observations
[1] 74.8
51
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
median(Team.B)
[1] 80
mean(Team.B)
[1] 70
mean(Team.B,
trim = .20) # This trims to the inner 60% of observations
[1] 80
library(psych)
winsor(Team.B,
trim = 0.20) # This Winsorizes to the inner 60% of observations
[1] 80 80 80 80 70
### Note that the Winsorized values at the extremes appear to be calculated
### with a function analogous to the quantile(x, probs, type = 7) function.
winsor.mean(Team.B,
trim = 0.20) # This Winsorizes to the inner 60% of observations
[1] 78
In this example, the means and medians for Team A and Team B were identical. However, the
trimmed mean for Team A was less than for Team B (77 vs. 80), and the Winsorized mean for A was
less than for Team B (75 vs. 78). According to either the trimmed mean method or the Winsorized
mean method, Team B had a higher score.
Note also that the Winsorized means were lower than for the trimmed means, for both teams. This is
because the Winsorized mean is better able to take into account the low scores for each team.
Geometric mean
The geometric mean is used to summarize certain measurements, such as average return for
investments, and for certain scientific measurements, such as bacteria counts in environmental water.
It is useful when data are log-normally distributed.
Practically speaking, using the geometric mean ameliorates the effect of outlying values in the data.
52
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
To get the geometric mean, the log of each value is taken, these values are averaged, and then the
result is the base of the log raised to this value.
Imagine a series of 9 counts of bacteria from lake water samples, called Bacteria here. The geometric
mean can be calculated with a nested log, mean, and exp functions. Or more simply, the
geometric.mean function in the psych package can be used.
Bacteria example
Bacteria = c(20, 40, 50, 60, 100, 120, 150, 200, 1000)
exp(mean(log(Bacteria)))
[1] 98.38887
library(psych)
geometric.mean(Bacteria)
[1] 98.38887
library(psych)
geometric.mean(Return + 1)-1
[1] -0.07344666
### The geometric mean will give the correct answer for average
### annual return
This can also be calculated manually. If you start with 100 dollars with this investment, you will end
up with 63 dollars, equivalent to a return of – 0.07.
100 * (1-0.80) * 1.20 * 1.30 * 1.30 * 1.20 * 1.30
[1] 63.2736
53
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Harmonic mean
The harmonic mean is another type of average that is used in certain situations, such as averaging
rates or speeds.
To get the harmonic mean, the inverse of each value is taken, these values are averaged, and then the
inverse of this result is reported.
Imagine a series of 9 speeds, called Speed here. The harmonic mean can be calculated with a nested
1/x, mean, and 1/x functions. Or more simply, the harmonic.mean function in the psych package can be
used.
Speed = c(20, 40, 50, 60, 100, 120, 150, 200, 1000)
1/mean(1/Speed)
63.08411
library(psych)
harmonic.mean(Speed)
63.08411
Exercises C
• Which workshops had a higher mean number of attendees and what was the mean? Ren’s or
Stimpy’s?
• Which workshops had a higher mean number of attendees and what was the mean? Ren
North or Ren South?
54
DESCRIPTIVE STATISTICS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
• What was the median education level for females from Elwood County?
• What was the median education level for males from Elwood County?
• How many male students in the November workshop were from Bear Lake County?
5. As part of a nutrition education program, extension educators had students keep diaries of what
they ate for a day and then calculated the calories students consumed. The following data are the
result. Rating indicates the score, from 1 to 5, that students gave as to the usefulness of the program.
It should be considered an ordinal variable.
Student Teacher Sex Calories Rating
a Tetsuo male 2300 3
b Tetsuo female 1800 3
c Tetsuo male 1900 4
d Tetsuo female 1700 5
e Tetsuo male 2200 4
f Tetsuo female 1600 3
g Tetsuo male 1800 3
h Tetsuo female 2000 3
i Kaneda male 2100 4
j Kaneda female 1900 5
k Kaneda male 1900 4
l Kaneda female 1600 4
m Kaneda male 2000 4
n Kaneda female 2000 5
55
CONFIDENCE INTERVALS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
a. What are the variables in this data set and what type of variable is each? (Types are “nominal”,
“ordinal”, and “interval/ratio”, not how their types are reported by R.)
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
g. What was the mean caloric intake for females in Kaneda’s class?
Confidence Intervals
Packages used in this chapter
The following commands will install these packages if they are not already installed:
if(!require(Rmisc)){install.packages("Rmisc")}
if(!require(DescTools)){install.packages("DescTools")}
if(!require(plyr)){install.packages("plyr")}
if(!require(boot)){install.packages("boot")}
if(!require(rcompanion)){install.packages("rcompanion")}
56
CONFIDENCE INTERVALS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Confidence intervals are used to indicate how confident we are that a reported statistic falls within a
certain range. Confidence intervals can be calculated for a variety of statistics, such as the mean,
median, or slope of a linear regression. This chapter will focus on confidences intervals for means.
This book contains a separate chapter, Confidence Intervals for Medians, which addresses confidence
intervals for medians. There is also a chapter Confidence Intervals for Proportions in this book.
The Statistics Learning Center video in the Required Readings below gives a good explanation of the
meaning of confidence intervals.
The mean caloric intake could be calculated for this sample, but this mean will not be exactly the same
as the mean for the larger population. If we collect a large sample and the values aren’t too variable,
then the sample mean should be close to the population mean. But if we have few observations, or the
values are highly variable, we are less confident our sample mean is close to the population mean.
It’s best not to overthink the discussion on populations and samples. We aren’t necessarily actually
extending our statistics to a larger population. That is, we shouldn’t think our measurements from
two classrooms are actually indicative of the whole country. There are likely many factors that would
change the result school to school and region to region. But even if we are thinking about just the 7th
graders in just these two classrooms, most of our statistics will still be based on the assumption that
there is a whole large population of 7th graders in these two classrooms, and we are sampling just a
subset.
As a technically-dubious, but useful, interpretation, we often say that we are then justified in thinking
that the true population mean likely lies within our calculated confidence interval.
In all cases, the confidence interval says something about the calculated statistic. That is, we are
saying something about the mean, not about the underlying data per se.
57
CONFIDENCE INTERVALS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For example, if we want to compare the means of two groups to see if they are statistically different,
we will use a t-test, or similar test, calculate a p-value, and draw a conclusion. An alternative approach
would be to construct 95% or 99% confidence intervals about the mean for each group. If the
confidence intervals of the two means don’t overlap, we are justified in calling them statistically
different.
Likewise, if the 95% confidence interval for some statistic includes zero, we can conclude that the
statistic is not significantly different from zero.
As a technical note, non-overlapping confidence intervals for means do not equate exactly to a t-test
with a p-value of 0.05. They are different methods to assess similar questions. The article by
Cumming and Finch in the “References” section gives more details on the relationship between
overlapping confidence intervals and p-values from statistical tests.
For this example, extension educators had students wear pedometers to count their number of steps
over the course of a day. The following data are the result. Rating is the rating each student gave
about the usefulness of the program, on a 1-to-10 scale.
Input = ("
Student Sex Teacher Steps Rating
a female Catbus 8000 7
b female Catbus 9000 10
c female Catbus 10000 9
d female Catbus 7000 5
e female Catbus 6000 4
f female Catbus 8000 8
g male Catbus 7000 6
h male Catbus 5000 5
i male Catbus 9000 10
j male Catbus 7000 8
k female Satsuki 8000 7
l female Satsuki 9000 8
m female Satsuki 9000 8
n female Satsuki 8000 9
o male Satsuki 6000 5
p male Satsuki 8000 9
q male Satsuki 7000 6
r female Totoro 10000 10
s female Totoro 9000 10
t female Totoro 8000 8
u female Totoro 8000 7
v female Totoro 6000 7
w male Totoro 6000 8
x male Totoro 8000 10
y male Totoro 7000 7
z male Totoro 7000 7
")
Data = read.table(textConnection(Input),header=TRUE)
58
CONFIDENCE INTERVALS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data
str(Data)
summary(Data)
rm(Input)
The traditional method is the most commonly encountered, and is appropriate for normally
distributed data or with large sample sizes. It produces an interval that is symmetric about the mean.
For routine use, I recommend using bootstrapped confidence intervals, particularly the BCa or
percentile methods. For further discussion, see below Optional Analyses: confidence intervals for the
mean by bootstrapping.
The data must be housed in a data frame. By default, the function reports confidence intervals by the
traditional method.
In the groupwiseMean function, the measurement variable is indicated by the var argument, and the
grouping variable is indicated by the group argument. Variable names are in quotes.
The confidence level is indicated by, e.g., the conf = 0.95 argument. The digits option indicates the
number of significant digits to which the output is rounded. Note that in the output, the means and
other statistics are rounded to 3 significant figures.
Ungrouped data
Ungrouped data is indicated with the group = NULL argument.
library(rcompanion)
groupwiseMean(data = Data,
var = "Steps",
group = NULL,
59
CONFIDENCE INTERVALS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
conf = 0.95,
digits = 3)
One-way data
library(rcompanion)
groupwiseMean(data = Data,
var = "Steps",
group = "Sex",
conf = 0.95,
digits = 3)
Two-way data
library(rcompanion)
groupwiseMean(data = Data,
var = "Steps",
group = c("Teacher", "Sex"),
conf = 0.95,
digits = 3)
60
CONFIDENCE INTERVALS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
optional statistic, the mean by bootstrap. The R option indicates the number of iterations to calculate
each bootstrap statistic.
library(rcompanion)
groupwiseMean(data = Data,
var = "Steps",
group = "Sex",
conf = 0.95,
digits = 3,
R = 10000,
boot = TRUE,
traditional = FALSE,
normal = FALSE,
basic = FALSE,
percentile = FALSE,
bca = TRUE)
library(rcompanion)
groupwiseMean(data = Data,
var = "Steps",
group = c("Teacher", "Sex"),
conf = 0.95,
digits = 3,
R = 10000,
boot = TRUE,
traditional = FALSE,
normal = FALSE,
basic = FALSE,
percentile = FALSE,
bca = TRUE)
61
CONFIDENCE INTERVALS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
mean of x
7692.308
library(DescTools)
MeanCI(Data$Steps,
conf.level=0.95)
library(Rmisc)
CI(Data$Steps,
ci=0.95)
library(Rmisc)
group.CI(Steps ~ Sex,
data=Data,
ci = 0.95)
62
CONFIDENCE INTERVALS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Bootstrapped confidence intervals can be more reliable than those determined by the traditional
method for certain data sets. For more details on the different types of bootstrapped confidence
intervals, see the Carpenter and Bithel article in the “References” section of this chapter. The BCa
method (bias corrected, accelerated) is often cited as the best method, and the percentile method is
also cited as typically good.
The boot package can calculate confidence intervals for means by bootstrap. In the boot function, R
indicates the number of re-samplings.
The function groupwiseMean in the rcompanion package allows for calculating confidence intervals for
means for grouped data, using the bootstrap procedures from the boot package.
Note that for bootstrap procedures, your results may vary slightly from the results reported here.
library(boot)
Mboot = boot(Data$Steps,
function(x,i) mean(x[i]),
R=10000)
mean(Mboot$t[,1])
7693.381
boot.ci(Mboot,
conf = 0.95,
type = c("norm", "basic" ,"perc", "bca")
)
Intervals :
Level Normal Basic
95% (7208, 8174 ) (7192, 8154 )
Boot
hist(Boot$t[,1],
col = "darkgray")
63
CONFIDENCE INTERVALS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Required readings
[Video] “Understanding Confidence Intervals: Statistics Help” from Statistics Learning Center. (Dr.
Nic). 2013. www.youtube.com/watch?v=tFWsuO9f74o.
Optional readings
[Video] “Calculating the Confidence interval for a mean using a formula” from Statistics Learning
Center. (Dr. Nic). 2013. www.youtube.com/watch?v=s4SRdaTycaw.
“Confidence intervals” , Chapter 4.2 in Diez, D.M., C.D. Barr , and M. Çetinkaya-Rundel. 2012.
OpenIntro Statistics, 2nd ed. www.openintro.org/.
References
Carpenter, J. and J. Bithel. 2000. “Bootstrap confidence intervals: when, which, what? A practical guide
for medical statisticians”. Statistics in Medicine 19:1141–1164.
www.tau.ac.il/~saharon/Boot/10.1.1.133.8405.pdf.
Cumming, G. and Finch, S. 2005. Inference by Eye:Confidence Intervals and How to Read Pictures of
Data. American Psychologist 60:170-180.
Optional analyses
Bacteria = c(20, 40, 50, 60, 100, 120, 150, 200, 1000)
library(Rmisc)
exp(CI(log(Bacteria), ci=0.95))
64
CONFIDENCE INTERVALS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
B 100
B 120
B 210
B 300
B 420
B 400
B 500
B 800
B 4000
C 10
C 30
C 40
C 60
C 110
C 100
C 160
C 210
C 1200
")
Data = read.table(textConnection(Input),header=TRUE)
library(rcompanion)
groupwiseGeometric(data = Data,
var = "Bacteria",
group = "Site", ### can use e.g. c("V1", "V2")
digits = 3,
na.rm = TRUE)
65
CONFIDENCE INTERVALS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Exercises D
What is the 95% confidence interval for Steps for females (traditional method)?
3. Looking at the 95% confidence intervals for Steps for males and females, are we justified in claiming
that the mean Steps was statistically different for females and males? Why?
4. As part of a nutrition education program, extension educators had students keep diaries of what
they ate for a day and then calculated the calories students consumed.
Student Teacher Sex Calories Rating
a Tetsuo male 2300 3
b Tetsuo female 1800 3
c Tetsuo male 1900 4
d Tetsuo female 1700 5
e Tetsuo male 2200 4
f Tetsuo female 1600 3
g Tetsuo male 1800 3
h Tetsuo female 2000 3
i Kaneda male 2100 4
j Kaneda female 1900 5
k Kaneda male 1900 4
l Kaneda female 1600 4
m Kaneda male 2000 4
n Kaneda female 2000 5
o Kaneda male 2100 3
p Kaneda female 1800 4
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
d. What is the 95% confidence interval for Calories for females (traditional method)?
66
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Basic Plots
Packages used in this chapter
The following commands will install these packages if they are not already installed:
if(!require(lattice)){install.packages("lattice")}
if(!require(plyr)){install.packages("plyr")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(FSA)){install.packages("FSA")}
if(!require(DescTools)){install.packages("DescTools")}
if(!require(vcd)){install.packages("vcd")}
if(!require(rcompanion)){install.packages("rcompanion")}
There are several reasons why it is important to be able to produce and interpret plots of data.
Visualize your data. Plots are essential for visualizing and exploring your data. Plotting a histogram
gives a sense of the range, center, and shape of the data. It shows if the data is symmetric, skewed,
bimodal, or uniform. Box plots and plots of means, medians, and measures of variation visually
indicate the difference in means or medians among groups. Scatter plots show the relationship
between two measured variables.
Communicate your results to others. Being able to choose and produce appropriate plots is important
for summarizing your data for professional or extension presentations and journal articles. Plots can
be very effective for summarizing data in a visual manner and can show statistical results with impact.
Understand other’s results. It is essential to understand the plots you find in extension and
professional literature.
67
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For some good advice on producing high-quality plots, read the “General tips” section of the McDonald
page listed in the “Optional readings I”.
I strive for clear, bold text, lines and points. In a presentation, it can be difficult for the audience to
understand busy plots, or see thin text, lines, or subtle distinctions of colors. Publications may or may
not accept color in plots, and subtle shading or hatching may not be preserved. Sometimes the
resolution of images of plots are downsampled, making small text or symbols unclear.
In general, a plot and its caption should more-or-less be able to stand alone. Obviously, you can’t
describe your whole study in a caption, but it is helpful to give enough information so that the figure is
informative if it were separated from the rest of the article or presentation. The amount of
information will be tempered by the style of medium you’re writing for, journal versus factsheet, for
example. In a scientific article, you may wish to include the subjects (“students in seventh grade who
had completed a 4-H program in robotics”), location, time period, and other relevant information.
Be sure to label axes as thoroughly as possible, and also accurately. For example, an “increase in
student scores” is not the same as “student scores.” Indicate if the values are cumulative, total,
monthly, etc.
When possible, plots should show some measures of variation, such as standard deviation, standard
error of the mean, or confidence interval. By their nature, histograms and box plots show the variation
in the data. Plots of means or medians will need to have error bars added to show dispersion or
confidence limits.
If applicable, usually the x-axis is the independent variable and the y-axis is the dependent variable of
an analysis.
Misleading plots
You should also avoid making misleading plots. Remember that you are trying to accurately
summarize your data and show the relationships among variables. It takes care to do this accurately
and in a way that won’t confuse or mislead your reader.
The “Optional analyses” section on “Misleading and disorienting plots” near the end of this chapter
gives some examples of plots I’ve seen that display their shortcomings.
68
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Sometimes it is easier to produce plots using software with a graphic user interface like Microsoft
Excel, Apple Numbers, or free software like LibreOffice Calc.
In general, you should use whichever tool will best serve your purposes.
Exporting plots
RStudio makes exporting plots from R relatively easy. For lower-resolution images, plots can be
exported directly as image files like .jpg or .png. For higher-resolution images, images can be exported
as .pdf. See the Mangiafico reference in the “Optional readings I” section for some brief instructions.
Also, see “Exercises A” in this book for code on exporting higher resolution image files.
There are many types of plots, and this chapter will present only a few of the most common ones. In
later chapters, variants of these plots will be used to explore the data or to check the assumptions of
the analyses.
The type of plot you choose will depend upon on the number and types of variables you are trying to
present, and what you are trying to show.
For this example, extension educators had students wear pedometers to count their number of steps
over the course of a day. The following data are the result. Rating is the rating each student gave
about the usefulness of the program, on a 1-to-10 scale.
Note the code that orders the levels of the factor variables. In general, R will alphabetize levels of
factors, and this may be reflected on plots. The code will order the factor by the order that the levels
are encountered in the dataset.
Input = ("
Student Sex Teacher Steps Rating
a female Catbus 8000 7
b female Catbus 9000 10
c female Catbus 10000 9
d female Catbus 7000 5
e female Catbus 6000 4
f female Catbus 8000 8
g male Catbus 7000 6
h male Catbus 5000 5
i male Catbus 9000 10
j male Catbus 7000 8
k female Satsuki 8000 7
l female Satsuki 9000 8
m female Satsuki 9000 8
69
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
Data$Sex = factor(Data$Sex,
levels=unique(Data$Sex))
Data$Teacher = factor(Data$Teacher,
levels=unique(Data$Teacher))
Data
str(Data)
summary(Data)
rm(Input)
Histogram
A histogram shows the frequency of observations broken down into ranges, to give a sense of the
range and distribution of the data.
• If you want to make a histogram for counts of a categorical variable in R, you will want to use
a bar plot.
70
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Note that col defines the colors of the bars; main defines the main plot title; xlab defines the label on
the x-axis.
Simple histogram
hist(Data$Steps,
col="gray",
main="",
xlab="Steps")
The function plotDensityHistogram in the rcompanion package will produce this plot. Options include
the options for the hist function as well as adjust, bw, and kernel, which is passed to the density
function.
library(rcompanion)
plotDensityHistogram(Data$Steps,
adjust = 1) ### Decrease this number
### to make line less smooth
71
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Histogram of the number of steps taken by students with the smoothed gaussian
kernel density shown as a black curve.
The function plotNormalHistogram in the rcompanion package will produce this plot. Options include
the options for the hist function as well as linecol for the line color.
library(rcompanion)
plotNormalHistogram(Data$Steps)
72
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(lattice)
73
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Histograms of the number of steps taken by male and female students in each of
the classes of Catbus, Satsuki, and Totoro.
Box plots
Box plots graphically present the five-number summary. The main box indicates the 25th and 75th
percentile of the plotted data. The median is indicated with a heavy line through the box. The
whiskers extending from the box indicate the range of the data, but different software packages may
use somewhat different statistics for the extent of the whiskers. For this reason, when you are
presenting box plots, you should always specify what the length of whiskers indicate.
By default, the whiskers in the boxplot function indicate the range of the data, unless there are data
that are further away from the box than 1.5 times the length of the box. In this case, the whiskers
extend only 1.5 times the length of the box, and points beyond this distance are indicated with circles.
An example of box plots with circles indicating outlying values is shown in the “Box plot for one-way
data” section. If you wish to have the whiskers extend to the range of the data, and not display any
point with circles, the range=0 option can be used. See ?boxplots and ?boxplot.stats for more details.
The video from Statistics Learning Center in the “Optional readings II” section may be a helpful
introduction to box plots. The “Graphing Distributions” chapter in the Lane textbook in the “Optional
readings II” section also has a nice description of box plots.
• A single box plot is used for one interval/ratio or ordinal variable. Don’t use means for
ordinal data.
• Multiple box plots can be put on one plot to compare among groups.
74
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Note that xlab defines the label on the x-axis; ylab defines the label on the y-axis
Box plot of the number of steps taken by students. The box indicates the 1st and
3rd quartiles, and the dark line indicates the median. Whiskers extend to the
maximum and minimum of the data.
boxplot(Data$Steps,
ylab="Steps",
xlab="")
points(M,
col="red",
pch="+", ### symbol to use, see ?points
cex=2) ### size of symbol
Box plot of the number of steps taken by students. The box indicates the 1st and
3rd quartiles, and the dark line indicates the median. The mean is indicated
with the red cross. Whiskers extend to the maximum and minimum of the data.
75
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Box plots of the number of steps taken by male and female students. The boxes
indicate the 1st and 3rd quartiles, and the dark lines indicate the median.
Points more than 1.5 times the inter-quartile range away from the box are shown
with hollow circles.
boxplot(Steps ~ Sex,
data=Data,
ylab="Steps")
points(M,
col="red",
pch="+",
cex=2)
Box plots of the number of steps taken by male and female students. The boxes
indicate the 1st and 3rd quartiles, and the dark lines indicate the median. The
76
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
means are indicated with red crosses. Points more than 1.5 times the inter-
quartile range away from the box are shown with hollow circles.
Box plots of the number of steps taken by male and female students in each class.
The boxes indicate the 1st and 3rd quartiles, and the dark lines indicate the
median. Points more than 1.5 times the inter-quartile range away from the box
are shown with hollow circles.
M = tapply(Data$Steps,
INDEX = Data$Teacher : Data$Sex,
FUN = mean)
points(M,
col="red",
pch="+",
cex=2)
77
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Box plots of the number of steps taken by male and female students in each class.
The boxes indicate the 1st and 3rd quartiles, and the dark lines indicate the
median. The means are indicated with red crosses. Points more than 1.5 times
the inter-quartile range away from the box are shown with hollow circles.
For two-way data, these plots are sometimes called interaction plots.
• Each mean or median represents one group or treatment for an interval/ratio or ordinal
variable. Error bars are associated with each mean or median. Don’t use means for ordinal
data.
• Multiple means or medians can be plotted on the same plot, with groups from one or two
independent variables.
The package ggplot2 will be used for this type of plot. A simple interaction plot can be made with the
qplot function, and more refined plots can be made with the ggplot function.
The ggplot2 package is very powerful and flexible for making plots. The tradeoff is that the grammar
can be difficult to understand. When trying to get a plot to look a certain way, it may be necessary to
find an example plot online, and follow the code closely. Good resources include those by Hadley
Wickham, Winston Chang, and Robbins and Robbins in the “References” section.
78
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(rcompanion)
Sum
library(ggplot2)
qplot(x = Sex ,
y = Mean,
data = Sum) +
geom_errorbar(aes(
ymin = Trad.lower,
ymax = Trad.upper,
width = 0.15))
Plot of mean steps taken versus sex, for students in the classes of Catbus,
Satsuki, and Totoro. Error bars indicate the traditional 95% confidence
intervals for the mean.
79
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The code invokes the black-and-white visual theme with the option theme_bw.
library(rcompanion)
Sum
library(ggplot2)
ylab("Mean steps")
80
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Plot of mean steps taken versus sex, for students in the classes of Catbus,
Satsuki, and Totoro. Error bars indicate the traditional 95% confidence
intervals for the mean.
We will use the Summarize function to produce the data frame Sum, and will then add a variable se to
the data frame for the standard error.
Note that the default color palette of ggplot uses a pinkish color for the first group in the legend and a
blueish color for the second. I wasn’t intentionally matching females and males with their traditional
colors. These colors could be changed to avoid the appearance of gender essentialism.
library(FSA)
### This creates a new variable for standard error in our summary data frame
### Replace n with nvalid if there are missing values
Sum
81
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
4 Catbus male 4 4 7000 1632.9932 5000 6500 7000 7500 9000 0 816.4966
5 Satsuki male 3 3 7000 1000.0000 6000 6500 7000 7500 8000 0 577.3503
6 Totoro male 4 4 7000 816.4966 6000 6750 7000 7250 8000 0 408.2483
library(ggplot2)
qplot(x = Teacher,
y = mean,
color = Sex,
data = Sum) +
Interaction plot of mean steps taken versus teacher for males and females. Error
bars indicate the standard error of the mean.
### This creates a new variable for standard error in our summary data frame
82
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Sum
library(ggplot2)
geom_point(shape = 15,
size = 4,
position = pd) +
ylab("Mean steps")
83
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Interaction plot of mean steps taken versus teacher for males and females. Error
bars indicate the standard error of the mean.
• Each bar represents one group mean or median for an interval/ratio or ordinal variable.
Error bars are associated with each mean or median. Don’t use means for ordinal data.
• Multiple means or medians can be plotted on the same plot, with groups from one or two
independent variables.
The barplot function can produce a simple bar plot, from a table of numbers. For more complex bar
plots, or to add error bars, the ggplot2 package can be used.
Sum
84
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Table = as.table(Sum$mean)
rownames(Table) = Sum$Sex
Table
female male
8200 7000
barplot(Table,
ylab="Mean steps",
xlab="Sex")
Bar plot of the mean steps taken by female and male students.
Sum
85
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(ggplot2)
qplot(x = Sex ,
y = Mean,
data = Sum) +
geom_bar(stat ="identity",
fill = "gray") +
geom_errorbar(aes(ymin = Trad.lower,
ymax = Trad.upper,
width = 0.15))
Bar plot of the mean steps taken by female and male students. Error bars
indicate traditional 95% confidence intervals of the means.
Note that the default color palette of ggplot uses a pinkish color for the first group in the legend and a
blueish color for the second. These colors can be changed.
library(FSA)
### This creates a new variable for standard error in our summary data frame
### Replace n with nvalid if there are missing values
Sum
86
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(ggplot2)
geom_bar(stat = "identity",
color = "black",
position = pd) +
theme_bw() +
theme(axis.title = element_text(face = "bold")) +
ylab("Mean steps")
87
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Bar plots of the mean steps taken by female and male students in each class.
Error bars indicate the standard error of the means.
Scatter plot
A scatter plot is used for bivariate data, to show the relationship between two interval/ratio or ordinal
variables. Each point represents a single observation with one measured variable on the x-axis, and
one measured variable is y-axis.
• Multiple plots can be shown together to investigate the relationships among multiple
measured variables. (Not shown in this chapter.)
88
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Plots for nominal data show the counts of observation for each category of the variable.
89
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
XT
Teacher
Sex Catbus Satsuki Totoro
female 6 4 5
male 4 3 4
barplot(XT,
beside = TRUE,
legend = TRUE,
ylim = c(0, 8), # adjust to remove legend overlap
xlab = "Teacher",
ylab = "Count")
Bar plot for frequencies of observations for female and male students in each
class.
It’s not necessary you understand the code completely, but in order to demonstrate error bars on this
plot, 95% confidence intervals for the counts will be produced with the MultinomialCI function in the
DescTools package. We call the results of this function MCI. These results will then be converted from
proportions to counts and added to the Sum data frame.
We will use the scale_fill_manual option at the end of the code to change the fill on the bars to grays.
90
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Note that this example uses two-way data in a data frame, and extracts the count of observations from
the Summarize function. A similar plot, using one-way data, and starting with a vector of counts, is
shown in the “Multinomial test example with plot and confidence intervals” in the Goodness-of-Fit Tests
for Nominal Variables chapter.
library(FSA)
library(DescTools)
MCI = MultinomCI(Sum$n,
conf.level=0.95)
Total = sum(Sum$n)
### The few lines above create new variables in the data frame Sum
## for the multinomial confidence intervals for n
Sum
Teacher Sex n nvalid mean sd min Q1 median Q3 max percZero lower upper
1 Catbus female 6 6 8000 1414.2136 6000 7250 8000 8750 10000 0 2 11.27107
2 Satsuki female 4 4 8500 577.3503 8000 8000 8500 9000 9000 0 0 9.27107
3 Totoro female 5 5 8200 1483.2397 6000 8000 8000 9000 10000 0 1 10.27107
4 Catbus male 4 4 7000 1632.9932 5000 6500 7000 7500 9000 0 0 9.27107
5 Satsuki male 3 3 7000 1000.0000 6000 6500 7000 7500 8000 0 0 8.27107
6 Totoro male 4 4 7000 816.4966 6000 6750 7000 7250 8000 0 0 9.27107
library(ggplot2)
pd = position_dodge(0.5) ### How much of a gap for the bars on the plot,
### 1 for non-overlapping bars
geom_bar(stat = "identity",
color = "black",
position = pd) +
geom_errorbar(aes(ymin = lower,
ymax = upper),
width = 0.2,
size = 0.7,
position = pd,
color = "black"
91
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
) +
theme_bw() +
theme(axis.title = element_text(face = "bold")) +
scale_fill_manual(values= c("gray80","gray50","green")) +
ylab("Count of observations")
Bar plots of the counts of observations by female and male students in each class.
Error bars indicate the 95% multinomial confidence intervals for each count (Sison
and Glaz method).
Mosaic plot
A mosaic plot can be constructed with the mosaic function in the vcd package.
Interpretation of mosaic plots is discussed in the Introduction to Tests for Nominal Variables chapter.
library(vcd)
92
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Mosaic plot for frequencies of observations for female and male students in each
class.
The order in which factor variable levels are plotted can often be controlled by ordering the levels of
the factor, for example,
Data$Teacher = factor(Data$Teacher,
levels=c("Totoro", "Catbus", "Satsuki"))
levels(Data$Teacher)
If you are going to be using other variables from the data frame, for example for error bars or mean
separation letters, it is sometimes best to reorder the whole data frame according to the factor
variable you want to use, and then reorder the levels of that variable as well. Sometimes this is
overkill, but it prevents any mismatching of the observations from the different variables.
library(rcompanion)
Sum
93
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Sum = Sum[order(factor(Sum$Teacher,
levels= c("Catbus", "Satsuki", "Totoro"))),]
Sum$Teacher = factor(Sum$Teacher,
levels= c("Catbus", "Satsuki", "Totoro"))
Sum
The following plots are histograms of water quality data for U.S. Geological Survey station 1467016,
Rancocas Creek at Willingboro, NJ, from approximately 1970 to 1974 (USGS, 2015).
The distribution of pH follows a normal distribution relatively well. It is rather symmetric around a
mean of about 6. A normal curve with the same mean and standard deviation as the data has been
superimposed as a blue line.
The distribution of conductance shows a slight right skew, but otherwise is relatively symmetric
around a mean of about 120.
I would describe the distribution of dissolved oxygen as bimodal. It seems to have relatively many
observations clustered around a value of 3 and around a value of 9.
I’m not sure how I would describe the distribution of temperature. If pressed I might say it is relatively
uniform. A histogram of a uniform distribution would have all bars of equal height. You might see
temperature as bimodal, or of indescribable shape.
Results from the describe function in the psych package are shown below the histograms, and kernel
density plots of these distributions are shown below those results.
For examples of skewed histograms, see the “Skewness” sub-section of the “Statistics of shape for
interval/ratio data” section in the Descriptive Statistics chapter.
94
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
### describe(pH)
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 366 6.02 0.24 6 6.01 0.15 5.2 6.7 1.5 0.11 0.84 0.01
### describe(Conductance)
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 366 121.95 14.08 121 121.33 13.34 94 162 68 0.38 -0.26 0.74
### describe(DO)
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 354 6.32 3.43 6.35 6.25 4.82 0.5 12.4 11.9 0.09 -1.42 0.18
### describe(Temperature)
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 366 14.48 7.98 14.55 14.54 11.64 1.5 27.1 25.6 0.02 -1.43 0.42
95
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The larger complaint I have is that there are no measures of variation indicated for the medians. As
presented, one might say, “Blacks have a higher median salary than do Whites, whose salary is higher
still than that of Hispanics.” But I suspect that the variation within each demographic category is much
greater than the variation between them. If we had error bars on each bar, we might be less inclined
to think that there is a real (statistically significant) difference among groups. Of course, it’s
impossible to draw any conclusions without their including measures of variation or statistical
analyses.
As a final note, usually if there is no statistical difference among groups, there is no need to plot their
values separately. In the example of this plot, if there were no statistical difference among racial
groups, we could just report one median for the whole lot, with a five-number summary, or a single
box plot. There are exceptions to this; for example, in this case if salaries among racial groups were of
some particular interest, we might show statistics for each group even if there were no statistical
differences.
96
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Original source: American Association of University Professors. 2013. Annual Report on the Economic
Status of the Profession, 2012–2013. www.aaup.org/13EconomicStatus.
The main deficiency of the plot on the left is that the bottom of the y-axis doesn’t represent zero, but
5.75 million, making the difference in the bars look large compared with the size of first bar. For good
measure, units on the y-axis are left off.
The plot on the right more honestly depicts the relationship between the difference in the bars and the
size of the bars by setting the bottom of the y-axis to zero. Also, units are included on the y-axis.
97
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The original plot had the confusing heading and sub-heading, “Gun deaths in Florida: number of
murders committed using firearms”. Of course, murders aren’t the same as gun deaths, so it’s not clear
what is being measured on the y-axis.
A quick look at the plot would suggest that gun murders dropped after 2005. But a more careful
inspection reveals that the y-axis is inverted, in that smaller numbers are on the top and larger
numbers are on the bottom. Therefore, gun murders went up after 2005!
At first I found it difficult to believe that this plot could have been made in good faith. Most commonly
we put larger numbers toward the top of the y-axis. I suspect, though, that the author was using a less-
common convention that positive outcomes are put toward the top of the y-axis. It makes sense if you
think of zeros murders as the goal, and increasing murders are further below that goal. An example of
this convention is the Hickey article in the “Reference” the end of the section. In that article, actors
with a low billing score are at the top of the y-axis, since first billing is the best.
Reuters did release a second plot with the y-axis in its normal orientation, which is mimicked in the
second plot.
As a final note, there are cases where the y-axis is traditionally inverted. For example when plotting
the depth to water table in groundwater hydrology, the surface (0 depth) is plotted at the top, so as the
depth increases, the value is vertically lower on the plot.
98
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Original source: Engel, P. 2014. This chart shows an alarming rise in Florida gun deaths after 'Stand
Your Ground' was enacted. Business Insider. www.businessinsider.com/gun-deaths-in-florida-
increased-with-stand-your-ground-2014-2.
Reference: Hickey, W. 2015. Congratulations, You’ve Been Cast In Star Wars! Will You Ever Work
Again? Five Thirty Eight. fivethirtyeight.com/features/congratulations-youve-been-cast-in-star-wars-
will-you-ever-work-again/.
99
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
But I don’t think there’s any good reason to suspect that life expectancy would continue to increase as
health care spending increases, beyond some limit. Life expectancy is probably limited by human
biology, limits in medical science, and intractable human behaviors. Looking at just the data points
and ignoring the blue curve, the data suggest to me that life expectancy increases as spending
increases up to about 2500 dollars (about where Israel, ISR, is) and then plateaus. Spending beyond
this critical value results in no further increase in life expectancy.
I created the model in the second plot, which uses a quadratic plateau model. With this model, a
quadratic curve rises to a plateau. This model predicts a critical level of about 3500 dollars, about
where Japan, JPN, is. It also suggests that life expectancy in the United States is lower than the model
predicts, but not much more than Switzerland’s, CHE, or Japan’s is above it. But more importantly, this
model and other similar alternatives predict a critical value in healthcare spending, above which we
should not expect an increase in life expectancy. Of course, there are other factors that would affect
life expectancy, and maybe spending above this critical value is warranted in some cases.
The third plot uses a linear plateau model, which is composed of a linear increasing segment and a
plateau segment. It fits the data slightly better than the quadratic plateau model, and indicates a
critical level near 2500 dollars.
100
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
101
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
References
Robbins, N.B. and J. Robbins. 2016. Effective graphs with Microsoft R Open. www.joyce-
robbins.com/wp-content/uploads/2016/04/effectivegraphsmro1.pdf.
[USGS] U.S. Geological Survey. 2015. National Water Information System: Web Interface.
waterdata.usgs.gov/nwis/.
Optional readings I
“General tips for all graphs” in “Guide to fairly good graphs” in McDonald, J.H. 2014. Handbook of
Biological Statistics. www.biostathandbook.com/graph.html.
“Exporting graphics” in “A Few Notes to Get Started with R” in Mangiafico, S.S. 2015. An R Companion
for the Handbook of Biological Statistics, version 1.09. rcompanion.org/rcompanion/a_06.html.
Optional readings II
[Video] “Understanding summary statistics: The box plot” from Statistics Learning Center (Dr. Nic).
2015. www.youtube.com/watch?v=bhkqq0w60Gc.
“Graphing Distributions”, Chapter 2 in Lane, D.M. (ed.). No date. Introduction to Statistics, v. 2.0.
onlinestatbook.com/. (Stem and Leaf Displays, Histograms, Frequency Polygons, Box Plots, Bar Charts,
Line Graphs, Dot Plots.)
102
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
“Scatterplots for paired data” , Chapter 1.6.1 in Diez, D.M., C.D. Barr , and M. Çetinkaya-Rundel. 2012.
OpenIntro Statistics, 2nd ed. www.openintro.org/.
“Dot plots and the mean” , Chapter 1.6.2 in Diez, D.M., C.D. Barr , and M. Çetinkaya-Rundel. 2012.
OpenIntro Statistics, 2nd ed. www.openintro.org/.
“Histograms and shape” , Chapter 1.6.3 in Diez, D.M., C.D. Barr , and M. Çetinkaya-Rundel. 2012.
OpenIntro Statistics, 2nd ed. www.openintro.org/.
“Box plots, quartiles, and the median” , Chapter 1.6.5 in Diez, D.M., C.D. Barr , and M. Çetinkaya-
Rundel. 2012. OpenIntro Statistics, 2nd ed. www.openintro.org/.
“Contingency tables and bar plots” , Chapter 1.7.1 in Diez, D.M., C.D. Barr , and M. Çetinkaya-Rundel.
2012. OpenIntro Statistics, 2nd ed. www.openintro.org/.
“Segmented bar and mosaic plot” , Chapter 1.7.3 in Diez, D.M., C.D. Barr , and M. Çetinkaya-Rundel.
2012. OpenIntro Statistics, 2nd ed. www.openintro.org/.
Exercises E
1. Look at the histogram of the steps taken by male and female students. Would you describe the
distributions of each as symmetrical, skewed, bimodal, or uniform?
2. Look at the box plot of the steps taken by all students. Estimate the minimum, 25th percentile,
median, 75th percentile, and maximum.
3. In box plots including the mean, a symmetrical distribution will have the mean close to the median,
and the box will be symmetrical on either side of the median line. Looking at the box plot of the steps
taken by female and male students, would you describe the distributions of each as symmetrical or
skewed?
4. Looking at the plot of mean steps for female and male students, estimate the mean of each.
5. It is justified to say that group means with non-overlapping 95% confidence intervals are
statistically different. Looking at the plot of mean steps with confidence intervals for female and male
students, are the two means statistically different?
6. Looking at the interaction plot for mean steps for female and male students by each class, do the
error bars for standard error overlap or not for each teacher?
7. Look at the scatter plot. Does it look like there is a correlation or relationship between Steps and
Rating, or not? If so, how would you describe it?
8. Look at any of the plots for nominal data. Are there more females or males in these classes? In
which class is the greatest disparity?
103
BASIC PLOTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
9–11. As part of a nutrition education program, extension educators had students keep diaries of what
they ate for a day and then calculated the calories students consumed.
Student Teacher Sex Calories Rating
a Tetsuo male 2300 3
b Tetsuo female 1800 3
c Tetsuo male 1900 4
d Tetsuo female 1700 5
e Tetsuo male 2200 4
f Tetsuo female 1600 3
g Tetsuo male 1800 3
h Tetsuo female 2000 3
i Kaneda male 2100 4
j Kaneda female 1900 5
k Kaneda male 1900 4
l Kaneda female 1600 4
m Kaneda male 2000 4
n Kaneda female 2000 5
o Kaneda male 2100 3
p Kaneda female 1800 4
For each of the following, answer the question, and show the plot or other output from the analyses
you used to answer the question.
a. Write a caption for this plot. Submit the plot and caption.
b. Would you describe the distribution of each as symmetrical, skewed, bimodal, or uniform?
10. Produce box plots of the calories consumed for males and females.
a. Write a caption for this plot. Submit the plot and caption.
b. Report the 25th percentile, median, and 75th percentile. (Estimate from the plot, or report
more precise statistics.)
11. Plot means of the calories consumed for males and females. Include error bars for either
confidence intervals or standard error of the means. You can use either qplot or ggplot, and you can
use either the bar chart style or the "plot of means" style.
a. Write a caption for this plot. Submit the plot and caption.
104
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Most of what we’ve covered in this book so far is about producing descriptive statistics: calculating
means and medians, plotting data in various ways, and producing confidence intervals. The bulk of the
rest of this book will cover statistical inference: using statistical tests to draw some conclusion about
the data. We’ve already done this a little bit in earlier chapters by using confidence intervals to
conclude if means are different or not among groups.
As Dr. Nic mentions in her article in the “References and further reading” section, this is the part where
people sometimes get stumped. It is natural for most of us to use summary statistics or plots, but
jumping to statistical inference needs a little change in perspective. The idea of using some statistical
test to answer a question isn’t a difficult concept, but some of the following discussion gets a little
theoretical. The video from the Statistics Learning Center in the “References and further reading”
section does a good job of explaining the basis of statistical inference.
The most important thing to gain from this chapter is an understanding of how to use the p-value,
alpha, and decision rule to test the null hypothesis. But once you are comfortable with that, you will
want to return to this chapter to have a better understanding of the theory behind this process.
Hypothesis testing
The alternative hypothesis is the contrary of the null hypothesis, and so describes the cases where
there is a difference among groups or a correlation between two variables, etc.
Notice that the definitions of null hypothesis and alternative hypothesis have nothing to do with what
you want to find or don't want to find, or what is interesting or not interesting, or what you expect to
find or what you don’t expect to find. If you were comparing the height of men and women, the null
hypothesis would be that the height of men and the height of women were not different. Yet, you
might find it surprising if you found this hypothesis to be true for some population you were studying.
Likewise, if you were studying the income of men and women, the null hypothesis would be that the
income of men and women are not different, in the population you are studying. In this case you might
be hoping the null hypothesis is true, though you might be unsurprised if the alternative hypothesis
were true. In any case, the null hypothesis will take the form that there is no difference between
groups, there is no correlation between two variables, or there is no effect of this variable in our
model.
105
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
p-value definition
Most of the tests in this book rely on using a statistic called the p-value to evaluate if we should reject,
or fail to reject, the null hypothesis.
Given the assumption that the null hypothesis is true, the p-value is defined as the probability of
obtaining a result equal to or more extreme than what was actually observed in the data.
Decision rule
The p-value for the given data will be determined by conducting the statistical test.
This p-value is then compared to a pre-determined value alpha. Most commonly, an alpha value of
0.05 is used, but there is nothing magic about this value.
If the p-value for the test is less than alpha, we reject the null hypothesis.
If the p-value is greater than or equal to alpha, we fail to reject the null hypothesis.
This is what we call a two-sided test, since we are testing both extremes suggested by our data:
getting 95 or greater heads or getting 95 or greater tails. In most cases we will use two sided tests.
You can imagine that the p-value for this data will be quite small. If the null hypothesis is true, and the
coin is fair, there would be a low probability of getting 95 or more heads or 95 or more tails.
(Actually, R reports it as < 2.2e-16, which is shorthand for the number in scientific notation, 2.2 x 10-16,
which is 0.00000000000000022, with 15 zeros after the decimal point.)
If we assume an alpha of 0.05, since the p-value is less than alpha, we reject the null hypothesis. That
is, we conclude that the coin is not fair.
106
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
In our example each classroom will have 10 students. The data is arranged into a contingency table.
Classroom Passed Failed
A 8 2
B 3 7
We will use Fisher’s exact test to test if there is an association between Classroom and the counts of
passed and failed students. The null hypothesis is that there is no association between Classroom and
Passed/Failed, based on the relative counts in each cell of the contingency table.
Input =("
Classroom Passed Failed
A 8 2
B 3 7
")
Matrix = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
Matrix
Passed Failed
A 8 2
B 3 7
fisher.test(Matrix)
p-value = 0.06978
The reported p-value is 0.070. If we use an alpha of 0.05, then the p-value is greater than alpha, so we
fail to reject the null hypothesis. That is, we did not have sufficient evidence to say that there is an
association between Classroom and Passed/Failed.
More extreme data in this case would be if the counts in the upper left or lower right (or both!) were
greater.
Classroom Passed Failed
A 9 1
B 3 7
107
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
In most cases we would want to consider as "extreme" not only the results when Classroom A has a
high frequency of passing students, but also results when Classroom B has a high frequency of passing
students. This is called a two-sided or two-tailed test. If we were only concerned with one classroom
having a high frequency of passing students, relatively, we would instead perform a one-sided test.
The default for the fisher.test function is two-sided, and usually you will want to use two-sided tests.
Classroom Passed Failed
A 2 8
B 7 3
In both cases, "extreme" means there is a stronger association between Classroom and Passed/Failed.
Given the assumption that the null hypothesis is true, the p-value is defined as the probability of
obtaining a result equal to or more extreme than what was actually observed in the data.
The astute reader might be asking herself, “If I’m trying to determine if the null hypothesis is true or
not, why would I start with the assumption that the null hypothesis is true? And why am I using a
probability of getting certain data given that a hypothesis is true? Don’t I want to instead determine
the probability of the hypothesis given my data?”
The answer is yes, we would like a method to determine the likelihood of our hypothesis being true
given our data, but we use the Null Hypothesis Significance Test approach since it is relatively
straightforward, and has wide acceptance historically and across fields.
108
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Of course, in practice we do use the results of the statistical tests to reach conclusions about the null
hypothesis. Technically, the p-value says nothing about the alternative hypothesis. But logically, if the
null hypothesis is rejected, then its logical complement, the alternative hypothesis, is supported.
Practically, this is how we handle significant p-values, though this practical approach generates
disapproval in some theoretical circles.
This is somewhat similar to the approach of a jury in a trial. The jury either finds sufficient evidence to
declare someone guilty, or fails to find sufficient evidence to declare someone guilty.
Failing to convict someone isn’t necessarily the same as declaring someone innocent. Likewise, if we
fail to reject the null hypothesis, we shouldn’t assume that the null hypothesis is true. It may be that
we didn’t have sufficient samples to get a result that would have allowed us to reject the null
hypothesis, or maybe there are some other factors affecting the results that we didn’t account for. This
is similar to an “innocent until proven guilty” stance.
Errors in inference
For the most part, the statistical tests we use are based on probability, and our data could always be
the result of chance. Considering the coin flipping example above, if we did flip a coin 100 times and
came up with 95 heads, we would be compelled to conclude that the coin was not fair. But 95 heads
could happen with a fair coin strictly by chance.
We can, therefore, make two kinds of errors in testing the null hypothesis:
• A Type I error occurs when the null hypothesis really is true, but based on our decision rule
we reject the null hypothesis. In this case, our result is a false positive; we think there is an
effect (unfair coin, association between variables, difference among groups) when really
there isn’t. The probability of making this kind error is alpha, the same alpha we used in our
decision rule.
• A Type II error occurs when the null hypothesis is really false, but based on our decision rule
we fail to reject the null hypothesis. In this case, our result is a false negative; we have failed
to find an effect that really does exist. The probability of making this kind of error is called
beta.
Reality
___________________________________
109
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Statistical power
The statistical power of a test is a measure of the ability of the test to detect a real effect. It is related
to the effect size, the sample size, and our chosen alpha level.
The effect size is a measure of how unfair a coin is, how strong the association between two variables,
or how large the difference among groups. As the effect size increases or as the number of
observations we collect increases, or as the alpha level decreases, the power of the test increases.
Statistical power in the table above is indicated by 1 – beta, which is the probability of correctly
rejecting the null hypothesis.
An example should make these relationship clear. Imagine we are sampling a class of 7th grade
students for their height. In reality, for this class, the girls are taller than the boys, but the difference is
small (that is the effect size is small), and there is a lot of variability in students’ heights. You can
imagine that in order to detect the difference between girls and boys that we would have to measure
many students. If we fail to sample enough students, we might make a Type II error. That is, we might
fail to detect the actual difference in heights between sexes.
If we had a different experiment with a larger effect size—for example the weight difference between
mature hamsters and mature hedgehogs—we might need fewer samples to detect the difference.
Note also, that our chosen alpha plays a role in the power of our test, too. All things being equal, across
many tests, if we decrease our alpha, that is to insist on a lower rate of Type I errors, we are more
likely to commit a Type II error, and so have a lower power. This is analogous to a case of a meticulous
jury that has a very high standard of proof to convict someone. In this case, the likelihood of a false
conviction is low, but the likelihood of a letting a guilty person go free is relatively high.
One situation in which the alpha level is increased is in preliminary studies in which it is better to
include potentially significant effects even if there is not strong evidence for keeping them. In this
case, the researcher is accepting an inflated chance of Type I errors in order to decrease the chance of
Type II errors.
Imagine an experiment in which you wanted to see if various environmental treatments would
improve student learning. In a preliminary study, you might have many treatments, with few
observations each, and you want to retain any potentially successful treatments for future study. For
example, you might try playing classical music, improved lighting, complimenting students, and so on,
110
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
and see if there is any effect on student learning. You might relax your alpha value to 0.10 or 0.15 in
the preliminary study to see what treatments to include in future studies.
On the other hand, in situations where a Type I, false positive, error might be costly in terms of money
or people’s health, a lower alpha can be used, perhaps, 0.01 or 0.001. You can imagine a case in which
there is an established treatment say for cancer, and a new treatment is being tested. Because the new
treatment is likely to be expensive and to hold people’s lives in the balance, a researcher would want
to be very sure that the new treatment is more effective than the established treatment. In reality, the
researchers would not just lower the alpha level, but also look at the effect size, submit the research
for peer review, replicate the study, be sure there were no problems with the design of the study or
the data collection, and weigh the practical implications.
In reality, though, 0.05 is almost always used in most fields for readers of this book. Choosing a
different alpha value will rarely go without question. It is best to keep with the 0.05 level unless you
have good justification for another value, or are in a discipline where other values are routinely used.
Practical advice
One good practice is to report actual p-values from analyses. It is fine to also simply say, e.g. “The
dependent variable was significantly correlated with variable A (p < 0.05).” But I prefer when possible
to say, “The dependent variable was significantly correlated with variable A (p = 0.026).
It is probably best to avoid using terms like “marginally significant” or “borderline significant” for p-
values less than 0.10 but greater than 0.05, though you might encounter similar phrases. It is better to
simply report the p-values of tests or effects in straight-forward manner. If you had cause to include
certain model effects or results from other tests, they can be reported as e.g., “Variables correlated
with the dependent variable with p < 0.15 were A, B, and C.”
For example, imagine we want to compare the SAT scores of two SAT preparation classes with a t-test.
111
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Class.A = c(1500, 1505, 1505, 1510, 1510, 1510, 1515, 1515, 1520, 1520)
Class.B = c(1510, 1515, 1515, 1520, 1520, 1520, 1525, 1525, 1530, 1530)
t.test(Class.A, Class.B)
mean of x mean of y
1511 1521
The p-value is reported as 0.003, so we would consider there to be a significant difference between the
two classes (p < 0.05).
But we have to ask ourselves the practical question, is a difference of 10 points on the SAT large
enough for us to care about? What if enrolling in one class costs significantly more than the other
class? Is it worth the extra money for a difference of 10 points on average?
For example, in the SAT example above, the p-value is fairly small, but the size of the effect (difference
between classes) in this case is relatively small (10 points, especially small relative to the range of
scores students receive on the SAT).
In converse, there could be a relatively large size of the effects, but if there is a lot of variability in the
data or the sample size is not large enough, the p-value could be relatively large.
In this example, the SAT scores differ by 100 points between classes, but because the variability is
greater than in the previous example, the p-value is not significant.
Class.C = c(1000, 1100, 1200, 1250, 1300, 1300, 1400, 1400, 1450, 1500)
Class.D = c(1100, 1200, 1300, 1350, 1400, 1400, 1500, 1500, 1550, 1600)
t.test(Class.A, Class.B)
mean of x mean of y
1290 1390 )
boxplot(cbind(Class.C, Class.D))
112
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
This simply follows mathematically from the definition of alpha. If we allow a probability of 0.05, or
5% chance, of making a Type I error for any one test, as we do more and more tests, the chances that at
least one of them having a false positive becomes greater and greater.
p-value adjustment
One way we deal with the problem of multiple p-values in statistical analyses is to adjust p-values
when we do a series of tests together (for example, if we are comparing the means of multiple groups).
In R, the software can adjust the p-values, instead of us changing in our minds our alpha value.
Unfortunately, students in analysis of experiments courses often learn to use Bonferroni adjustment
for p-values. This method is simple to do with hand calculations, but is excessively conservative in
most situations, and, in my opinion, antiquated.
There are other p-value adjustment methods, and the choice of which one to use is dictated either by
which are common in your field of study, or by doing enough reading to understand which are
statistically most appropriate for your application.
Preplanned tests
The statistical tests covered in this book assume that tests are preplanned for their p-values to be
accurate. That is, in theory, you set out an experiment, collect the data as planned, and then say “I’m
going to analyze it with kind of model and do these post-hoc tests afterwards”, and report these
results, and that’s all you would do.
113
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Some authors emphasize this idea of preplanned tests, but in reality, of course, researchers will often
collect data, and then use an exploratory data analysis approach to see what the data suggests before
heading into statistical analyses.
Sometimes the preplanned approach is appropriate. If an experiment is set out in a specific design,
then usually it is appropriate to use the analysis suggested by this design.
p-value hacking
It is important when approaching data from an exploratory approach, to avoid committing p-value
hacking. Imagine the case in which the researcher collects many different measurements across a
range of subjects. The researcher might be tempted to simply try different tests and models to relate
one variable to another, for all the variables. He might continue to do this until he found a test with a
significant p-value.
Because an alpha value of 0.05 allows us to make a false-positive error five percent of the time, finding
one p-value below 0.05 after several successive tests may simply be due to chance.
Some forms of p-value hacking are more egregious. For example, if one were to collect some data, run
a test, and then continue to collect data and run tests iteratively until a significant p-value is found.
Publication bias
A related issue in science is that there is a bias to publish, or to report, only significant results. This
can also lead to an inflation of the false-positive rate. As a hypothetical example, imagine if there are
currently 20 similar studies being conducted testing a similar effect—let’s say the effect of
glucosamine supplements on joint pain. If 19 of those studies found no effect and so were discarded,
but one study found an effect using an alpha of 0.05, and was published, is this really any support that
glucosamine supplements decrease joint pain?
Significant
xkcd.com/882/
Null hypothesis
xkcd.com/892
114
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
P-values
xkcd.com/1478/
Experimental designs
A true experimental design assigns treatments in a systematic manner. The experimenter must be
able to manipulate the experimental treatments and assign them to subjects. Since treatments are
randomly assigned to subjects, a casual inference can be made for significant results. That is, we can
say that the variation in the dependent variable is caused by the variation in the independent variable.
For interval/ratio data, traditional experimental designs can be analyzed with specific parametric
models, assuming other model assumptions are met. These traditional experimental designs include:
Quasi-experiment designs
Often a researcher cannot assign treatments to individual experimental units, but can assign
treatments to groups. For example, if students are in a specific grade or class, it would not be practical
to randomly assign students to grades or classes. But different classes could receive different
treatments (such as different curricula). Causality can be inferred cautiously if treatments are
randomly assigned and there is some understanding of the factors that affect the outcome.
Observational studies
In observational studies, the independent variables are not manipulated, and no treatments are
assigned. Surveys are often like this, as are studies of natural systems without experimental
manipulation. Statistical analysis can reveal the relationships among variables, but causality cannot be
inferred. This is because there may be other unstudied variables that affect the measured variables in
the study.
Sampling
Good sampling practices are critical for producing good data. In general, samples need to be collected
in a random fashion so that bias is avoided.
In survey data, bias is often introduced by a self-selection bias. For example, internet or telephone
surveys include only those who respond to these requests. Might there be some relevant difference in
the variables of interest between those who respond to such requests and the general population
being surveyed? Or bias could be introduced by the researcher selecting some subset of potential
subjects, for example only surveying a 4-H program with particularly cooperative students and
ignoring other clubs.
115
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
In election forecasting, good pollsters need to account for selection bias and other biases in the survey
process. For example, if a survey is done by landline telephone, those being surveyed are more likely
to be older than the general population of voters, and so likely to have a bias in their voting patterns.
But in general, it is much better to plan ahead and be consistent with measurements.
Consistency
People sometimes have the tendency to change measurement frequency or experimental treatments
during the course of a study. This inevitably causes headaches in trying to analyze data, and makes
writing up the results messy. Try to avoid this.
Including checks and controls helps with the analysis in a practical sense, since they serve as standard
treatments against which to compare the experimental treatments. In the case where the
experimental treatments have similar effects, controls and checks allow you say, for example, “Means
for the all experimental treatments were similar, but were higher than the mean for control, and lower
than the mean for check treatment.”
Include covariates
Including additional independent variables that might affect the dependent variable is often helpful in
an analysis. In an educational setting, you might assess student age, grade, school, town, background
level in the subject, or how well they are feeling that day.
The effects of covariates on the dependent variable may be of interest in itself. But also, including co-
variates in an analysis can better model the data, sometimes making treatment effects more clear or
making a model better meet model assumptions.
116
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
• p-values have properties that may be misleading, for example that they do not represent
effect size, and that they change with sample size.
Personally, I don’t find these to be very convincing arguments against the NHST approach.
The first complaint is in some sense pedantic: Like so many things, students and researchers learn the
definition of p-values at some point and then eventually forget. This doesn’t seem to impact the
usefulness of the approach.
The second point has weight only if researchers use only p-values to draw conclusions from statistical
tests. As this book points out, one should always consider the size of the effects and practical
considerations of the effects, as well present finding in table or graphical form, including confidence
intervals or measures of dispersion. There is no reason why parameter estimates, goodness-of-fit
statistics, and confidence intervals can’t be included when a NHST approach is followed.
The properties in the third point also don’t count much as criticism if one is using p-values correctly.
One should understand that it is possible to have a small effect size and a small p-value, and vice-versa.
This is not a problem, because p-values and effect sizes are two different concepts. We shouldn’t
expect them to be the same. The fact that p-values change with sample size is also in no way
problematic to me. It makes sense that when there is a small effect size or a lot of variability in the
data that we need many samples to conclude the effect is likely to be real.
(One case where I think the considerations in the preceding point are commonly problematic is when
people use statistical tests to check for the normality or homogeneity of data or model residuals. As
sample size increases, these tests are better able to detect small deviations from normality or
homoscedasticity. Too many people use them and think their model is inappropriate because the test
can detect a small effect size, that is, a small deviation from normality or homoscedasticity).
The fourth point is a good one. It doesn’t make much sense to come to one conclusion if our p-value is
0.049 and the opposite conclusion if our p-value is 0.051. But I think this can be ameliorated by
reporting the actual p-values from analyses. And at some point, one does sometimes have to have a
cutoff value.
Overall it seems to me that these complaints condemn poor practices that the authors observe: not
reporting the size of effects in some manner; not including confidence intervals or measures of
117
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
dispersion; basing conclusions solely on p-values; and not including important results like parameter
estimates and goodness-of-fit statistics.
This approach is used in a few spots in this book, including in the Confidence Intervals chapter and in
the Jerry Coyne example.
This approach can be very straightforward, easy for readers to understand, and easy to present
clearly.
Another example of when this approach can be used is in the case of estimates from an analysis like
multiple regression. Instead of reporting p-values for terms in the model, the estimates and
confidence intervals for each term in the model could be reported. Terms with confidence intervals
not including zero contribute statistically to the model, and the value of slope coefficient suggests the
size of the contribution of the term.
Bayesian approach
The most popular competitor to the NHST approach is Bayesian inference. Bayesian inference has the
advantage of calculating the probability of the hypothesis given the data, which is what we thought we
should be doing in the “Wait, does this make any sense?” section above. Essentially it takes prior
knowledge about the distribution of the parameters of interest for a population and adds the
information from the measured data to reassess some hypothesis related to the parameters of interest.
If the reader will excuse the vagueness of this description, it makes intuitive sense. We start with what
we suspect to be the case, and then use new data to assess our hypothesis.
One disadvantage of the Bayesian approach is that it is not obvious in most cases what could be used
for legitimate prior information. A second disadvantage is that conducting Bayesian analysis is not as
straightforward as the tests presented in this book.
[Video] “Understanding statistical inference” from Statistics Learning Center (Dr. Nic). 2015.
www.youtube.com/watch?v=tFRXsngz4UQ.
[Video] “Hypothesis tests, p-value” from Statistics Learning Center (Dr. Nic). 2011.
www.youtube.com/watch?v=0zZYBALbZgg.
[Video] “Understanding the p-value” from Statistics Learning Center (Dr. Nic). 2011.
118
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
www.youtube.com/watch?v=eyknGvncKLw.
“Understanding statistical inference” from Dr. Nic. 2015. Learn and Teach Statistics & Operations
Research. learnandteachstatistics.wordpress.com/2015/11/09/understanding-statistical-
inference/.
“Basic concepts of hypothesis testing” in McDonald, J.H. 2014. Handbook of Biological Statistics.
www.biostathandbook.com/hypothesistesting.html.
“Hypothesis testing”, section 4.3, in Diez, D.M., C.D. Barr , and M. Çetinkaya-Rundel. 2012. OpenIntro
Statistics, 2nd ed. www.openintro.org/.
“Hypothesis Testing with One Sample”, sections 9.1–9.2 in Openstax. 2013. Introductory Statistics.
openstaxcollege.org/textbooks/introductory-statistics.
"Proving causation" from Dr. Nic. 2013. Learn and Teach Statistics & Operations Research.
learnandteachstatistics.wordpress.com/2013/10/21/proving-causation/.
[Video] “Variation and Sampling Error” from Statistics Learning Center (Dr. Nic). 2014.
www.youtube.com/watch?v=y3A0lUkpAko.
[Video] “Sampling: Simple Random, Convenience, systematic, cluster, stratified” from Statistics
Learning Center (Dr. Nic). 2012. www.youtube.com/watch?v=be9e-Q-jC-0.
“Overview of data collection principles”, section 1.3, in Diez, D.M., C.D. Barr , and M. Çetinkaya-
Rundel. 2012. OpenIntro Statistics, 2nd ed. www.openintro.org/.
“Observational studies and sampling strategies”, section 1.4, in Diez, D.M., C.D. Barr , and M.
Çetinkaya-Rundel. 2012. OpenIntro Statistics, 2nd ed. www.openintro.org/.
“Experiments”, section 1.5, in Diez, D.M., C.D. Barr , and M. Çetinkaya-Rundel. 2012. OpenIntro
Statistics, 2nd ed. www.openintro.org/.
Exercises F
A) The number of heads from the coin is not different from the number of tails.
B) The number of heads from the coin is different from the number of tails.
119
HYPOTHESIS TESTING AND P-VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
A) There is an association between classroom and sex. That is, there is a difference in counts of
girls and boys between the classes.
B) There is no association between classroom and sex. That is, there is no difference in counts
of girls and boys between the classes.
4. We flip a coin 10 times and it lands on heads 7 times. We want to know if the coin is fair.
5. We measure the height of 9 boys and 9 girls in a class. We want to know if one group is taller than
the other.
Girls = c(152, 150, 140, 160, 145, 155, 150, 152, 147)
Boys = c(144, 142, 132, 152, 137, 147, 142, 144, 139)
t.test(Girls, Boys)
120
REPORTING RESULTS OF DATA AND ANALYSES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
mean of x mean of y
150.1111 142.1111
boxplot(cbind(Girls, Boys))
6. We count the number of boys and girls in two classrooms. We are interested to know if there is an
association between the classrooms and the number of girls and boys. That is, we want to know if
there are more girls than boys in one of the classes.
Input =("
Classroom Girls Boys
A 13 7
B 5 15
")
Matrix = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
fisher.test(Matrix)
p-value = 0.02484
121
REPORTING RESULTS OF DATA AND ANALYSES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Given the variety of experimental designs, potential types of data, and analytical approaches, it is
relatively impossible to develop a cookbook approach to reporting data summaries and analyses. That
being said, it is the intent of this chapter to give some broad and practical advice for this task.
The following commands will install these packages if they are not already installed:
if(!require(FSA)){install.packages("FSA")}
The following bullets list some of the information you should include in reporting summaries and
analyses. These can be included in a combination of text, plots, tables, plot captions, and table
headings.
A variety of plots are shown in the Basic Plots chapter, as well as in the chapters for specific statistical
analyses. Simple tables for grouped data are shown in the chapters on Descriptive Statistics, Confidence
Intervals, Descriptive Statistics for Likert Data, and Confidence Intervals for Medians.
Procedures
• A description of the analysis. Give the test used, indicate the dependent variables and
independent variables, and describe the experimental design or model in as much detail as is
appropriate. Mention the experimental units, dates, and location.
• Model assumptions checks. Describe what assumptions about the data or residuals are made
by the test and how you assessed your data’s conformity with these assumptions.
Results
• A measure of the central tendency or location of the data, such as the mean or median for
groups or populations
• A measure of the variation for the mean or median for each group, such standard deviation,
standard error, first and third quartile, or confidence intervals
• Size of the effect. Often this is conveyed by presenting means and medians in a plot or table
so that the reader can see the differences. Sometimes, specific statistics are reported to
122
REPORTING RESULTS OF DATA AND ANALYSES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
indicate “effect size”. See the “Optional technical note on effect sizes” below for more
information.
• Any other relevant statistics, such as the predictive equation from linear regression or the
critical x value from a linear plateau model.
• Results from post-hoc analyses. Summarize the differences among groups with letters, in a
plot or table. Include the alpha value for group separations
Plots
Some advice for producing plots is given in the “Some advice on producing plots” section of the
Basic Plots chapter.
For additional advice on presenting data in plots, see McDonald (2014a) in the “References” section.
Tables
Table style and format will vary widely from publication to publication. As general advice, the style
and format should follow that of the journals or extension publications in your field or institution, or
where you hope to get published.
123
REPORTING RESULTS OF DATA AND ANALYSES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For additional advice on presenting data in tables, see McDonald (2014b) in the “References” section.
Example elements
Element Example
Description of data “Mean of reading scores for program curricula”
Experimental units “for 8th grade students”
Location “in Watkins Glen, NY”
Date “1999–2000”
Statistics “Error bars represent standard error of the
mean. The effect of curricula on mean reading
score was significant by one-way ANOVA (p =
0.027). Means sharing a letter not significantly
different by Tukey-adjusted mean separations
(alpha = 0.05). Total observations = 24.”
citation("FSA")
Procedures
A study of student learning was conducted in 1999–2000 in Watkins Glen, NY. Students were
randomly assigned to one of four curricula, which they studied for one-hour per week under
teacher-supervised conditions, and their scores on an assessment exam was recorded at the
end of the study. A one-way analysis of variance was conducted with student score as the
dependent variable and curriculum as the independent variable (Mangiafico, 2016).
Treatment means were separated by Tukey-adjusted comparisons. Model residuals were
checked for normality and homoscedasticity by visual inspection of residual plots. Analysis of
variance and post-hoc tests were conducted in R (R Core Team, 2016) with the car and lsmeans
packages. Data summary was conducted with the FSA package.
124
REPORTING RESULTS OF DATA AND ANALYSES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Results
Figure 1. Mean of reading scores for program curricula for 8th grade students in Watkins Glen, NY,
1999–2000. Error bars represent standard error of the mean. The effect of curricula on mean
reading score was significant by one-way ANOVA (p < 0.0001). Means sharing a letter not
significantly different by Tukey-adjusted mean separations (alpha = 0.05). Total observations = 24.
Table 1. Mean of reading scores for program curricula for 8th grade students in
Watkins Glen, NY, 1999–2000. The effect of curricula on mean reading score was
significant by one-way ANOVA (p < 0.0001). Means sharing a letter not significantly
different by Tukey-adjusted mean separations (alpha = 0.05). n indicates number of
observations. Std. err. indicates the standard error of the mean.
Curriculum n Mean score Std. err. Tukey group
( points / 100)
A 6 90 2.89 a
B 6 60 2.89 b
C 6 55 1.29 b
D 6 80 2.89 a
For some examples of these statistics, see Sullivan and Feinn (2012) or IDRE (2015) in the
“References” section of this chapter.
125
CHOOSING A STATISTICAL TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The pwr package can calculate the effect size for proportions, chi-square goodness-of-fit, and chi-
square test of association. For the effect size for one-way ANOVA (Cohen’s f), see Mangiafico (2015) or
IDRE (2015). For effect sizes for nonparametric tests, see Tomczak and Tomczak (2014), and King and
Rosopa (2010).
References
“One-way Anova” in Mangiafico, S.S. 2015. An R Companion for the Handbook of Biological
Statistics, version 1.09. rcompanion.org/rcompanion/d_05.html.
“Guide to fairly good graphs” in McDonald, J.H. 2014a. Handbook of Biological Statistics.
www.biostathandbook.com/graph.html.
Sullivan, G.M. and R. Feinn. 2012. Using Effect Size—or Why the P Value is Not Enough. Journal
of Graduate Medical Education 4(3): 279–282.
www.ncbi.nlm.nih.gov/pmc/articles/PMC3444174/.
Tomczak, M. and Tomczak, E. 2014. The need to report effect size estimates revisited. An
overview of some recommended measures of effect size. Trends in Sports Sciences 1(21):1–
25. www.tss.awf.poznan.pl/files/3_Trends_Vol21_2014__no1_20.pdf.
[IDRE] Institute for Digital Research and Education. 2015. How is effect size used in power
analysis? UCLA.
www.ats.ucla.edu/stat/mult_pkg/faq/general/effect_size_power/effect_size_power.htm/.
King, B.M. and P.J. Rosopa. 2010. Some (Almost) Assumption-Free Tests. In Statistical Reasoning
in the Behavioral Sciences, 6th ed. Wiley.
It is important to have an experimental design planned out before you start collecting data, and to
have some an idea of how you plan on analyzing the data. One of the most common mistakes people
make in doing research is collecting a bunch of data without having thought through what questions
126
CHOOSING A STATISTICAL TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
they are trying to answer, what specific hypotheses they want to test, and what statistical tests they
can use to test these hypotheses.
The most important consideration in choosing a statistical test is determining what hypothesis you
want to test. Or, more generally, what question are you are trying to answer.
Often people have a notion about the purpose of the research they are conducting, but haven’t
formulated a specific hypothesis. It is possible to begin with exploratory data analysis, to see what
interesting secrets the data wish to say. But ultimately, choosing a statistical test relies on having in
mind a specific hypothesis to test.
For example, we may know that our goal is to determine if one curriculum works better than another.
But then we must be more specific in our hypothesis. Perhaps we wish to compare the mean of scores
that students get on an exam across the different curricula. Then a specific null hypothesis is, There is
no difference among the mean of student scores across curricula.
In this example, we identified the dependent variable as Student scores, and the independent variable
as Curriculum.
Of course, we might make things more complicated. For example, if the curricula were used in
different classrooms, we might want to include Classroom as an independent blocking variable.
To a large extent, the appropriate statistical test for your data will depend upon the number and types
of variables you wish to include in the analysis.
• However, if it is an ordinal variable, you would look toward nonparametric and ordinal
regression models.
• Nominal variables arranged in contingency tables can be analyzed with chi-square and
similar tests. Nominal dependent variables can be related to independent variables with
logistic regression.
• Count data dependent variables can be related to independent variables with Poisson
regression and related models.
127
CHOOSING A STATISTICAL TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The number and type of independent variables will also be taken into account. As will whether there
are paired observations or random blocking variables.
The table below lists the tests in this book according to their number and types of variables.
Note that each test has its own set of assumptions for appropriate data, which should be assessed
before proceeding with the analysis.
Also note that the tests in this book cover cases with a single dependent variable only. There are other
statistical tests, included under the umbrella of multivariate statistics that can analyze multiple
dependent variables simultaneously. These include multivariate analysis of variance (MANOVA),
canonical correlation, and discriminant function analysis.
The “References” and “Optional readings” sections of this chapter includes a few other guides to
choosing statistical tests.
128
CHOOSING A STATISTICAL TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
129
CHOOSING A STATISTICAL TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
130
CHOOSING A STATISTICAL TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Tests that may have analogous purposes, like comparing a measurement variable across two groups,
may test very different hypotheses.
For example, imagine you are investigating the income of two towns. Let’s say the income of Town A
is normally distributed about a mean and median of $48,000. The income of Town B has a similar
median, but has right skew, with some observations close to $1 million.
What test or statistic would you use to compare the income of these two towns?
You might be tempted to compare the means of the two towns with a t-test. In this case, however,
means may not be the best statistic for skewed data, and this data may not meet the assumptions of
the t-test.
You might be interested in comparing the median of the income of the two towns, for example with
Mood’s median test. This might make sense for some regulatory purpose that is concerned with
medians.
131
CHOOSING A STATISTICAL TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
On the other hand, looking for a systemic change in the income across the two towns may make more
sense. For example, the higher incomes in Town B may give the town a different character, for
example, some streets with larger homes or upscale stores. For this, you might use the Mann–Whitney
test.
Or you might compare the overall distributions of incomes for the two towns using the Kolmogorov–
Smirnov test.
Finally, we might want to compare at the 75th percentile of income for the two towns. This could be
done using quantile regression.
Example
The following code compares some of these results for a hypothetical data set of income in two towns.
Note that the assumptions and pitfalls of these tests are not discussed here, but should be considered
in real situations.
### load required packages
if(!require(FSA)){install.packages("FSA")}
if(!require(psych)){install.packages("psych")}
if(!require(RVAideMemoire)){install.packages("RVAideMemoire")}
if(!require(coin)){install.packages("coin")}
if(!require(quantreg)){install.packages("quantreg")}
TwoTowns = read.table("http://rcompanion.org/documents/TwoTowns.csv",
header=TRUE, sep=",")
library(psych)
headTail(TwoTowns)
summary(TwoTowns)
library(FSA)
Summarize(Income ~ Town,
data=TwoTowns,
digits=3)
132
CHOOSING A STATISTICAL TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
boxplot(Income ~ Town,
data=TwoTowns)
library(RVAideMemoire)
mood.medtest(Income ~ Town,
data = TwoTowns)
X-squared = 0, df = 1, p-value = 1
wilcox.test(Income ~ Town,
data=TwoTowns)
library(coin)
independence_test(Income ~ Town,
data = TwoTowns)
library(FSA)
ksTest(Income ~ Town,
data = TwoTowns)
133
INDEPENDENT AND PAIRED VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(quantreg)
model.null = rq(Income ~ 1,
data = TwoTowns,
tau = 0.75)
anova(model.q, model.null)
References
[IDRE] Institute for Digital Research and Education. 2015. What statistical analysis should I use?
UCLA. www.ats.ucla.edu/stat/stata/whatstat/.
Optional readings
[Video] “Choosing which statistical test to use” from Statistics Learning Center (Dr. Nic). 2014.
www.youtube.com/watch?v=rulIUAN0U3w.
134
INDEPENDENT AND PAIRED VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Dependent samples commonly arise in a few situations. One is repeated measures, in which the same
subject is measured on multiple dates. This is like the student height example described above.
A second is when we are taking multiple measurements of the same individual. An example of this
might be if we are testing students on multiple concepts; we might suspect that if a student scores well
in one section, that she is likely to score well in the other sections. Another example would be
measuring the length of people’s hands. We would suspect that someone with a large left hand is
likely to have a large right hand. A final example would be if student raters were measuring multiple
instructors. We might suspect that a rater who scores one instructor low might be likely to score
another instructor low.
A related concept is that of blocks. If observations can be broken into meaningful groups where values
are likely to be different, this should be taken into account. For example, if we are measuring students’
scores from two classes, and we suspect scores would be lower for one class than the other. If we
were testing instructional methods, we may care about the effect of the instructional methods, and not
care at all about the classes per se, but we want to take differences due to the different classes into
account.
The following commands will install these packages if they are not already installed:
if(!require(FSA)){install.packages("FSA")}
if(!require(rcompanion)){install.packages("rcompanion")}
135
INDEPENDENT AND PAIRED VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
B Right 18.5
C Right 15.9
D Right 14.9
E Right 13.7
F Right 18.9
G Right 19.5
H Right 21.5
I Right 18.5
J Right 17.1
K Right 18.9
L Right 17.5
M Right 19.5
N Right 16.5
O Right 17.4
P Right 15.6
")
Data = read.table(textConnection(Input),header=TRUE)
### Note: for the paired test, data must be ordered so that
### the first observation of Group 1
### is the same subject as the first observation of Group 2
### The following will order the data frame by Hand, and then Individual
Data
str(Data)
summary(Data)
rm(Input)
Summarize(Length ~ Hand,
data=Data,
digits=3)
136
INDEPENDENT AND PAIRED VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
boxplot(Length ~ Hand,
data=Data,
ylab="Length, cm")
Right_hand = Data$Length[Data$Hand=="Right"]
barplot(Difference,
col="dark gray",
xlab="Observation",
ylab="Difference (Right – Left)")
137
INDEPENDENT AND PAIRED VALUES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
t.test(Length ~ Hand,
data = Data,
paired = FALSE)
### No difference between left hand and right if length treated as not paired
t.test(Length ~ Hand,
data = Data,
paired = TRUE)
Paired t-test
Left_hand = Data$Length[Data$Hand=="Left"]
Right_hand = Data$Length[Data$Hand=="Right"]
library(rcompanion)
plotNormalHistogram(Difference,
xlab = "Difference")
139
INTRODUCTION TO LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Likert Data
Likert data
Numbers of responses
Most commonly, a 5- or 7- point scales is used for Likert items. It is believed that most people can
think about or visualize 5 or 7 ordered options easily. Younger children, however, may do better with
a 3-point scale or a simple dichotomous question. On the other hand, if the audience is educated about
a subject and trained in the evaluation, a 10-point scale could be used.
Symmetry
Responses to Likert items are usually symmetrical. That is, if there are options for “agree” and
“strongly agree”, there should be options for “disagree” and “strongly disagree”.
Neutral responses
Responses to Likert items also tend to have a neutral option, such as “neutral”, “neither agree nor
disagree”. But neutral responses may also be terms like “sometimes” or “occasionally” if “never” and
“rarely” on one side are balanced with “often” and “always”.
Form of responses
Numbered responses are typically described with descriptive terms, either for every number, for just
the end points, or for the end points and the middle points, for example:
Strongly Agree Neutral Disagree Strongly
agree disagree
1 2 3 4 5
———————————————————————————————
Strongly Strongly
agree disagree
1 2 3 4 5
———————————————————————————————
1 2 3 4 5
140
INTRODUCTION TO LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Other options for Likert responses include faces (smiley face, neutral face, frowny face), and a line on
which respondents mark their response.
Opt-out answers
Questions may also include opt-out responses, like “Don’t know” or “Not applicable”. These are
included outside the Likert responses.
Strongly Neutral Strongly
agree disagree
1 2 3 4 5 Not applicable
I am in favor of including opt-out responses as it tends to encourage more honest responses. It seems
to me it is better to allow a respondent to opt out of answering a question rather than force an
inauthentic response. It is possible that a respondent has no opinion or doesn’t understand a question,
or that a question is not applicable for them.
That being said, the opt-out answer “Don’t know” may not be a great choice, simply because
respondents and researchers may interpret “Don’t know” as a “Neutral” answer. It may be better to
choose less ambiguous opt-out answers like “Not applicable”.
Brown, S. Likert Scale Examples for Surveys. Iowa State University Extension.
www.extension.iastate.edu/ag/staff/info/likertscaleexamples.pdf.
Technically, a Likert item is a single question with Likert responses, whereas a Likert scale is a group
of items viewed together as a single measure. For example, one could have several Likert items with
various questions about religious attitudes or behaviors, and then combine those items to a single
Likert scale on religiosity.
When presenting methods and results, it is important to be clear if data were handled as Likert item
data or Likert scale data.
This book will treat Likert data as individual Likert items, and will not create Likert scales.
141
INTRODUCTION TO LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
One consideration is that values in interval/ratio data need to be equally spaced. That is, 2 is equally
between 1 and 3, and you could average 1 and 3 and the response would be 2. But it is not clear that
“agree” is equally spaced between “strongly agree” and “neutral”. Nor is it clear that “strongly agree”
and “neutral” could be averaged for a result of “agree”. Simply numbering the response levels does not
make the responses interval/ratio data.
This book will treat Likert data as ordinal data. It will avoid using parametric tests, such as t-test and
ANOVA, with Likert data. Likert data typically do not meet the assumptions of those parametric tests.
Instead, we will use nonparametric tests, permutation tests, and ordinal regression.
A couple other properties suggest that Likert data should not be treated as interval/ratio data. Likert
data are not continuous; that is, there typically aren’t any decimal points in Likert responses. Also, the
responses in Likert data are constrained at their ends; that is, on a five-point scale, the responses
cannot be below 1 or above 5.
Where it is useful, this book will treat Likert data as nominal data for certain types of summaries. In
general it is better to not treat ordinal data as nominal data in statistical analyses. One reason is that
when treating the data as nominal data, the information about the ordered nature of the response
categories is lost. However, sometimes it is useful to collapse Likert responses into categories; for
example, grouping “strongly agree” and “agree” together as one category and reporting its frequency
as a percentage of responses.
Cases in which treating Likert responses as interval/ratio data may be reasonable include:
• When there are a high number of response options per question (say 10)
• When only the endpoints of the responses are indicated with text descriptors
• When response options are assumed to be equally spaced
• When respondents mark their answer on a line so that the precise location of the mark can be
measured
This is not entirely permissible from a theoretical point of view since Likert scales are made up of
Likert items, and so have the same properties. But it is often a reasonable approach if the data meet
the assumptions of the analysis. This is particularly the case if the scale data take on many values.
142
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Ordinal regression
Probably the best tool for the analysis of experiments with Likert item data as the dependent variable
is ordinal regression. The ordinal package in R provides a powerful and flexible framework for ordinal
regression. It can handle a wide variety of experimental designs, including those with paired or
repeated observations. Ordinal regression is relatively easy to perform in R, but might be somewhat
challenging for the novice in statistical analyses. Occasionally there are problems with fitting models
or checking model assumptions. These cases may be frustrating for the novice user.
Permutation tests
Another tool appropriate for the analysis of Likert item data are permutation tests. The coin package
in R provides a relatively powerful and flexible framework for permutation tests with ordinal
dependent variables. It can handle models analogous to a one-way analysis of variance with blocks,
including paired or repeated observations in a complete block design. This covers more than all the
designs that can be handled with the common traditional nonparametric tests.
As a technical note, some authors have questioned using these tests with Likert item data. One
consideration is that the underlying statistics for some tests are based on the dependent variable being
continuous in nature. Another consideration is that, while these tests have provisions to handle tied
values, that they may not behave well when there are many ties, as is likely for Likert data.
Optional reading
“Oh Ordinal data, what do we do with you?” from Dr. Nic. 2013. Learn and Teach Statistics &
Operations Research. learnandteachstatistics.wordpress.com/2013/07/08/ordinal/.
“Can Likert Scale Data ever be Continuous?” from Grace-Martin, K. 2008. The Analysis Factor.
www.theanalysisfactor.com/can-likert-scale-data-ever-be-continuous/.
143
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
values 1, 2, 3, 4, 5, 5, 5, the median value is 4. If there are an even number of values, the two middle
values are averaged.
In concept, 50% of observations are less than the median, and 50% are greater than the median
(ignoring the median itself in cases of an odd number of observations). You could also find the median
of ordinal data consisting just of text descriptors. For example, for the set of values:
Using medians makes sense for ordinal data, whereas using means would in general not be
appropriate.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(lattice)){install.packages("lattice")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(plyr)){install.packages("plyr")}
if(!require(boot)){install.packages("boot")}
if(!require(rcompanion)){install.packages("rcompanion")}
144
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Imagine a scenario in which 10 respondents are evaluating a single speaker on a single Likert item.
The following example will create a data frame called Data consisting of three variables: Speaker,
Rater, and Likert. The speaker is the same for all observations, and Likert represents the response on a
five-point Likert item.
One-sample data
One-sample data refers to a single set of values that is not broken into groups. In this example, we
have only one speaker with 10 ratings, hence one set of ratings values.
Note that the str function reports that Speaker is a factor variable, Rater and Likert are integer
variables, and Likert.f is an ordered factor variable. The levels of the factor variables are shown with
the level function.
Recall that Data$Likert tells R to use the Likert variable in the data frame Data.
Input =("
Speaker Rater Likert
'Maggie Simpson' 1 3
'Maggie Simpson' 2 4
'Maggie Simpson' 3 5
'Maggie Simpson' 4 4
'Maggie Simpson' 5 4
'Maggie Simpson' 6 4
'Maggie Simpson' 7 4
'Maggie Simpson' 8 3
'Maggie Simpson' 9 2
'Maggie Simpson' 10 5
")
Data = read.table(textConnection(Input),header=TRUE)
### Create a new variable which is the Likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered = TRUE,
levels = c("1", "2", "3", "4", "5")
)
library(psych)
145
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
headTail(Data)
str(Data)
levels(Data$Speaker)
levels(Data$Likert.f)
summary(Data)
rm(Input)
1 2 3 4 5
0 1 2 5 2
146
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
### and the next row is the counts for each value.
summary(Data)
XT
Likert.f
1 2 3 4 5
0 1 2 5 2
barplot(XT,
col="dark gray",
xlab="Maggie's Likert",
ylab="Frequency")
147
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
These statistics are visualized with a box plot. Note that the heavy line in the box is the median, and
the ends of the box are the first quartile and third quartile. The extent of the whiskers in the box plot
defaults to being the most extreme values, but no more than 1.5 times the length of the box itself.
Values beyond the extent of the whiskers will be indicated with circles. Note that in the box plot for
this example that the whiskers extend to the most extreme values in the data (2 and 5), and that there
are no circles in the plot.
summary(Data$Likert)
summary(Data)
library(FSA)
Summarize(Data$Likert,
digits=3)
Box plot
boxplot(Data$Likert,
ylab="Likert scores",
xlab="Maggie's results")
148
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For this example, imagine that there are two speakers, each of which is being evaluated on each of
three questions on 10-point Likert items. There are six raters, each of whom evaluates each speaker
on each of these items.
One-way data
One-way data refers to a data set with a single measured variable, but divided into groups. In this
example looking at values of Likert for Spongebob and for Patrick would be an example of one-way
data. Note that for one-way data there is a single dependent variable (Likert) and a single independent
factor variable (Speaker).
Looking at Likert for the three levels of Question (Information, Presentation, and Questions) would also
be an example of one-way data.
Multi-way data
Multi-way data refers to looking at a single measured variable divided into groups, where the groups
are defined by at least two factor variables. If we look at values of Likert for each of the two speakers
for each of the three questions, we are looking at two-way data.
Note that the str function reports that Speaker, Question, and Rater are factor variables, Likert is an
integer variable, and Likert.f is an ordered factor variable. The levels of the factor variables are shown
with the level function.
Recall that Data$Likert tells R to use the Likert variable in the data frame Data.
Input =("
Speaker Question Rater Likert
Spongebob Information a 5
Spongebob Information b 6
Spongebob Information c 7
Spongebob Information d 8
Spongebob Information e 6
Spongebob Information f 5
Spongebob Presentation a 8
Spongebob Presentation b 7
Spongebob Presentation c 8
Spongebob Presentation d 9
Spongebob Presentation e 10
Spongebob Presentation f 7
Spongebob Questions a 3
Spongebob Questions b 4
149
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Spongebob Questions c 5
Spongebob Questions d 4
Spongebob Questions e 6
Spongebob Questions f 5
Patrick Information a 6
Patrick Information b 7
Patrick Information c 8
Patrick Information d 9
Patrick Information e 7
Patrick Information f 6
Patrick Presentation a 9
Patrick Presentation b 9
Patrick Presentation c 10
Patrick Presentation d 10
Patrick Presentation e 8
Patrick Presentation f 8
Patrick Questions a 5
Patrick Questions b 6
Patrick Questions c 6
Patrick Questions d 7
Patrick Questions e 5
Patrick Questions f 7
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Speaker = factor(Data$Speaker,
levels=unique(Data$Speaker))
Data$Question = factor(Data$Question,
levels=unique(Data$Question))
Data$Likert.f = factor(Data$Likert,
ordered=TRUE,
levels = c("1", "2", "3", "4", "5",
"6", "7", "8", "9", "10")
)
library(psych)
headTail(Data)
150
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
4 Spongebob Information d 8 8
... <NA> <NA> <NA> ... <NA>
33 Patrick Questions c 6 6
34 Patrick Questions d 7 7
35 Patrick Questions e 5 5
36 Patrick Questions f 7 7
str(Data)
levels(Data$Speaker)
levels(Data$Question)
levels(Data$Rater)
levels(Data$Likert.f)
[1] "1" "2" "3" "4" "5" "6" "7" "8" "9" "10"
summary(Data)
rm(Input)
The xtabs function can be used to generate counts across groups. This process is known as cross-
tabulation, or cross-tabs. The prop.table function, with the margin=1 option produces the proportions
of observations for each row.
151
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
XT
Likert.f
Speaker 1 2 3 4 5 6 7 8 9 10
Spongebob 0 0 1 2 4 3 3 3 1 1
Patrick 0 0 0 0 2 4 4 3 3 2
prop.table(XT,
margin = 1)
Likert.f
Speaker 1 2 3 4 5 6 7 8 9 10
Spongebob 0.0000 0.0000 0.0556 0.1111 0.2222 0.1667 0.1667 0.1667 0.0556 0.0556
Patrick 0.0000 0.0000 0.0000 0.0000 0.1111 0.2222 0.2222 0.1667 0.1667 0.1111
sum(XT)
[1] 36
rowSums(XT)
Spongebob Patrick
18 18
colSums(XT)
1 2 3 4 5 6 7 8 9 10
0 0 1 2 6 7 7 6 4 3
152
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
XT
barplot(XT,
beside=TRUE,
legend=TRUE,
ylim=c(0, 5), # adjust to remove legend overlap
xlab="Likert score",
ylab="Frequency"
)
XT2
, , Question = Information
Likert.f
Speaker 1 2 3 4 5 6 7 8 9 10
Spongebob 0 0 0 0 2 2 1 1 0 0
Patrick 0 0 0 0 0 2 2 1 1 0
, , Question = Presentation
Likert.f
Speaker 1 2 3 4 5 6 7 8 9 10
Spongebob 0 0 0 0 0 0 2 2 1 1
153
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Patrick 0 0 0 0 0 0 0 2 2 2
, , Question = Questions
Likert.f
Speaker 1 2 3 4 5 6 7 8 9 10
Spongebob 0 0 1 2 2 1 0 0 0 0
The Summary function in the FSA package can produce these summary statistics across groups for
one-way data or multi-way data.
Histograms are then produced to visualize the distribution of responses across groups. Histograms for
both one-way and two-way data can be produced with the histogram function in the lattice package.
library(FSA)
Summarize(Likert ~ Question,
data=Data,
digits=3)
library(FSA)
154
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(lattice)
155
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
156
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
In this example, the median Likert is calculated for each level of the interaction of two factors, and the
first and third quartiles are used to indicate the spread of data about each median.
The Summarize function in the FSA package creates a data frame called Sum. The variables median, Q1,
and Q3 in this data frame will then be used in the plot.
The interaction plot is produced with the ggplot2 package. This package is very versatile and powerful
for creating plots, but the code can be intimidating at first. Note that the code indicates that the data
frame to use is Sum, and the use of the variables Speaker, median, Question, Q1, Q3 from this data
frame. Also note that the y axis label defined as “Median Likert score”.
### Create a data frame called Sum with median and quartiles
library(FSA)
157
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Sum
library(ggplot2)
pd = position_dodge(.2)
ggplot(Sum, aes(x=Speaker,
y=median,
color=Question)) +
geom_errorbar(aes(ymin=Q1,
ymax=Q3),
width=.2, size=0.7, position=pd) +
geom_point(shape=15, size=4, position=pd) +
theme_bw() +
theme(axis.title = element_text(face = "bold"))+
ylab("Median Likert score")
158
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The function groupwiseMedian in the rcompanion package creates a data frame, called Sum, here that
includes medians and confidence intervals of the medians for one-way or multi-way data. By default
the function uses confidence intervals by the BCa method, but can produce confidence intervals by
other methods.
Note that the order of speakers has changed from the previous interaction plot.
library(rcompanion)
Sum
library(ggplot2)
pd = position_dodge(.2)
ggplot(Sum, aes(x=Speaker,
y=Median,
color=Question)) +
geom_errorbar(aes(ymin=Bca.lower,
ymax=Bca.upper),
width=.2, size=0.7, position=pd) +
geom_point(shape=15, size=4, position=pd) +
theme_bw() +
theme(axis.title = element_text(face = "bold"))+
ylab("Median Likert score")
159
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Opt-out responses include options like “don’t know” or “not applicable”. A typical approach to
handling opt-out responses in Likert data is to separate them from the numeric or ordered factor
responses of the question and report them separately.
For example, if responses to a question were 5, 5, 5, 4, 4, 2, ‘Don’t know’, ‘N/A’, you might report that
you had 8 total responses, with 12.5% “Don’t know”, 12.5% “N/A”, and a median response of 4.5.
Q1.f = as.factor(Q1)
summary(Q1.f)
160
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Q1.n = as.numeric(Q1)
library(FSA)
Summarize(Q1.n,
digits = 2)
Note that the read.table function by default reads in the values of Likert as a factor variable, since it
contains character values, not as a character variable. Also note that you shouldn’t use NA for a value
of “not applicable” since NA is reserved by R for a missing value.
Input =("
Rater Question Likert
'1' Q1 5
'2' Q1 5
'3' Q1 5
'4' Q1 4
'5' Q1 4
'6' Q1 2
'7' Q1 DontKnow
'8' Q1 NotApp
'1' Q2 3
'2' Q2 3
'3' Q2 3
'4' Q2 2
'5' Q2 DontKnow
'6' Q2 2
'7' Q2 DontKnow
'8' Q2 DontKnow
'1' Q3 1
'2' Q3 2
'3' Q3 3
'4' Q3 2
'5' Q3 3
'6' Q3 1
'7' Q3 2
'8' Q3 3
")
Data = read.table(textConnection(Input),header=TRUE)
XT
161
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Likert
Question 1 2 3 4 5 DontKnow NotApp
Q1 0 1 0 2 3 1 1
Q2 0 2 3 0 0 3 0
Q3 2 3 3 0 0 0 0
Data$Likert.n = as.numeric(as.character(Data$Likert))
library(FSA)
Summarize(Likert.n ~ Question,
data = Data)
XT
Likert
1 2 3 4 5 DontKnow NotApp
2 6 6 2 3 4 1
sum(XT)
[1] 24
Summarize( ~ Likert.n,
data = Data,
digits = 2)
162
DESCRIPTIVE STATISTICS FOR LIKERT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Exercises G
• How many Likert responses of "5" did Spongebob and Patrick get, respectively?
• What were the median scores for Patrick for each of Information, Presentation, and
Questions?
• What do the histograms for the two-way data (Spongebob and Patrick for each of
Information, Presentation, and Questions) suggest to you?
5. Sandy Cheeks and Squidward each delivered presentations, and were each evaluated on each of the
two courses (A and B).
Speaker Course Likert
Sandy A 5
Sandy A 5
Sandy A 5
Sandy A 4
Sandy A 3
Sandy A 3
Sandy B 5
Sandy B 4
Sandy B 3
Sandy B 3
Sandy B 2
Sandy B 2
Squidward A 4
163
DESCRIPTIVE STATISTICS WITH THE LIKERT PACKAGE SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Squidward A 4
Squidward A 3
Squidward A 3
Squidward A 2
Squidward A 2
Squidward B 3
Squidward B 3
Squidward B 2
Squidward B 2
Squidward B 1
Squidward B 1
a. Summarize the data as one-way or two-way data, using at least one plot, and at least one
numerical summary. Interpret your results, and describe your conclusions. Remember to
use statistics that are appropriate for Likert data.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(likert)){install.packages("likert")}
164
DESCRIPTIVE STATISTICS WITH THE LIKERT PACKAGE SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
4 2 4
5 2 4
5 3 3
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Pooh = factor(Data$Pooh,
levels = c("1", "2", "3", "4", "5"),
ordered = TRUE)
Data$Piglet = factor(Data$Piglet,
levels = c("1", "2", "3", "4", "5"),
ordered = TRUE)
Data$Tigger = factor(Data$Tigger,
levels = c("1", "2", "3", "4", "5"),
ordered = TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
library(likert)
likert(Data)
165
DESCRIPTIVE STATISTICS WITH THE LIKERT PACKAGE SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Item 1 2 3 4 5
1 Pooh 0 0 10 60 30
2 Piglet 10 60 20 10 0
3 Tigger 0 0 20 60 20
### Note: if there are NA’s, this summary doesn’t tell you!
summary(Data)
### Note: if there are NA’s, this summary will tell you
library(likert)
Result = likert(Data)
summary(Result)
### Note: responses are grouped into "low", "neutral", and "high"
### Note: if there are NA’s, this summary doesn’t tell you!
Bar plot
library(likert)
Result = likert(Data)
plot(Result,
type="bar")
166
DESCRIPTIVE STATISTICS WITH THE LIKERT PACKAGE SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Heat plot
library(likert)
Result = likert(Data)
plot(Result,
type="heat",
low.color = "white",
high.color = "blue",
text.color = "black",
text.size = 4,
wrap = 50)
167
DESCRIPTIVE STATISTICS WITH THE LIKERT PACKAGE SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Density plot
library(likert)
Result = likert(Data)
plot(Result,
type="density",
facet = TRUE,
bw = 0.5)
168
CONFIDENCE INTERVALS FOR MEDIANS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
### Note: Curves are density plots, which show the distribution of values
### similar to a histogram.
References
Jason Bryer, J. and Speerschneider, K. likert: An R package analyzing and visualizing Likert items.
jason.bryer.org/likert/.
The methods for determining confidence intervals for medians are distinct from the traditional
method for means. There are different methods for calculating confidence intervals for the median,
and there are few different methods are presented in this chapter.
When data distributions are normal or uniform in distribution, and the number of observations is
large (say, n ≥ 100), any of the methods should work reasonably well. When distributions are heavily
skewed or the number of observations is relatively small (say, n < 20), results from some methods can
differ notably from others.
For routine use, I probably recommend against the basic, normal, exact, and wilcox methods. The BCa
(bias corrected, accelerated) is often cited as the best for theoretical reasons. The percentile method is
also cited as typically good. However, if you get the “extreme order statistics used as endpoints”
warning message, use a different test. For small data sets the interval from BCa may be wider than for
some other methods.
For a description of the bootstrap confidence interval methods, see Carpenter and Bithell (2000) in the
“References” section below.
There are also some variants in the calculation of the median, such as the “(pseudo)median” calculated
by the wilcox.test function.
169
CONFIDENCE INTERVALS FOR MEDIANS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
• FSA
• boot
• DescTools
• plyr
• rcompanion
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(boot)){install.packages("boot")}
if(!require(DescTools)){install.packages("DescTools")}
if(!require(plyr)){install.packages("plyr")}
if(!require(rcompanion)){install.packages("rcompanion")}
For one-sample data, the median can be calculated with the median function, the summary function,
and the Summarize function from the FSA package.
Confidence intervals for the median can be calculated with the wilcox.test function, or by a process
called bootstrapping with the boot function in the boot package, or with the MedianCI function in the
DescTools package.
The function groupwiseMedian in the rcompanion package produces medians, and allows for the
calculation “basic”, “normal”, “percentile”, and “bca” bootstrap confidence intervals for medians, as
well as the confidence intervals for medians from the wilcox.test function. It can also calculate these
statistics for grouped data (one-way or multi-way).
This example will use some theoretical data for Lisa Simpson, rated on a 10-point Likert item.
Input =("
Speaker Rater Likert
'Lisa Simpson' 1 8
'Lisa Simpson' 2 10
'Lisa Simpson' 3 9
'Lisa Simpson' 4 10
'Lisa Simpson' 5 6
'Lisa Simpson' 6 5
'Lisa Simpson' 7 3
'Lisa Simpson' 8 7
'Lisa Simpson' 9 8
'Lisa Simpson' 10 5
'Lisa Simpson' 11 10
'Lisa Simpson' 12 4
'Lisa Simpson' 13 8
'Lisa Simpson' 14 6
'Lisa Simpson' 15 9
'Lisa Simpson' 16 8
'Lisa Simpson' 17 7
170
CONFIDENCE INTERVALS FOR MEDIANS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
'Lisa Simpson' 18 5
'Lisa Simpson' 19 8
'Lisa Simpson' 20 7
'Lisa Simpson' 21 9
'Lisa Simpson' 22 8
'Lisa Simpson' 23 7
'Lisa Simpson' 24 5
'Lisa Simpson' 25 10
'Lisa Simpson' 26 7
")
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
[1] 7.5
summary(Data)
Summarize(Likert ~ Speaker,
data=Data,
digits=3)
171
CONFIDENCE INTERVALS FOR MEDIANS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Note that the conf.int=TRUE option must be used to produce the confidence interval, and the
conf.level=0.95 option indicates that a 95% confidence interval should be calculated.
wilcox.test(Data$Likert,
alternative="two.sided",
correct=TRUE,
conf.int=TRUE,
conf.level=0.95)
The boot package in R can derive various statistics with a bootstrap process. Note that the grammar of
the function is somewhat complicated, but here Data$Likert is the variable we wish to get the statistics
for, and R=10000 indicates the number of bootstrap replicates to use. Mboot here is defined as the
result of the bootstrap. The boot.ci function produces four types of confidence intervals for the
median. Note also that the displaying the result Mboot gives a standard error for the estimated
median. See ?boot.ci for more details on these methods.
If the function takes too long to complete, you can decrease the R= value.
Note that results for any statistic derived from an iterative process like bootstrapping may be slightly
different if the process is re-run.
Optional technical note: Bootstrapped confidence intervals may not be reliable for discreet data, such
as the ordinal Likert data used in these examples, especially for small samples. My understanding is
that the determination of the confidence intervals assumes a continuous and bell-shaped distribution
of the statistic (the median, in this case) from the bootstrapped samples. That is, hist(Mboot$t[,1], col =
"darkgray") below should produce a continuous and bell-shaped distribution. This concern is not
considered in the examples in this book.
library(boot)
Mboot = boot(Data$Likert,
function(x,i) median(x[i]),
R=10000)
172
CONFIDENCE INTERVALS FOR MEDIANS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
boot.ci(Mboot,
conf = 0.95,
type = c("norm", "basic" ,"perc", "bca")
)
Intervals :
Level Normal Basic
95% ( 6.510, 8.535 ) ( 7.000, 8.500 )
Mboot
hist(Mboot$t[,1],
col = "darkgray")
MedianCI(Data$Likert,
conf.level = 0.95,
na.rm = FALSE,
method = "exact",
R = 10000)
It can calculate these statistics for grouped data (one-way or multi-way), with the factors for two-way
data indicated with, for example, group=c("Factor.A", "Factor.B"). An example of using this function
with two-way data is in the “Interaction plot using medians and confidence intervals” section in the
Descriptive Statistics for Likert Data chapter.
The basic, normal, percentile, bca, wilcox, and exact options determine which types of confidence
intervals will be calculated.
173
CONFIDENCE INTERVALS FOR MEDIANS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Note that results for any statistic derived from an iterative process like bootstrapping may be slightly
different.
Also note that Speaker is being treated a grouping variable, even though it has only one level.
library(rcompanion)
groupwiseMedian(data=Data,
group="Speaker",
var="Likert",
conf=0.95,
R=5000,
percentile=TRUE,
bca=TRUE,
digits=3)
The following example revisits the Pooh, Piglet and Tigger data from the Descriptive Statistics with the
likert Package chapter.
The Summarize function in the FSA package will produce medians and quantiles for grouped data
including one-way and multi-way data.
The function groupwiseMedian will produce medians and confidence intervals for grouped data,
including one-way and multi-way data.
Input =("
Speaker Likert
Pooh 3
Pooh 5
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 5
Pooh 5
Piglet 2
Piglet 4
Piglet 2
Piglet 2
Piglet 1
Piglet 2
Piglet 3
174
CONFIDENCE INTERVALS FOR MEDIANS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Piglet 2
Piglet 2
Piglet 3
Tigger 4
Tigger 4
Tigger 4
Tigger 4
Tigger 5
Tigger 3
Tigger 5
Tigger 4
Tigger 4
Tigger 3
")
Data = read.table(textConnection(Input),header=TRUE)
Summarize(Likert ~ Speaker,
data=Data,
digits=3)
groupwiseMedian function to produce medians and confidence intervals for grouped data
The groupwiseMedian function produces medians, and calculates confidence intervals for medians
using the boot.ci function in the boot package and the wilcox.test function.
It can calculate these statistics for grouped data, with the factors for two-way data indicated with, for
example, group=c("Factor.A", "Factor.B"). An example of using this function with two-way data is in
the “Interaction plot using medians and confidence intervals” section in the Descriptive Statistics for
Likert Data chapter.
The basic, normal, percentile, bca, wilcox, and exact options determine which types of confidence
intervals will be calculated. For details on the calculations of these statistics, see ?wilcox.test and
?boot.ci.
Note that results for any statistic derived from an iterative process like bootstrapping may be slightly
different. Conducting bootstraps across several groups may take a long time if the R option is set to a
high value; however setting R to too low a value may produce errors. R=1000 should be fine for most
applications.
175
CONFIDENCE INTERVALS FOR MEDIANS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(rcompanion)
groupwiseMedian(data=Data,
group="Speaker",
var="Likert",
conf=0.95,
R=5000,
percentile=TRUE,
bca=FALSE,
digits=3)
References
Carpenter, J. and J. Bithel. 2000. “Bootstrap confidence intervals: when, which, what? A practical guide
for medical statisticians”. Statistics in Medicine 19:1141–1164.
www.tau.ac.il/~saharon/Boot/10.1.1.133.8405.pdf.
Exercises H
2. For Lisa Simpson's data, what is the 95% confidence interval for the median using the following
methods?
• wilcox.test
• normal
• basic
• percentile
• BCa
• For Piglet, what was his median score and 95% confidence interval using the percentile
method?
5. Bart Simpson and Milhouse Van Houten were each evaluated on a Likert scale. Determine the
median score for each, and determine the 95% confidence interval for the median score.
176
CONVERTING NUMERIC DATA TO CATEGORIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
• Be sure to state which method for the confidence interval you used.
This approach relies on the chosen cut-off points being meaningful. For example, for a grade of 70–79
to be considered “sufficient”, the evaluation instruments (e.g. the tests, quizzes, and assignments) need
177
CONVERTING NUMERIC DATA TO CATEGORIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
to be calibrated so that a grade of 75 really is “sufficient”, and not “excellent” or “good”, etc. You can
imagine a case where a 4 or 5 on a 5-point Likert item is considered “high”, but if all respondents
scored 4 or 5 on the item, it might not be clear that these values are “high”, but may just be the typical
response. Likewise, in this case, the decision to group 4 and 5 as “high” needs be compared with a
decision to group 4 with 2 and 3 as “medium”. This breakdown may be closer to how people interpret
a 5-point Likert scale. Either grouping could be meaningful depending on the purpose of the
categorization and the interpretation of each level in the Likert item.
The advantage to this approach is that it does not rely on the scoring system being meaningful in its
absolute values. That is, students scoring above the 90th percentile are scoring higher than 90% of
students. It doesn’t matter if the score for the 90th percentile is 90 out of 100 or 50 out of 100. If the
groups use equally-spaced breakpoints, for example 0th, 25th, 50th, 75th, and 100th percentiles, there
should be approximately an equal number of respondents in each category.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(cluster)){install.packages("cluster")}
if(!require(fpc)){install.packages("fpc")}
178
CONVERTING NUMERIC DATA TO CATEGORIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Homer c 4
Homer d 4
Homer e 4
Homer f 5
Homer g 5
Homer h 5
Homer i 3
Homer j 2
Homer k 3
Homer l 4
Homer m 5
Homer n 5
Homer o 5
Homer p 4
Homer q 4
Homer r 3
Homer s 2
Homer t 5
Homer u 3
")
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Categorize data
Data$Category[Data$Likert == 1 | Data$Likert == 2] = "Low"
Data$Category[Data$Likert == 3 ] = "Medium"
Data$Category[Data$Likert == 4 | Data$Likert == 5] = "High"
Data
179
CONVERTING NUMERIC DATA TO CATEGORIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
XT
Instructor
Category Homer
Low 2
Medium 5
High 14
[1] j s
Data$Student[Data$Category == "Medium"]
[1] a i k r u
Data$Student[Data$Category == "High"]
[1] b c d e f g h l m n o p q t
$Low
[1] j s
$Medium
[1] a i k r u
$High
[1] b c d e f g h l m n o p q t
180
CONVERTING NUMERIC DATA TO CATEGORIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Summary table
Category Range Count Students
Low 1 or 2 2 j, s
Medium 3 5 a, i, k, r, u
High 4 or 5 14 b, c, d, e, f, g, h, l, m, n, o, p, q, t
Categorize data
Percentile_00 = min(Data$Likert)
Percentile_33 = quantile(Data$Likert, 0.33333)
Percentile_67 = quantile(Data$Likert, 0.66667)
Percentile_100 = max(Data$Likert)
dimnames(RB)[[2]] = "Value"
RB
Value
Percentile_00 2.0000
Percentile_33 3.6666
Percentile_67 4.3334
Percentile_100 5.0000
Data
XT
181
CONVERTING NUMERIC DATA TO CATEGORIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Instructor
Group Homer
Lower_third 7
Middle_third 7
Upper_third 7
$Lower_third
[1] a i j k r s u
$Middle_third
[1] b c d e l p q
$Upper_third
[1] f g h m n o t
Summary table
Group Range Count Students
Lower third 1, 2, or 3 7 a, i, j, k, r, s, u
Middle third 4 7 b, c, d, e, l, p, q
Upper third 5 7 f, g, h, m, n, o, t
182
CONVERTING NUMERIC DATA TO CATEGORIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Marge q 5 1
Marge r 5 1
Marge s 4 1
Marge t 4 1
")
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Plot data
In the following plot, each letter represents a Student from the data frame. To me, this plot suggests
that the data could be reasonably clustered into 4 or perhaps 7 or 8 clusters.
plot(jitter(Happy) ~ jitter(Tired),
data = Data,
pch=as.character(Data$Student))
183
CONVERTING NUMERIC DATA TO CATEGORIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Practical considerations may override the results of the pamk function. In the example below, if we
include 1 in the possible range of cluster numbers, the function will determine that 1 is the optimum
number. Also, if we extend the range to, say, 10, the function will choose 7 as the optimum number,
but this may be too many for our purposes.
library(fpc)
PAMK = pamk(Data.num,
krange = 2:5,
metric="manhattan")
PAMK$nc
[1] 4
plot(PAMK$crit)
lines(PAMK$crit)
For the range of cluster numbers shown on the x-axis, the crit value is maximized
at 4, suggesting 4 is optimum number of clusters for this range.
Categorize data
We will use the pam function in the cluster package to divide our data into four clusters.
library(cluster)
184
CONVERTING NUMERIC DATA TO CATEGORIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
PAM
Medoids:
ID Happy Tired
[1,] 10 5 5
[2,] 9 1 5
[3,] 13 3 3
[4,] 16 5 1
Clustering vector:
[1] 1 1 2 2 1 1 2 2 2 1 3 3 3 4 4 4 4 4 4 4
Data$Cluster = PAMClust
Data
XT
Instructor
Cluster Marge
Cluster 1 5
Cluster 2 5
Cluster 3 3
Cluster 4 7
185
CONVERTING NUMERIC DATA TO CATEGORIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
$`Cluster 1`
[1] a b e f j
$`Cluster 2`
[1] c d g h i
$`Cluster 3`
[1] k l m
$`Cluster 4`
[1] n o p q r s t
Summary table
Cluster Interpretation Count Students
Cluster 1 Tired and happy 5 a, b, e, f, j
Cluster 2 Tired and not happy 5 c, d, d, h, i
Cluster 3 Middle of the road 3 k, l, m
Cluster 4 Not tired and happy 7 n, o, p, q, r, s, t
Final plot
ggplot(Data,
aes(x = Tired,
y = Happy,
color = Cluster)) +
geom_point(size=3) +
geom_jitter(width = 0.4, height = 0.4) +
theme_bw()
186
CONVERTING NUMERIC DATA TO CATEGORIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
187
INTRODUCTION TO TRADITIONAL NONPARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Nonparametric tests do not assume that the underlying data have any specific distribution. However,
it is important to understand the assumptions of each specific test before using it.
• The tests presented in this section are relatively common, and your audience is relatively
likely to be familiar with them.
• Their nonparametric nature makes them appropriate for interval/ratio data that don’t meet
the assumptions of parametric analyses. These include data that are skewed, non-normal,
contain outliers, or possibly are censored. Censored data is data where there is an upper or
lower limit to values. For example, if ages under 5 are reported as “under 5”.
In general, these tests determine if there is a systematic difference between two groups. This may be
due to a difference in location (e.g. median) or in the shape or spread of the distribution of the data. It
is therefore appropriate to report significant results as, e.g., “There is a significant difference between
Likert scores from the pre-test and the post-test." Or, "The significant Mann–Whitney test indicates
that Likert scores from the two classes come from different populations."
For the Mann-Whitney, Kruskal-Wallis, and Friedman tests, if the distributions of the groups have the
same shape and spread, then it can be assumed that the difference between groups is a difference in
medians. Otherwise, the difference is a difference in distributions.
In reality, you should look at the distributions of each group in these tests, with histograms or box
plots, so that your conclusions can accurately reflect the data. You don't want to imply that differences
between two treatments are differences in medians when they are really differences in the shape or
spread of the distributions. On the other hand, if it really does look like the difference is a difference in
location, you want to be clear about this.
188
INTRODUCTION TO TRADITIONAL NONPARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
As a point of interest, Mangiafico (2015) and McDonald (2014) in the “References” section provide an
example of a significant Kruskal–Wallis test where the groups have identical medians.
Some authors caution against using traditional nonparametric tests with ordinal dependent variables,
since many of them were developed for use with continuous (interval/ratio) data. Other authors
argue that, since these tests rank-transform data before analysis and have adjustments for tied ranks,
that they are appropriate for ordinal data. Some authors have further concerns about situations
where are likely to be many ties in ranks, such as Likert data.
The following sections of this book, Ordinal Tests with Cumulative Link Models and Permutation Tests
describe different approach to handling ordinal data that may be a better approach than using the
traditional nonparametric tests described in this section, at least in some cases.
These nonparametric tests are commonly used for interval/ratio data when the data fail to meet the
assumptions of parametric analysis.
Some authors discourage using common nonparametric tests for interval/ratio data.
• One issue is the interpretation of the results mentioned above. That is, often results are
incorrectly interpreted as a difference in medians when they are really describing a
difference in distributions.
• Another problem is the lack of flexibility in designs these test can handle. For example, there
is no common equivalent for a parametric two-way analysis of variance.
• Finally, these tests may lack power relative to their parametric equivalents.
Given these considerations and the fact that that parametric statistics are often relatively robust to
minor deviations in their assumptions, some authors argue that it is often better to stick with
parametric analyses for interval/ratio data if it’s possible to make them work.
References
“Kruskal–Wallis Test” in Mangiafico, S.S. 2015. An R Companion for the Handbook of Biological
Statistics, version 1.09. rcompanion.org/rcompanion/d_06.html.
189
ONE-SAMPLE WILCOXON SIGNED-RANK TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Appropriate data
• One-sample data
• Data are ordinal, interval, or ratio
• Data are relatively symmetrical about their median
Hypotheses
• Null hypothesis: The distribution of the data set is symmetric about the default value.
• Alternative hypothesis (two-sided): The distribution of the data set is not symmetric about
the default value.
Interpretation
Reporting significant results as “Likert scores were significantly different from a neutral value of 3” is
acceptable.
The best advice is to use a name specific to the test being used.
If data are not symmetrical, the sign test can be used as an alternative. The sign test is described in the
next chapter.
The following commands will install these packages if they are not already installed:
190
ONE-SAMPLE WILCOXON SIGNED-RANK TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(BSDA)){install.packages("BSDA")}
This example will re-visit the Maggie Simpson data from the Descriptive Statistics for Likert Data
chapter.
The example answers the question, “Are Maggie’s scores significantly different from a ‘neutral’ score of
3?”
The test will be conducted with the wilcox.test function, which produces a p-value for the hypothesis,
as well a pseudo-median and confidence interval.
Note that the bar plot shows that the data are relatively symmetrical in distribution, suggesting that
the one-sample Wilcoxon test will be appropriate.
Input =("
Speaker Rater Likert
'Maggie Simpson' 1 3
'Maggie Simpson' 2 4
'Maggie Simpson' 3 5
'Maggie Simpson' 4 4
'Maggie Simpson' 5 4
'Maggie Simpson' 6 4
'Maggie Simpson' 7 4
'Maggie Simpson' 8 3
'Maggie Simpson' 9 2
'Maggie Simpson' 10 5
")
Data = read.table(textConnection(Input),header=TRUE)
### Create a new variable which is the likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered = TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
191
ONE-SAMPLE WILCOXON SIGNED-RANK TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
rm(Input)
Likert.f
Speaker 2 3 4 5
Maggie Simpson 1 2 5 2
prop.table(XT,
margin = 1)
Likert.f
Speaker 2 3 4 5
Maggie Simpson 0.1 0.2 0.5 0.2
Bar plot
XT = xtabs(~ Likert.f,
data=Data)
barplot(XT,
col="dark gray",
xlab="Maggie's Likert",
ylab="Frequency")
192
ONE-SAMPLE WILCOXON SIGNED-RANK TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Summarize(Likert ~ Speaker,
data=Data,
digits=3)
### You will get the "cannot compute exact p-value with ties" error
### You can ignore this, or use the exact=FALSE option.
sample estimates:
(pseudo)median
4.000032
### Note that the output will also produce a pseudo-median value
### and a confidence interval if the conf.int=TRUE option is used.
Exercises I
What were the first and third quartiles for her scores?
193
ONE-SAMPLE WILCOXON SIGNED-RANK TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Are the data reasonably symmetric about their median, or should another test be used?
According to the one-sample Wilcoxon signed-rank test, is her median score significantly
different from a neutral score of 3?
Is the confidence interval output from the test useful in answering the previous question?
Do these results reflect what you would expect from looking at the bar plot?
2. Brian Griffin wants to assess the education level of students in his course on creative writing for
adults. He wants to know the median education level of his class, and if the education level of his class
is different from the typical Bachelor’s level.
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
What was the median education level? (Be sure to report the education level, not just the
numeric code!)
194
SIGN TEST FOR ONE-SAMPLE DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Are the data reasonably symmetric about their median, or should another test be used?
According to the one-sample Wilcoxon signed-rank test, is the median education level
significantly different from a typical level of Bachelor’s?
Is the confidence interval output from the test useful in answering the previous question?
Plot Brian’s data in a way that helps you visualize the data. Do the results reflect what you
would expect from looking at the plot?
The test is conducted with the SIGN.test function in the BSDA package or the SignTest function in the
DescTools package. These functions produce a p-value for the hypothesis, as well as the median and
confidence interval of the median for the data.
Appropriate data
• One-sample data
• Data are ordinal, interval, or ratio
Hypotheses
• Null hypothesis: The median of the data set is equal to the default value.
• Alternative hypothesis (two-sided): The median of the data set is not equal to the default
value.
Interpretation
Reporting significant results as “Likert scores were significantly different from a default value of 3” is
acceptable. As is “Median Likert scores were significantly different from a default value of 3”
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("BSDA")}
195
SIGN TEST FOR ONE-SAMPLE DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
if(!require(BSDA)){install.packages("DescTools")}
For appropriate plots and data frame checking, see the One-sample Wilcoxon Signed-rank Test chapter.
Input =("
Speaker Rater Likert
'Maggie Simpson' 1 3
'Maggie Simpson' 2 4
'Maggie Simpson' 3 5
'Maggie Simpson' 4 4
'Maggie Simpson' 5 4
'Maggie Simpson' 6 4
'Maggie Simpson' 7 4
'Maggie Simpson' 8 3
'Maggie Simpson' 9 2
'Maggie Simpson' 10 5
")
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Note that Data$Likert is the one-sample data, and md=3 indicates the default value to compare to.
library(BSDA)
SIGN.test(Data$Likert,
md = 3)
One-sample Sign-Test
s = 7, p-value = 0.07031
196
TWO-SAMPLE MANN–WHITNEY U TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
sample estimates:
median of x
4
Note that Data$Likert is the one-sample data, and mu=3 indicates the default value to compare to.
library(DescTools)
SignTest(Data$Likert,
mu = 3)
One-sample Sign-Test
sample estimates:
median of the differences
4
The two-sample Mann–Whitney U test compares values for two groups. A significant result suggests
that the values for the two groups are different. It is equivalent to a two-sample Wilcoxon rank-sum
test.
In the context of this book, the test is useful to compare the scores or ratings from two speakers, two
different presentations, or two groups of audiences.
197
TWO-SAMPLE MANN–WHITNEY U TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
If the shape and spread of the distributions of values of each group is similar, then the test compares
the medians of the two groups. Otherwise, the test is really testing if there is a systematic difference in
the values of the two groups.
The test assumes that the observations are independent. That is, it is not appropriate for paired
observations or repeated measures data.
If the distributions of values of each group are similar in shape, but have outliers, then Mood’s median
test is an appropriate alternative.
Appropriate data
• Two-sample data. That is, one-way data with two groups only
• Dependent variable is ordinal, interval, or ratio
• Independent variable is a factor with two levels. That is, two groups
• Observations between groups are independent. That is, not paired or repeated measures
data
• In order to be a test of medians, the distributions of values for each group need to be of
similar shape and spread; outliers affect the spread. Otherwise the test is a test of
distributions.
Hypotheses
If the distributions of the two groups are similar in shape and spread:
• Null hypothesis: The medians of values for each group are equal.
• Alternative hypothesis (two-sided): The medians of values for each group are not equal.
If the distributions of the two groups are not similar in shape and spread:
• Null hypothesis: The distribution of values for each group are equal.
• Alternative hypothesis (two-sided): There is systematic difference in the distribution of
values for the groups.
Interpretation
If the distributions of the two groups are similar in shape:
Significant results can be reported as “The median value of group A was significantly different
from that of group B.”
Mood’s median test compares the medians of two groups. It is described in the next chapter.
198
TWO-SAMPLE MANN–WHITNEY U TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Another alternative is to use cumulative link models for ordinal data, which are described later in this
book.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(lattice)){install.packages("lattice")}
This example re-visits the Pooh and Piglet data from the Descriptive Statistics with the likert Package
chapter.
It answers the question, “Are Pooh's scores significantly different from those of Piglet?”
The Mann–Whitney U test is conducted with the wilcox.test function, which produces a p-value for the
hypothesis. First the data are summarized and examined using bar plots for each group.
Because the bar plots show that the distributions of scores for Pooh and Piglet are relatively similar in
shape, the Mann–Whitney U test can be interpreted as a test of medians.
Input =("
Speaker Likert
Pooh 3
Pooh 5
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 5
Pooh 5
Piglet 2
Piglet 4
Piglet 2
Piglet 2
Piglet 1
Piglet 2
Piglet 3
Piglet 2
199
TWO-SAMPLE MANN–WHITNEY U TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Piglet 2
Piglet 3
")
Data = read.table(textConnection(Input),header=TRUE)
### Create a new variable which is the Likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered = TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Likert.f
Speaker 1 2 3 4 5
Piglet 1 6 2 1 0
Pooh 0 0 1 6 3
prop.table(XT,
margin = 1)
Likert.f
Speaker 1 2 3 4 5
Piglet 0.1 0.6 0.2 0.1 0.0
Pooh 0.0 0.0 0.1 0.6 0.3
200
TWO-SAMPLE MANN–WHITNEY U TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Summarize(Likert ~ Speaker,
data=Data,
digits=3)
wilcox.test(Likert ~ Speaker,
data=Data)
201
TWO-SAMPLE MANN–WHITNEY U TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
### You may get a "cannot compute exact p-value with ties" error.
### You can ignore this or use the exact=FALSE option.
Exercises J
What were the first and third quartiles for each instructor’s scores?
Are the data for both instructors reasonably symmetric about their medians?
Based on your previous answer, what is the null hypothesis for the Mann–Whitney test?
According to the Mann–Whitney test, is there a difference in scores between the instructors?
How would you summarize the results of the descriptive statistics and tests?
2. Brian and Stewie Griffin want to assess the education level of students in their courses on creative
writing for adults. They want to know the median education level for each class, and if the education
level of the classes were different between instructors.
202
MOOD’S MEDIAN TEST FOR TWO-SAMPLE DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
'Brian Griffin' j 4
'Brian Griffin' k 3
'Brian Griffin' l 2
'Stewie Griffin' m 4
'Stewie Griffin' n 5
'Stewie Griffin' o 4
'Stewie Griffin' p 4
'Stewie Griffin' q 4
'Stewie Griffin' r 4
'Stewie Griffin' s 3
'Stewie Griffin' t 5
'Stewie Griffin' u 4
'Stewie Griffin' v 4
'Stewie Griffin' w 3
'Stewie Griffin' x 2
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
What was the median score for each instructor? (Be sure to report the education level, not just
the numeric code!)
What were the first and third quartiles for each instructor’s scores?
Are the data for both instructors reasonably symmetric about their medians?
Based on your previous answer, what is the null hypothesis for the Mann–Whitney test?
According to the Mann–Whitney test, is there a difference in scores between the instructors?
Plot Brian and Stewie’s data in a way that helps you visualize the data. Do the results reflect
what you would expect from looking at the plot? How would you summarize the results of
the descriptive statistics and tests?
The test can be conducted with the mood.medtest function in the RVAideMemoire package.
Appropriate data
• One-way data with two or more groups
• Dependent variable is ordinal, interval, or ratio
203
MOOD’S MEDIAN TEST FOR TWO-SAMPLE DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Hypotheses
• Null hypothesis: The medians of values for each group are equal.
• Alternative hypothesis (two-sided): The medians of values for each group are not equal.
Interpretation
Significant results can be reported as “The median value of group A was significantly different
from group B.”
The following commands will install these packages if they are not already installed:
if(!require(RVAideMemoire)){install.packages("RVAideMemoire")}
This example uses the formula notation indicating that Likert is the dependent variable and Speaker is
the independent variable. The data= option indicates the data frame that contains the variables. For
the meaning of other options, see ?mood.medtest.
For appropriate plots and data frame checking, see the Two-sample Mann–Whitney U Test chapter.
Input =("
Speaker Likert
Pooh 3
Pooh 5
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 5
Pooh 5
Piglet 2
Piglet 4
Piglet 2
Piglet 2
Piglet 1
Piglet 2
204
TWO-SAMPLE PAIRED RANK-SUM TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Piglet 3
Piglet 2
Piglet 2
Piglet 3
")
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
mood.medtest(Likert ~ Speaker,
data = Data)
p-value = 0.001093
The two-sample rank-sum test for paired data is used to compare values for two groups where each
observation in one group is paired with one observation in the other group. The distribution of
differences in the paired samples should be symmetric in shape.
The test is useful to compare scores on a pre-test vs. scores on a post-test, or scores or ratings from
two speakers, two different presentations, or two groups of audiences when there is a reason to pair
observations, such as being done by the same rater.
205
TWO-SAMPLE PAIRED RANK-SUM TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
A discussion of paired data can be found in the Independent and Paired Values chapter of this book.
The test is performed with the wilcox.test function with the paired=TRUE option.
Appropriate data
• Two-sample paired data. That is, one-way data with two groups only, where the
observations are paired between groups.
• Dependent variable is ordinal, interval, or ratio
• Independent variable is a factor with two levels. That is, two groups
• The distribution of differences in paired samples is symmetric
Hypotheses
• Null hypothesis: The distribution of the differences in paired values is symmetric around
zero.
• Alternative hypothesis (two-sided): The distribution of the differences in paired values is not
symmetric around zero.
Interpretation
Significant results can be reported as “Values for group A were significantly different from
those of group B.”
Another alternative is to use cumulative link models for ordinal data, which are described later in this
book.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(BSDA)){install.packages("BSDA")}
For this example, imagine we want to compare scores for Pooh between Time 1 and Time 2. Here,
we’ve recorded the identity of the student raters, and Pooh’s score for each rater. This allows us to
focus on the changes for each rater between Time 1 and Time 2. This makes for a more-powerful test
than would the Mann–Whitney U test in cases like this where one rater might tend to rate high and
206
TWO-SAMPLE PAIRED RANK-SUM TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
another rater might tend to rate low, but there is an overall trend in how raters change their scores
between Time 1 and Time 2.
Note in this example we needed to record the identity of the student rater so that a rater’s score from
Time 1 can be paired with their score from Time 2. If we cannot pair data in this way—for example, if
we did not record the identity of the raters—the data would have to be treated as unpaired,
independent samples, for example like those in the Two-sample Mann–Whitney U Test chapter.
Also note that the data is arranged in long form. In this form, the data must be ordered so that the first
observation where Time = 1 is paired to the first observation where Time = 2, and so on.
Input =("
Speaker Time Student Likert
Pooh 1 a 1
Pooh 1 b 4
Pooh 1 c 3
Pooh 1 d 3
Pooh 1 e 3
Pooh 1 f 3
Pooh 1 g 4
Pooh 1 h 3
Pooh 1 i 3
Pooh 1 j 3
Pooh 2 a 4
Pooh 2 b 5
Pooh 2 c 4
Pooh 2 d 5
Pooh 2 e 4
Pooh 2 f 5
Pooh 2 g 3
Pooh 2 h 4
Pooh 2 i 3
Pooh 2 j 4
")
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
207
TWO-SAMPLE PAIRED RANK-SUM TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Note that the points in the plot are jittered slightly so that points which would fall directly on top of
one another can be seen.
First, two new variables, Time.1 and Time.2, are created by extracting the values of Likert for
observations with the Time variable equal to 1 or 2, respectively, and then the plot is produced.
Time.1 = Data$Likert[Data$Time==1]
Time.2 = Data$Likert[Data$Time==2]
plot(Time.1, jitter(Time.2), # jitter offsets points so you can see them all
pch = 16, # shape of points
cex = 1.0, # size of points
xlim=c(1, 5.5), # limits of x axis
ylim=c(1, 5.5), # limits of y axis
xlab="Time 1",
ylab="Time 2"
)
abline(0,1, col="blue", lwd=2) # line with intercept of 0 and slope of 1
New variables are first created for Time.1, Time.2, and their Difference. And then the plot is produced.
208
TWO-SAMPLE PAIRED RANK-SUM TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Time.1 = Data$Likert[Data$Time==1]
Time.2 = Data$Likert[Data$Time==2]
Here, new variables are created: Time.1, Time.2, Difference, and Diff.f, which has the same values as
Difference but as a factor variable. The xtabs function is used to create a count of values of Diff.f. The
barplot function then uses these counts.
Time.1 = Data$Likert[Data$Time==1]
Time.2 = Data$Likert[Data$Time==2]
Diff.f = factor(Difference,
ordered = TRUE)
X = xtabs(~ Diff.f)
barplot(X,
col="dark gray",
xlab="Difference in Likert",
ylab="Frequency")
209
TWO-SAMPLE PAIRED RANK-SUM TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
This example uses the formula notation indicating that Likert is the dependent variable and Time is the
independent variable. The data= option indicates the data frame that contains the variables, and
paired=TRUE indicates that the test for paired data should be used. For the meaning of other options,
see ?wilcox.test.
wilcox.test(Likert ~ Time,
data = Data,
paired = TRUE,
conf.int = TRUE,
conf.level = 0.95)
### You may get a "cannot compute exact p-value with ties" error.
### You can ignore this or use the exact=FALSE option.
210
TWO-SAMPLE PAIRED RANK-SUM TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Exercises K
What do the plots suggest about the relative value of the scores for Time 1 and Time 2? Do they
suggest that scores increased, decreased, or stayed the same between Time 1 and Time 2?
Is the distribution of the differences between paired samples relatively symmetrical? Is the
two-sample paired ranked-sum test appropriate in this case?
Does the two-sample paired ranked-sum test indicate that there is a significant difference
between Time 1 and Time 2?
2. Lois Griffin gave proficiency scores to her students in her course on piano playing for adults. She
gave a score for each student for their left hand playing and right hand playing. She wants to know if
students in her class are more proficient in the right hand, left hand, or if there is no difference in
hands.
Instructor Student Hand Score
'Lois Griffin' a left 8
'Lois Griffin' a right 9
'Lois Griffin' b left 6
'Lois Griffin' b right 5
'Lois Griffin' c left 7
'Lois Griffin' c right 9
'Lois Griffin' d left 6
'Lois Griffin' d right 7
'Lois Griffin' e left 7
'Lois Griffin' e right 7
'Lois Griffin' f left 9
'Lois Griffin' f right 9
'Lois Griffin' g left 4
'Lois Griffin' g right 6
'Lois Griffin' h left 5
'Lois Griffin' h right 8
'Lois Griffin' i left 5
'Lois Griffin' i right 6
'Lois Griffin' j left 7
'Lois Griffin' j right 8
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
Is the distribution of the differences between paired samples relatively symmetrical? Is the
two-sample paired ranked-sum test appropriate in this case?
211
SIGN TEST FOR TWO-SAMPLE PAIRED DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Does the two-sample paired ranked-sum test indicate that there is a difference between hands?
If so, which hand received higher scores?
What can you conclude about the results of the plots, summary statistics, and statistical test?
What if Lois wanted to change the design of the experiment so that she could determine if each
student were more proficient in one hand or the other? That is, is student a more proficient
in left hand or right? Is student b more proficient in left hand or right? How should she
change what data she’s collecting to determine this?
The SIGN.test function in the BSDA package requires the data to be separated into two variables, each
of which is ordered so that the first observation of each are paired, and so on. Information on options
for the function can be viewed with ?SIGN.test. The SignTest function in the DescTools package is
similar.
For appropriate plots see the Two-sample Paired Rank-sum Test chapter.
Appropriate data
• Two-sample paired data. That is, one-way data with two groups only, where the
observations are paired between groups.
• Dependent variable is ordinal, interval, or ratio
• Independent variable is a factor with two levels. That is, two groups
Hypotheses
• Null hypothesis: The median of the differences between pairs in group A and group B is zero.
• Alternative hypothesis (two-sided): The median of the differences between pairs in group A
and group B is not zero.
Interpretation
Significant results can be reported as “There was a significant difference in values between
group A and group B.”
212
SIGN TEST FOR TWO-SAMPLE PAIRED DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(BSDA)){install.packages("BSDA")}
if(!require(DescTools)){install.packages("DescTools")}
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
213
SIGN TEST FOR TWO-SAMPLE PAIRED DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(BSDA)
SIGN.test(x = Time.1,
y = Time.2,
alternative = "two.sided",
conf.level = 0.95)
Dependent-samples Sign-Test
S = 1, p-value = 0.03906
sample estimates:
median of x-y
-1
library(DescTools)
SignTest(x = Time.1,
y = Time.2)
Dependent-samples Sign-Test
sample estimates:
median of the differences
-1
214
KRUSKAL–WALLIS TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Kruskal–Wallis Test
The Kruskal–Wallis test is a rank-based test that is similar to the Mann–Whitney U test, but can be
applied to one-way data with more than two groups. The test is useful to compare the scores or
ratings from multiple speakers, presentations, or groups of audiences.
If the shape and spread of the distributions of values of each group is similar, then the test compares
the medians of the two groups. Otherwise, the test is really testing if there is a systematic difference in
the values among the groups.
If the distributions of values of each group are similar in shape, but have outliers, then Mood’s median
test is an appropriate alternative. Mood’s median test is described in the next chapter.
Post-hoc tests
The outcome of the Kruskal–Wallis test tells you if there are differences among the groups, but doesn’t
tell you which groups are different from other groups. In order to determine which groups are
different from others, post-hoc testing can be conducted. Probably the most common post-hoc test for
the Kruskal–Wallis test is the Dunn test, here conducted with the dunnTest function in the FSA
package. An alternative to this is to conduct Mann–Whitney tests on each pair of groups. This is
accomplished with pairwise.wilcox.test function.
Appropriate data
• One-way data
• Dependent variable is ordinal, interval, or ratio
• Independent variable is a factor with two or more levels. That is, two or more groups
• Observations between groups are independent. That is, not paired or repeated measures
data
• In order to be a test of medians, the distributions of values for each group need to be of
similar shape and spread. Otherwise the test is a test of distributions.
Hypotheses
If the distributions of the two groups are similar in shape and spread:
• Null hypothesis: The medians of values for each group are equal.
• Alternative hypothesis (two-sided): The medians of values for each group are not equal.
If the distributions of the two groups are not similar in shape and spread:
• Null hypothesis: The distribution of values for each group are equal.
• Alternative hypothesis (two-sided): There is systematic difference in the distribution of
values for the groups.
215
KRUSKAL–WALLIS TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Interpretation
If the distributions of the two groups are similar in shape and spread:
Significant results can be reported as “There was a significant difference in median values
across groups.”
Post-hoc analysis allows you to say “The median for group A was higher than the median for
group B”, and so on.
If the distributions of the two groups are not similar in shape and spread:
Significant results can be reported as “There was a significant difference in values among
groups.”
Another alternative is to use cumulative link models for ordinal data, which are described later in this
book.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(lattice)){install.packages("lattice")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(rcompanion)){install.packages("rcompanion")}
This example re-visits the Pooh, Piglet, and Tigger data from the Descriptive Statistics with the likert
Package chapter.
It answers the question, “Are the scores significantly different among the three speakers?”
The Kruskal–Wallis test is conducted with the kruskal.test function, which produces a p-value for the
hypothesis. First the data are summarized and examined using bar plots for each group.
Note that because the bar plot show that the distributions of scores for each of the speakers are
relatively similar in shape and spread, the Kruskal–Wallis test can be interpreted as a test of medians.
216
KRUSKAL–WALLIS TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Input =("
Speaker Likert
Pooh 3
Pooh 5
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 5
Pooh 5
Piglet 2
Piglet 4
Piglet 2
Piglet 2
Piglet 1
Piglet 2
Piglet 3
Piglet 2
Piglet 2
Piglet 3
Tigger 4
Tigger 4
Tigger 4
Tigger 4
Tigger 5
Tigger 3
Tigger 5
Tigger 4
Tigger 4
Tigger 3
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Speaker = factor(Data$Speaker,
levels=unique(Data$Speaker))
### Create a new variable which is the likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered = TRUE)
library(psych)
headTail(Data)
str(Data)
217
KRUSKAL–WALLIS TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
summary(Data)
rm(Input)
Likert.f
Speaker 1 2 3 4 5
Pooh 0 0 1 6 3
Piglet 1 6 2 1 0
Tigger 0 0 2 6 2
prop.table(XT,
margin = 1)
Likert.f
Speaker 1 2 3 4 5
Pooh 0.0 0.0 0.1 0.6 0.3
Piglet 0.1 0.6 0.2 0.1 0.0
Tigger 0.0 0.0 0.2 0.6 0.2
218
KRUSKAL–WALLIS TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Summarize(Likert ~ Speaker,
data=Data,
digits=3)
219
KRUSKAL–WALLIS TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Probably the most popular host-hoc test for the Kruskal–Wallis test is the Dunn test. The Dunn test
can be conducted with the dunnTest function in the FSA package.
Because the post-hoc test will produce multiple p-values, adjustments to the p-values can be made to
avoid inflating the possibility of making a type-I error. There are a variety of methods for controlling
the familywise error rate or for controlling the false discovery rate. See ?p.adjust for details on these
methods.
When there are many p-values to evaluate, it is useful to condense a table of p-values to a compact
letter display format. In the output, groups are separated by letters. Groups sharing the same letter
are not significantly different. Compact letter displays are a clear and succinct way to present results
of multiple comparisons.
### Order groups by median
Data$Speaker = factor(Data$Speaker,
levels=c("Pooh", "Tigger", "Piglet"))
levels(Data$Speaker)
library(FSA)
DT = dunnTest(Likert ~ Speaker,
data=Data,
method="bh") # Adjusts p-values for multiple comparisons;
# See ?dunnTest for options
DT
PT = DT$res
PT
library(rcompanion)
220
KRUSKAL–WALLIS TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
cldList(comparison = PT$Comparison,
p.value = PT$P.adj,
threshold = 0.05)
This can be conducted with the pairwise.wilcox.test function. This produces a table of p-values
comparing each pair of groups.
To prevent the inflation of type I error rates, adjustments to the p-values can be made using the
p.adjust.method option. Here the fdr method is used. See ?p.adjust for details on available p-value
adjustment methods.
When there are many p-values to evaluate, it is useful to condense a table of p-values to a compact
letter display format. This can be accomplished with a combination of the fullPTable function in the
rcompanion package and the multcompLetters function in the multcompView package.
A compact letter display condenses a table of p-values into a simpler format. In the output, groups are
separated by letters. Groups sharing the same letter are not significantly different. Compact letter
displays are a clear and succinct way to present results of multiple comparisons.
Here the fdr p-value adjustment method is used. See ?p.adjust for details on available methods.
The code creates a matrix of p-values called PT, then converts this to a fuller matrix called PT1. PT1 is
then passed to the multcompLetters function to be converted to a compact letter display.
Note that the p-value results of the pairwise Mann–Whitney U-tests differ somewhat from those of the
Dunn test.
### Order groups by median
Data$Speaker = factor(Data$Speaker,
levels=c("Pooh", "Tigger", "Piglet"))
Data
221
KRUSKAL–WALLIS TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
PT = pairwise.wilcox.test(Data$Likert,
Data$Speaker,
p.adjust.method="fdr")
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
PT
Pooh Tigger
Tigger 0.5174 -
Piglet 0.0012 0.0012
### Note that the values in the table are p-values comparing each
### pair of groups.
library(rcompanion)
PT1 = fullPTable(PT)
PT1
library(multcompView)
multcompLetters(PT1,
compare="<",
threshold=0.05, # p-value to use as significance threshold
Letters=letters,
reversed = FALSE)
222
KRUSKAL–WALLIS TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The following code uses the groupwiseMedian function to produce a data frame of medians for each
speaker along with the 95% confidence intervals for each median with the percentile method.
These medians are then plotted, with their confidence intervals shown as error bars. The grouping
letters from the multiple comparisons (Dunn test or pairwise Mann–Whitney U-tests) are added.
library(rcompanion)
Sum = groupwiseMedian(data=Data,
group="Speaker",
var="Likert",
conf=0.95,
R=5000,
percentile=TRUE,
bca=FALSE,
digits=3)
Sum
X = 1:3
Y = Sum$Percentile.upper + 0.2
Label = c("a", "b", "a")
library(ggplot2)
annotate("text",
x = X,
y = Y,
label = Label)
223
KRUSKAL–WALLIS TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Plot of median Likert versus Speaker. Error bars indicate the 95% confidence
intervals for the median with the percentile method.
Exercises L
Are the data for all instructors reasonably similar shape and spread?
Based on your previous answer, what is the null hypothesis for the Kruskal–Wallis test?
According to the Kruskal–Wallis test, is there a statistical difference in scores among the
instructors?
Looking at the post-hoc analysis, which speakers’ scores are statistically different from which
others? Who had the statistically highest scores?
2. Brian, Stewie, and Meg want to assess the education level of students in their courses on creative
writing for adults. They want to know the median education level for each class, and if the education
level of the classes were different among instructors.
224
KRUSKAL–WALLIS TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
225
MOOD’S MEDIAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
What was the median education level for each instructor’s class? (Be sure to report the
education level, not just the numeric code!)
Are the distributions of education levels for all instructors reasonably similar shape and
spread?
Based on your previous answer, what is the null hypothesis for the Kruskal–Wallis test?
According to the Kruskal–Wallis test, is there a difference in the education level of students
among the instructors?
Looking at the post-hoc analysis, which classes education levels are different from which
others? Who had the statistically highest education level?
Plot Brian, Stewie, and Meg’s data in a way that helps you visualize the data. Do the results
reflect what you would expect from looking at the plot? How would you summarize the
results of the descriptive statistics and tests?
The test can be conducted with the mood.medtest function in the RVAideMemoire package.
Post-hoc tests
The outcome of Mood’s median test tells you if there are differences among the groups, but doesn’t tell
you which groups are different from other groups. In order to determine which groups are different
from others, post-hoc testing can be conducted. The function pairwiseMedianTest in the rcompanion
package can perform the post-hoc tests.
Appropriate data
• One-way data with two or more groups
• Dependent variable is ordinal, interval, or ratio
• Independent variable is a factor with levels indicating groups
• Observations between groups are independent. That is, not paired or repeated measures
data
• Distributions of values for each group are similar in shape; however, the test is not sensitive
to outliers
Hypotheses
• Null hypothesis: The medians of values for each group are equal.
• Alternative hypothesis (two-sided): The medians of values for each group are not equal.
226
MOOD’S MEDIAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Interpretation
Significant results can be reported as “There was a significant difference in the median values
among groups.”
Post-hoc analysis allows you to say “The median for group A was higher than the median for
group B”, and so on.
The following commands will install these packages if they are not already installed:
if(!require(RVAideMemoire)){install.packages("RVAideMemoire")}
if(!require(rcompanion)){install.packages("rcompanion")}
This example uses the formula notation indicating that Likert is the dependent variable and Speaker is
the independent variable. The data= option indicates the data frame that contains the variables. For
the meaning of other options, see ?mood.medtest.
A significant p-value for Mood’s median test indicates that not all medians among groups are equal.
For appropriate plots and data frame checking, see the Kruskal–Wallis Test chapter.
Input =("
Speaker Likert
Pooh 3
Pooh 5
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 5
Pooh 5
Piglet 2
Piglet 4
Piglet 2
Piglet 2
Piglet 1
Piglet 2
Piglet 3
Piglet 2
Piglet 2
227
MOOD’S MEDIAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Piglet 3
Tigger 4
Tigger 4
Tigger 4
Tigger 4
Tigger 5
Tigger 3
Tigger 5
Tigger 4
Tigger 4
Tigger 3
")
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
mood.medtest(Likert ~ Speaker,
data = Data)
p-value = 0.3211
An interesting thing happened with the result here. The test counts how many observations in each
group are greater than the global median for all groups together, in this case 4. It then tests if there is
a significant difference in this proportion among groups. For this data set, however, both Pooh and
Tigger have a majority of observations equal to the global median. Because they are equal to the global
median, they are not greater than the global median, and so aren’t much different than Piglet’s scores
on this count. The result in this case is a non-significant p-value.
But the test would come out differently if we were counting observation less than the global median,
because Pooh and Tigger have few of these, and Piglet has relatively many.
228
MOOD’S MEDIAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
This is a quirk with Mood’s median test, and isn’t common in statistical tests.
One solution would be to re-code the function to count observations less than the global median. This
would be possible.
But it is easier to simply invert the scale we are using. This is really an arbitrary change, but for this
test, it can make a difference. Imagine if our original scale interpreted 5 to be the best, and 1 to be the
worst. When we designed the survey tool, we could just as easily have made 1 the best and 5 the
worst. And then instead of ranking “good” with a 4, the respondents would have marked it 2, and so
on. By the way the calculations are done, this arbitrary change in scale will change the results of
Mood’s median test.
For a 5-point scale, we do this inversion by simply by making a new variable equal to 6 minus the
original score.
With Mood’s median test, I recommend making this kind of inversion in cases where many values are
equal to the global median. Then use whichever result has a lower p-value.
Data$Likert.inv = 6 - Data$Likert
library(psych)
headTail(Data)
library(RVAideMemoire)
mood.medtest(Likert.inv ~ Speaker,
data = Data)
p-value = 0.000402
229
MOOD’S MEDIAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For this we will use the pairwiseMedianTest function in the rcompanion package, which conducts
Mood’s median test on all pairs of groups from one-way data.
Because the post-hoc test will produce multiple p-values, adjustments to the p-values can be made to
avoid inflating the possibility of making a type-I error. There are a variety of methods for controlling
the familywise error rate or for controlling the false discovery rate. See ?p.adjust for details on these
methods.
### Order groups by median
Data$Speaker = factor(Data$Speaker,
levels=c("Pooh", "Tigger", "Piglet"))
library(rcompanion)
PT = pairwiseMedianTest(x = Data$Likert,
g = Data$Speaker,
exact = NULL,
method = "fdr")
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
PT
library(rcompanion)
cldList(comparison = PT$Comparison,
p.value = PT$p.adjust,
threshold = 0.05)
230
FRIEDMAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data$Speaker = factor(Data$Speaker,
levels=c("Pooh", "Tigger", "Piglet"))
library(rcompanion)
PT = pairwiseMedianMatrix(x = Data$Likert,
g = Data$Speaker,
exact = NULL,
method = "fdr")
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
PT
library(multcompView)
multcompLetters(PT$Adjusted,
compare="<",
threshold=0.05,
Letters=letters)
Friedman Test
The Friedman test determines if there are differences among groups for two-way data structured in a
specific way, namely in an unreplicated complete block design. In this design, one variable serves as the
treatment or group variable, and another variable serves as the blocking variable. It is the differences
among treatments or groups that we are interested in. We aren’t necessarily interested in differences
among blocks, but we want our statistics to take into account differences in the blocks. In the
unreplicated complete block design, each block has one and only one observation of each treatment.
For an example of this structure, look at the Belcher family data below. Rater is considered the
blocking variable, and each rater has one observation for each Instructor. The test will determine if
there are differences among values for Instructor, taking into account any consistent effect of a Rater.
For example if Rater a rated consistently low and Rater g rated consistently high, the Friedman test
can account for this statistically.
In other cases, the blocking variable might be the class where the ratings were done or the school
where the ratings were done. If you were testing differences among curricula or other teaching
treatments with different instructors, different instructors might be used as blocks.
231
FRIEDMAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
If the distribution of the differences in scores between each pair of groups are all symmetrical, or if the
distribution of values for each group is similar in shape and spread, the Friedman test determines if
there is a difference in medians among groups. If not, the test determines if there is a systematic
difference in the values among the groups.
Some people critique the Friedman test for having low power in detecting differences among groups.
It has been suggested, however, that Friedman test may be powerful when there are five or more
groups.
Post-hoc tests
The outcome of the Friedman test tells you if there are differences among the groups, but doesn’t tell
you which groups are different from other groups. In order to determine which groups are different
from others, post-hoc testing can be conducted.
For a post-hoc analysis, the pairwiseSignTest function can be used. It performs a two-sample paired
sign test on each pair of groups.
Appropriate data
• Two-way data arranged in an unreplicated complete block design
• Dependent variable is ordinal, interval, or ratio
• Treatment or group independent variable is a factor with two or more levels. That is, two or
more groups
• Blocking variable is a factor with two or more levels
• Blocks are independent of each other and have no interaction with treatments
• In order to be a test of medians, the distribution of the differences in scores between each
pair of groups are all symmetrical, or the distributions of values for each group have similar
shape and spread. Otherwise the test is a test of distributions.
Hypotheses
If the distribution of the differences in scores between each pair of groups are all symmetrical, or the
distributions of values for each group have similar shape and spread:
• Null hypothesis: The medians of values for each group are equal.
• Alternative hypothesis (two-sided): The medians of values for each group are not equal.
Interpretation
If the distribution of the differences in scores between each pair of groups are all symmetrical, or the
distributions of values for each group have similar shape and spread:
Significant results can be reported as “There was a significant difference in median values
across groups.”
Post-hoc analysis allows you to say “The median for group A was higher than the median for
group B”, and so on.
232
FRIEDMAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Another alternative is to use cumulative link models for ordinal data, which are described later in this
book.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(lattice)){install.packages("lattice")}
if(!require(BSDA)){install.packages("BSDA")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(PMCMR)){install.packages("PMCMR")}
if(!require(rcompanion)){install.packages("rcompanion")}
233
FRIEDMAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
'Linda Belcher' d 8
'Linda Belcher' e 8
'Linda Belcher' f 7
'Linda Belcher' g 10
'Linda Belcher' h 9
'Tina Belcher' a 7
'Tina Belcher' b 5
'Tina Belcher' c 7
'Tina Belcher' d 8
'Tina Belcher' e 8
'Tina Belcher' f 9
'Tina Belcher' g 10
'Tina Belcher' h 9
'Gene Belcher' a 6
'Gene Belcher' b 4
'Gene Belcher' c 5
'Gene Belcher' d 5
'Gene Belcher' e 6
'Gene Belcher' f 6
'Gene Belcher' g 5
'Gene Belcher' h 5
'Louise Belcher' a 8
'Louise Belcher' b 7
'Louise Belcher' c 8
'Louise Belcher' d 8
'Louise Belcher' e 9
'Louise Belcher' f 9
'Louise Belcher' g 8
'Louise Belcher' h 10
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Instructor = factor(Data$Instructor,
levels=unique(Data$Instructor))
### Create a new variable which is the likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
234
FRIEDMAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
rm(Input)
Likert.f
Instructor 4 5 6 7 8 9 10
Bob Belcher 2 1 4 0 0 0 1
Linda Belcher 0 0 1 1 4 1 1
Tina Belcher 0 1 0 2 2 2 1
Gene Belcher 1 4 3 0 0 0 0
Louise Belcher 0 0 0 1 4 2 1
prop.table(XT,
margin = 1)
Likert.f
Instructor 4 5 6 7 8 9 10
Bob Belcher 0.250 0.125 0.500 0.000 0.000 0.000 0.125
Linda Belcher 0.000 0.000 0.125 0.125 0.500 0.125 0.125
Tina Belcher 0.000 0.125 0.000 0.250 0.250 0.250 0.125
Gene Belcher 0.125 0.500 0.375 0.000 0.000 0.000 0.000
Louise Belcher 0.000 0.000 0.000 0.125 0.500 0.250 0.125
235
FRIEDMAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Note that the data must be ordered by the blocking variable so that the first observation for Louisa will
be paired with the first observation for Bob, and so on.
Also note that we had to specify the levels in the factor function defining Diff.f. This is so that the
values with zero counts will be displayed on the plot.
Bob = Data$Likert [Data$Instructor == "Bob Belcher"]
Louisa = Data$Likert [Data$Instructor == "Louise Belcher"]
Diff.f = factor(Difference,
ordered = TRUE,
levels = c("-4", "-3", "-2", "-1", "0", "1", "2", "3", "4")
236
FRIEDMAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
X = xtabs(~ Diff.f)
barplot(X,
col="dark gray",
xlab="Difference in Likert",
ylab="Frequency")
Note that the data must be ordered by the blocking variable so that the first observation for Bob will be
paired with the first observation for Linda, and so on.
library(rcompanion)
Data.diff=pairwiseDifferences(Data$Likert,
Data$Instructor,
factorize = TRUE,
plotit = TRUE)
library(psych)
headTail(Data.diff)
237
FRIEDMAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(lattice)
Summarize(Likert ~ Instructor,
data=Data,
digits=3)
238
FRIEDMAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
To prevent the inflation of type I error rates, adjustments to the p-values can be made using the
p.adjust.method option. See ?p.adjust for details on available p-value adjustment methods.
It has been suggested that the sign test may lack power in detecting differences in paired data sets.
But is useful because it has few assumptions about the distributions of the data to compare, and is the
test analogous to the Friedman test with two groups.
Data$Instructor = factor(Data$Instructor,
levels = c("Linda Belcher", "Louise Belcher",
"Tina Belcher", "Bob Belcher",
"Gene Belcher"))
library(rcompanion)
PT = pairwiseSignTest(Data$Likert,
Data$Instructor,
method="fdr")
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
PT
239
FRIEDMAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(rcompanion)
cldList(comparison = PT$Comparison,
p.value = PT$p.adjust,
threshold = 0.05)
Data$Instructor = factor(Data$Instructor,
levels = c("Linda Belcher", "Louise Belcher",
"Tina Belcher", "Bob Belcher",
"Gene Belcher"))
library(rcompanion)
PM = pairwiseSignMatrix(Data$Likert,
Data$Instructor,
method="fdr")
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
240
FRIEDMAN TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
PM
$Adjusted
Linda Belcher Louise Belcher Tina Belcher Bob Belcher Gene Belcher
Linda Belcher 1.00000 0.46880 0.68750 0.03908 0.02604
Louise Belcher 0.46880 1.00000 0.31240 0.11720 0.02604
Tina Belcher 0.68750 0.31240 1.00000 0.06250 0.02604
Bob Belcher 0.03908 0.11720 0.06250 1.00000 0.68750
Gene Belcher 0.02604 0.02604 0.02604 0.68750 1.00000
library(multcompView)
multcompLetters(PM$Adjusted,
compare="<",
threshold=0.05, # p-value to use as significance threshold
Letters=letters,
reversed = FALSE)
Linda Belcher Louise Belcher Tina Belcher Bob Belcher Gene Belcher
"a" "ab" "ab" "bc" "c"
Data$Instructor = factor(Data$Instructor,
levels = c("Linda Belcher", "Louise Belcher",
"Tina Belcher", "Bob Belcher",
"Gene Belcher"))
library(PMCMR)
PT = posthoc.friedman.conover.test(y = Data$Likert,
groups = Data$Instructor,
blocks = Data$Rater,
p.adjust.method="fdr")
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
PT
241
QUADE TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
PT0 = as.matrix(PT$p.value)
library(rcompanion)
PT1 = fullPTable(PT0)
library(multcompView)
multcompLetters(PT1,
compare="<",
threshold=0.05,
Letters=letters,
reversed = FALSE)
Linda Belcher Louise Belcher Tina Belcher Bob Belcher Gene Belcher
"a" "a" "a" "b" "b"
Quade Test
The Quade test is used for similar data and hypotheses as the Friedman test, namely for unreplicated
complete block designs.
While the Friedman test is a generalization of the paired sign test, the Quade test is a generalization of
the two-sample signed-rank test.
Some authors indicate that the Quade test is appropriate for ordinal data, while others suggest that it
is appropriate only for interval or ratio data. It is included in this section as an alternative to the
Friedman test.
Post-hoc tests
The outcome of the Quade test tells you if there are differences among the groups, but doesn’t tell you
which groups are different from other groups. In order to determine which groups are different from
others, post-hoc testing can be conducted with the pairwise.wilcox.test function.
242
QUADE TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Appropriate data
• Two-way data arranged in an unreplicated complete block design
• Dependent variable is ordinal, interval, or ratio, although some authors say data must be
interval or ratio only.
• Treatment or group independent variable is a factor with two or more levels. That is, two or
more groups
• Blocking variable is a factor with two or more levels
• Blocks are independent of each other and have no interaction with treatments
• In order to be a test of medians, the distributions of values for each group should have similar
shape and spread. If not, the test determines if there is a systematic difference in the values
among the groups.
Hypotheses
If the distribution of the differences in scores between each pair of groups are all symmetrical, or the
distributions of values for each group have similar shape and spread:
• Null hypothesis: The medians of values for each group are equal.
• Alternative hypothesis (two-sided): The medians of values for each group are not equal.
Interpretation
If the distribution of the differences in scores between each pair of groups are all symmetrical, or the
distributions of values for each group have similar shape and spread:
Significant results can be reported as “There was a significant difference in median values
across groups.”
Post-hoc analysis allows you to say “The median for group A was higher than the median for
group B”, and so on.
Another alternative is to use cumulative link models for ordinal data, which are described later in this
book.
243
QUADE TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
• FSA
• lattice
• multcompView
• PMCMR
• rcompanion
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(lattice)){install.packages("lattice")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(PMCMR)){install.packages("PMCMR")}
if(!require(rcompanion)){install.packages("rcompanion")}
244
QUADE TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
'Louise Belcher' b 7
'Louise Belcher' c 8
'Louise Belcher' d 8
'Louise Belcher' e 9
'Louise Belcher' f 9
'Louise Belcher' g 8
'Louise Belcher' h 10
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Instructor = factor(Data$Instructor,
levels=unique(Data$Instructor))
### Create a new variable which is the likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Likert.f
Instructor 4 5 6 7 8 9 10
Bob Belcher 2 1 4 0 0 0 1
Linda Belcher 0 0 1 1 4 1 1
Tina Belcher 0 1 0 2 2 2 1
Gene Belcher 1 4 3 0 0 0 0
Louise Belcher 0 0 0 1 4 2 1
245
QUADE TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
prop.table(XT,
margin = 1)
Likert.f
Instructor 4 5 6 7 8 9 10
Bob Belcher 0.250 0.125 0.500 0.000 0.000 0.000 0.125
Linda Belcher 0.000 0.000 0.125 0.125 0.500 0.125 0.125
Tina Belcher 0.000 0.125 0.000 0.250 0.250 0.250 0.125
Gene Belcher 0.125 0.500 0.375 0.000 0.000 0.000 0.000
Louise Belcher 0.000 0.000 0.000 0.125 0.500 0.250 0.125
246
QUADE TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Summarize(Likert ~ Instructor,
data=Data,
digits=3)
247
QUADE TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Quade test
Post-hoc test: pairwise two-sample paired rank-sum test for multiple comparisons
To prevent the inflation of type I error rates, adjustments to the p-values can be made using the
p.adjust.method option. See ?p.adjust for details on available p-value adjustment methods.
Note that the data must be ordered by the blocking variable so that the first observation for Bob will be
paired with the first observation for Linda, and so on.
### Order groups by median
Data$Instructor = factor(Data$Instructor,
levels=c("Linda Belcher", "Louise Belcher",
"Tina Belcher", "Bob Belcher",
"Gene Belcher"))
Data
PT = pairwise.wilcox.test(Data$Likert,
Data$Instructor,
p.adjust.method="fdr",
paired=TRUE)$p.value
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
PT
library(rcompanion)
PT1 = fullPTable(PT)
PT1
Linda Belcher Louise Belcher Tina Belcher Bob Belcher Gene Belcher
Linda Belcher 1.00000000 0.61269120 0.85010674 0.05090808 0.04637053
Louise Belcher 0.61269120 1.00000000 0.47198578 0.05090808 0.04637053
248
QUADE TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(multcompView)
multcompLetters(PT1,
compare="<",
threshold=0.05, # p-value to use as significance threshold
Letters=letters,
reversed = FALSE)
Linda Belcher Louise Belcher Tina Belcher Bob Belcher Gene Belcher
"a" "a" "a" "ab" "b"
Data$Instructor = factor(Data$Instructor,
levels=c("Linda Belcher", "Louise Belcher",
"Tina Belcher", "Bob Belcher",
"Gene Belcher"))
Data
library(PMCMR)
PT = posthoc.quade.test(y = Data$Likert,
groups = Data$Instructor,
blocks = Data$Rater,
p.adjust.method="fdr")
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
PT
249
NONPARAMETRIC REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
PT0 = as.matrix(PT$p.value)
library(rcompanion)
PT1 = fullPTable(PT0)
library(multcompView)
multcompLetters(PT1,
compare="<",
threshold=0.05,
Letters=letters,
reversed = FALSE)
Linda Belcher Louise Belcher Tina Belcher Bob Belcher Gene Belcher
"a" "b" "b" "a" "b"
Nonparametric Regression
There are different techniques that are considered to be forms of nonparametric regression. Kendall–
Theil regression fits a linear model between one x variable and one y variable using a completely
nonparametric approach. Quantile regression is a very flexible approach that can find a linear
relationship between a dependent variable and one or more independent variables. Local regression
fits a smooth curve to the dependent variable, and can accommodate multiple independent variables.
Generalized additive models are a powerful and flexible approach.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
250
NONPARAMETRIC REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
if(!require(mblm)){install.packages("mblm")}
if(!require(quantreg)){install.packages("quantreg")}
if(!require(rcompanion)){install.packages("rcompanion")}
if(!require(mgcv)){install.packages("mgcv")}
if(!require(lmtest)){install.packages("lmtest")}
Nonparametric correlation
Data for the examples in this chapter are borrowed from the Correlation and Linear Regression
chapter. In this hypothetical example, students were surveyed for their weight, daily caloric intake,
daily sodium intake, and a score on an assessment of knowledge gain
Input = ("
Instructor Grade Weight Calories Sodium Score
'Brendon Small' 6 43 2069 1287 77
'Brendon Small' 6 41 1990 1164 76
'Brendon Small' 6 40 1975 1177 76
'Brendon Small' 6 44 2116 1262 84
'Brendon Small' 6 45 2161 1271 86
'Brendon Small' 6 44 2091 1222 87
'Brendon Small' 6 48 2236 1377 90
'Brendon Small' 6 47 2198 1288 78
'Brendon Small' 6 46 2190 1284 89
'Jason Penopolis' 7 45 2134 1262 76
'Jason Penopolis' 7 45 2128 1281 80
'Jason Penopolis' 7 46 2190 1305 84
'Jason Penopolis' 7 43 2070 1199 68
'Jason Penopolis' 7 48 2266 1368 85
'Jason Penopolis' 7 47 2216 1340 76
'Jason Penopolis' 7 47 2203 1273 69
'Jason Penopolis' 7 43 2040 1277 86
'Jason Penopolis' 7 48 2248 1329 81
'Melissa Robins' 8 48 2265 1361 67
'Melissa Robins' 8 46 2184 1268 68
'Melissa Robins' 8 53 2441 1380 66
'Melissa Robins' 8 48 2234 1386 65
'Melissa Robins' 8 52 2403 1408 70
'Melissa Robins' 8 53 2438 1380 83
'Melissa Robins' 8 52 2360 1378 74
'Melissa Robins' 8 51 2344 1413 65
'Melissa Robins' 8 51 2351 1400 68
'Paula Small' 9 52 2390 1412 78
'Paula Small' 9 54 2470 1422 62
'Paula Small' 9 49 2280 1382 61
'Paula Small' 9 50 2308 1410 72
'Paula Small' 9 55 2505 1410 80
'Paula Small' 9 52 2409 1382 60
'Paula Small' 9 53 2431 1422 70
251
NONPARAMETRIC REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
Data$Instructor = factor(Data$Instructor,
levels=unique(Data$Instructor))
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
The method yields a slope and intercept for the fit line, and a p-value for the slope can be determined
as well. Typically, no measure analogous to r-squared is reported.
The mblm function in the mblm package uses the Siegel method by default. The Theil–Sen procedure
can be chosen with the repeated=FALSE option. See library(mblm); ?mblm for more details.
252
NONPARAMETRIC REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(mblm)
summary(model.k)
Coefficients:
Estimate MAD V value Pr(>|V|)
(Intercept) -208.5875 608.4540 230 0.000861 ***
Sodium 1.8562 0.4381 1035 5.68e-14 ***
### Values under Estimate are used to determine the fit line.
### MAD is the median absolute deviation, a robust measure of variability
abline(model.k,
col="blue",
lwd=2)
Pvalue = as.numeric(summary(model.k)$coefficients[2,4])
Intercept = as.numeric(summary(model.k)$coefficients[1,1])
Slope = as.numeric(summary(model.k)$coefficients[2,1])
R2 = NULL
253
NONPARAMETRIC REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Quantile regression
While traditional linear regression models the conditional mean of the dependent variable, quantile
regression models the conditional median or other quantile. Medians are most common, but for
example, if the factors predicting the highest values of the dependent variable are to be investigated, a
95th percentile could be used. Likewise, models for several quantiles, e.g. 25th , 50th, 75th percentiles,
could be investigated simultaneously.
Quantile regression makes no assumptions about the distribution of the underlying data, and is robust
to outliers in the dependent variable. It does assume the dependent variable is continuous. However,
there are functions for other types of dependent variables in the qtools package. The model assumes
that the terms are linearly related. Quantile regression is sometimes considered “semiparametric”.
Quantile regression is very flexible in the number and types of independent variables that can be
added to the model. The example, here, however, confines itself to a simple case with one independent
variable and one dependent variable.
This example models the median of dependent variable, which is indicated with the tau = 0.5 option.
A p-value for the model can be found by using the anova function with the fit model and the null model.
A pseudo R-squared value can be found with the nagelkerke function in the rcompanion package.
library(quantreg)
summary(model.q)
254
NONPARAMETRIC REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Coefficients:
coefficients lower bd upper bd
(Intercept) -84.12409 -226.58102 134.91738
Sodium 1.76642 1.59035 1.89615
### Values under Coefficients are used to determine the fit line.
### bd appears to be a confidence interval for the coefficients
model.null = rq(Calories ~ 1,
data = Data,
tau = 0.5)
anova(model.q, model.null)
library(rcompanion)
nagelkerke(model.q)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.115071
Cox and Snell (ML) 0.783920
Nagelkerke (Cragg and Uhler) 0.783921
abline(model,
col="blue",
lwd=2)
library(rcompanion)
255
NONPARAMETRIC REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Local regression
There are several techniques for local regression. The idea is to fit a curve to data by averaging, or
otherwise summarizing, data points that are next to one another. The amount of “wiggliness” of the
curve can be adjusted.
Local regression is useful for investigating the behavior of the response variable in more detail than
would be possible with a simple linear model.
The function loess in the native stats package can be used for one continuous dependent variable and
up to four independent variables. The process is essentially nonparametric, and is robust to outliers in
the dependent variable. Usually no p-value or r-squared are reported. Integer variables have to
coerced to numeric variables.
The plot below shows a basically linear response, but also shows an increase in Calories at the upper
end of Sodium.
Data$Sodium = as.numeric(Data$Sodium)
256
NONPARAMETRIC REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
summary(model.l)
Number of Observations: 45
Equivalent Number of Parameters: 4.19
Residual Standard Error: 91.97
Plot
library(rcompanion)
plotPredy(data = Data,
x = Sodium,
y = Calories,
model = model.l,
xlab = "Calories per day",
ylab = "Sodium intake per day")
Generalized additive models are very flexible, allowing for a variety of types of independent variables
and of dependent variables. A smoother function is often used to create a “wiggly” model analogous to
that used in local regression. The gam function in the mgcv package uses smooth functions plus a
conventional parametric component, and so would probably be classified as a semiparametric
approach. The summary function reports an R-squared value, and p-values for the terms. The anova
function can be used for one model, or to compare two models.
library(mgcv)
257
NONPARAMETRIC REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
summary(model.g)
Parametric coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 2304.87 13.62 169.2 <2e-16 ***
model.null = gam(Calories ~ 1,
data = Data,
family=gaussian())
anova(model.g,
model.null)
library(lmtest)
lrtest(model.g,
model.null)
plotPredy(data = Data,
x = Sodium,
258
NONPARAMETRIC REGRESSION FOR TIME SERIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
y = Calories,
model = model.g,
xlab = "Calories per day",
ylab = "Sodium intake per day")
Pvalue = 2.25e-14
R2 = 0.718
It is my understanding that the tests in this chapter do not take into consideration any autocorrelation
in the data. Autocorrelation would be for example if an observation with a high value would tend to be
followed by another observation with a high value. For the stage data in this chapter, since the stage of
a tidal river is affected by the phase of the moon and by the direction and strength of the wind, it
would be reasonable to assume that the average stage for one day would be correlated to the previous
day. Due to this limitation in these tests, there are two conditions under which these they are usually
259
NONPARAMETRIC REGRESSION FOR TIME SERIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
used. 1) When the observations are far enough apart in time that autocorrelation is unlikely to be
important. For example, in the stage data in this chapter, monthly averages are used. 2) When the
values are likely to be influenced mostly by non-autocorrelated factors. For data where
autocorrelation is likely to be important, other models, such as autoregressive integrated moving
average (ARIMA), could be used.
The following commands will install these packages if they are not already installed:
if(!require(mice)){install.packages("mice")}
if(!require(Kendall)){install.packages("Kendall")}
if(!require(trend)){install.packages("trend")}
The data used in this chapter is a times series of stage measurements of the tidal Cohansey River in
Greenwich, NJ. Stage is the height of the river, in this case given in feet, with an arbitrary 0 datum. The
data are from U.S. Geology Survey site 01413038, and are monthly averages.
The MannKendall and SeasonalMannKendall functions can handle missing data. However, the
sens.slope, sea.sens.slope, decompose, and stl functions, at the time of writing, cannot handle missing
data.
Because the data set has a few missing data points, we will impute those values with the mice function
in the mice package.
The function ts converts data into a time series object. The user needs to be careful using this function,
since it does not check for missing lines in the data. For example, if there is no observation for
February, the function will simply take the March the value for February and continue. However, one
could add an observation for February with a value of NA.
Greenwich = read.table("http://rcompanion.org/documents/Greenwich.csv",
header=TRUE, sep=",")
Greenwich[125:136,]
260
NONPARAMETRIC REGRESSION FOR TIME SERIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(mice)
Mousey = mice(Greenwich)
Greenwich = complete(Mousey)
Greenwich[125:136,]
TS
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2000 0.297 0.219 0.055 0.260 0.293 0.261 0.074 -0.013 -0.523
2001 -0.227 -0.465 -0.076 -0.008 0.183 0.157 0.206 0.198 0.412 0.037 -0.089 -0.263
2002 -0.513 -0.276 -0.522 -0.235 -0.120 0.172 0.135 0.253 0.362 0.448 -0.205 -0.276
2003 0.011 0.120 0.350 0.337 0.391 0.212 0.299 -0.132 -0.313 -0.534 -0.395 -0.310
2004 -0.419 -0.314 0.051 0.086 0.039 0.314 0.432 -0.099 -0.408 -0.267 -0.121 -0.140
2005 0.293 0.364 0.168 0.233 0.164 0.100 0.279 -0.375 -0.603 -0.427 -0.544 -0.532
2006 -0.122 0.164 -0.067 -0.106 0.115 0.446 0.034 0.160 -0.623 -0.692 -0.311 -0.731
2007 -0.199 -0.223 0.100 -0.063 0.113 -0.024 0.105 -0.284 -0.456 -0.591 -0.565 -0.381
2008 0.020 0.277 -0.037 -0.107 0.011 0.241 -0.038 -0.222 -0.806 -0.802 -0.654 -0.247
2009 -0.144 -0.119 0.510 0.274 0.097 0.412 0.418 0.350 -0.286 0.047 -0.323 0.393
2010 0.021 -0.049 0.141 0.035 0.318 0.263 0.023 0.107 -0.297 -0.350 0.051 0.289
2011 -0.063 0.164 0.314 0.037 0.347 0.738 0.499 0.087 -0.119 -0.355 -0.277 -0.025
2012 0.088 0.254 0.581 0.415 0.435 0.277 0.524 0.263 0.153 -0.290 -0.330 0.176
2013 0.054 0.023 0.289 0.341 0.392 0.378 0.536 -0.314 -0.265 -0.132 -0.408 -0.134
2014 0.158 0.300 0.401 0.172 0.456 0.492
plot(TS)
261
NONPARAMETRIC REGRESSION FOR TIME SERIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
It is helpful to decompose time series data into seasonal and trend components. The decompose
function in the native stats package uses “classical seasonal decomposition by moving averages”, and
the stl function in the native stats package uses “seasonal decomposition of time series by loess”.
plot(decompose(TS))
plot(stl(TS,
s.window="periodic"))
262
NONPARAMETRIC REGRESSION FOR TIME SERIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The Mann–Kendall trend test is completely nonparametric. The MannKendall function in the Kendall
package can be used with a time series object. The SeasonalMannKendall function performs the test
while taking into account the seasonality of the data.
library(Kendall)
MK = MannKendall(TS)
summary(MK)
library(Kendall)
SMK = SeasonalMannKendall(TS)
summary(SMK)
263
NONPARAMETRIC REGRESSION FOR TIME SERIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The sens.slope function in the trend package is used with a time series object. The sea.sens.slope
function performs the test while taking into account the seasonality of the data.
library(trend)
sens.slope(TS)
slope: 0.001
95 percent confidence intervall for slope
0.002 -1e-04
intercept: -0.0475
nr. of observations: 171
library(trend)
sea.sens.slope(TS)
slope: 0.0175
intercept: -1.6237
nr. of observations: 171
The pettitt.test function in the trend package identifies a point at which the values in the data change.
The results here suggest that the stage values were higher after May 2009 than before.
library(trend)
pettitt.test(TS)
sample estimates:
probable change point at tau
107
264
NONPARAMETRIC REGRESSION FOR TIME SERIES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Greenwich[107,]
265
INTRODUCTION TO PERMUTATION TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Permutation tests work by resampling the observed data many times in order to determine a p-value
for the test. Recall that the p-value is defined as the probability of getting data as extreme as the
observed data when the null hypothesis is true. If the data are shuffled many times in accordance with
the null hypothesis being true, the number of cases with data as extreme as the observed data could be
counted, and a p-value calculated.
• The general interpretation for significant results of these models isn’t that there is a
difference among medians, but that there is a significant effect of the independent variable on
the dependent variable, or that there is a significant difference among groups.
• Post-hoc tests for factors or groups can be conducted with pairwise tests of groups, or with
pairwise ordinal tests for paired data. The appropriate functions in the rcompanion package
are pairwisePermutationTest, pairwisePermutationMatrix, pairwisePermutationSymmetry, and
pairwisePermutationSymmetryMatrix.
266
INTRODUCTION TO PERMUTATION TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
• Permutation tests for data arranged in contingency tables are presented in the Association
Tests for Ordinal Tables chapter.
The following commands will install these packages if they are not already installed:
if(!require(coin)){install.packages("coin")}
The following example uses the left hand and right hand data from the Independent and Paired Values
chapter. For this example, we are interested in comparing the length of left hands and rights from 16
individuals. First we will compare the left hands to right hands as independent samples (analogous to
a Mann–Whitney test or t-test), then as paired values for each individual (analogous to a paired rank
sum test or paired t-test).
Input = ("
Individual Hand Length
A Left 17.5
B Left 18.4
C Left 16.2
D Left 14.5
E Left 13.5
F Left 18.9
G Left 19.5
H Left 21.1
I Left 17.8
J Left 16.8
K Left 18.4
L Left 17.3
M Left 18.9
N Left 16.4
O Left 17.5
P Left 15.0
A Right 17.6
B Right 18.5
C Right 15.9
D Right 14.9
E Right 13.7
F Right 18.9
G Right 19.5
H Right 21.5
I Right 18.5
J Right 17.1
K Right 18.9
L Right 17.5
M Right 19.5
267
INTRODUCTION TO PERMUTATION TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
N Right 16.5
O Right 17.4
P Right 15.6
")
Data = read.table(textConnection(Input),header=TRUE)
Data
str(Data)
summary(Data)
rm(Input)
library(FSA)
Summarize(Length ~ Hand,
data=Data,
digits=3)
Box plot
boxplot(Length ~ Hand,
data=Data,
ylab="Length, cm")
268
INTRODUCTION TO PERMUTATION TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Right = Data$Length[Data$Hand=="Right"]
plot(Left, Right,
pch = 16, # shape of points
cex = 1.0, # size of points
xlim=c(13, 22), # limits of x axis
ylim=c(13, 22), # limits of y axis
xlab="Left hand",
ylab="Right hand")
independence_test(Length ~ Hand,
data = Data)
269
ONE-WAY PERMUTATION TEST OF INDEPENDENCE FOR ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
References
For more information on permutation tests and the coin package, see:
Hothorn, T., K. Hornik, M.A. van de Wiel, and A. Zeileis. 2015. Implementing a Class of
Permutation Tests: The coin Package. cran.r-
project.org/web/packages/coin/vignettes/coin_implementation.pdf.
library(coin); help(package="coin")
A permutation test of independence can be used for one-way data with an ordinal dependent variable.
It will determine if there is a difference in the response variable among groups. There can be two or
more groups.
This test is analogous to designs used with Mann–Whitney, Kruskal–Wallis, two-sample t-test, one-
way anova, one-way anova with blocks, and ordinal regression equivalents.
The test assumes that the observations are independent. That is, it is not appropriate for paired
observations or repeated measures data. It does not make assumptions about the distribution of
values.
The test is performed with the independence_test function in the coin package.
Post-hoc testing can be conducted with pairwise permutation tests across groups.
270
ONE-WAY PERMUTATION TEST OF INDEPENDENCE FOR ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
Appropriate data
• One-way data. That is, one measurement variable in two or more groups
• Dependent variable is ordinal, and specified in R as an ordered factor variable
• Independent variable is a factor with two or more levels. That is, two or more groups
• Observations between groups are independent. That is, not paired or repeated measures
data
Hypotheses
• Null hypothesis: The values of the dependent variable among groups are equal.
• Alternative hypothesis (two-sided): The values of the dependent variable among groups are
not equal.
Interpretation
• Reporting significant results for the omnibus test as “Significant differences were found
among values for groups.” is acceptable. Alternatively, “A significant effect for Independent
Variable on Dependent Variable was found.”
• Reporting significant results for mean separation post-hoc tests as “Value of Dependent
Variable for group A was different than that for group B.” is acceptable.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(lattice)){install.packages("lattice")}
if(!require(coin)){install.packages("coin")}
if(!require(rcompanion)){install.packages("rcompanion")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(ggplot2)){install.packages("ggplot2")}
271
ONE-WAY PERMUTATION TEST OF INDEPENDENCE FOR ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
This example re-visits the Pooh, Piglet, and Tigger data from the Descriptive Statistics with the likert
Package chapter.
It answers the question, “Are the scores significantly different among the three speakers?”
Note that a new variable, Likert.f, is created for the Likert scores as an ordered factor variable. It is
necessary that the dependent variable passed to independence_test be an ordered factor variable for it
to be treated as an ordinal variable.
Input =("
Speaker Likert
Pooh 3
Pooh 5
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 5
Pooh 5
Piglet 2
Piglet 4
Piglet 2
Piglet 2
Piglet 1
Piglet 2
Piglet 3
Piglet 2
Piglet 2
Piglet 3
Tigger 4
Tigger 4
Tigger 4
Tigger 4
Tigger 5
Tigger 3
Tigger 5
Tigger 4
Tigger 4
Tigger 3
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Speaker = factor(Data$Speaker,
levels=unique(Data$Speaker))
272
ONE-WAY PERMUTATION TEST OF INDEPENDENCE FOR ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
### Create a new variable which is the likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered = TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Likert.f
Speaker 1 2 3 4 5
Pooh 0 0 1 6 3
Piglet 1 6 2 1 0
Tigger 0 0 2 6 2
prop.table(XT,
margin = 1)
Likert.f
Speaker 1 2 3 4 5
Pooh 0.0 0.0 0.1 0.6 0.3
Piglet 0.1 0.6 0.2 0.1 0.0
Tigger 0.0 0.0 0.2 0.6 0.2
273
ONE-WAY PERMUTATION TEST OF INDEPENDENCE FOR ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
Summarize(Likert ~ Speaker,
data=Data,
digits=3)
274
ONE-WAY PERMUTATION TEST OF INDEPENDENCE FOR ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
independence_test(Likert.f ~ Speaker,
data = Data)
Because the post-hoc test will produce multiple p-values, adjustments to the p-values can be made to
avoid inflating the possibility of making a type-I error. Here, the method of adjustment is indicated
with the method option. There are a variety of methods for controlling the familywise error rate or for
controlling the false discovery rate. See ?p.adjust for details on these methods.
Before conducting the pairwise tests, we will re-order the levels of the grouping variable by the
median of each group. This makes interpretation of the pairwise comparisons and compact letter
display easier.
Data$Speaker = factor(Data$Speaker,
levels=c("Pooh", "Tigger", "Piglet"))
library(rcompanion)
PT = pairwisePermutationTest(x = Data$Likert.f,
g = Data$Speaker,
method = "fdr")
PT
275
ONE-WAY PERMUTATION TEST OF INDEPENDENCE FOR ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
library(rcompanion)
cldList(comparison = PT$Comparison,
p.value = PT$p.adjust,
threshold = 0.05)
Here the fdr p-value adjustment method is used. See ?p.adjust for details on available methods.
The code creates a matrix of p-values called PM, which is then passed to the multcompLetters function
to be converted to a compact letter display.
### Order groups by median
Data$Speaker = factor(Data$Speaker,
levels=c("Pooh", "Tigger", "Piglet"))
library(rcompanion)
PM = pairwisePermutationMatrix(x = Data$Likert,
g = Data$Speaker,
exact = NULL,
method = "fdr")$Adjusted
PM
library(multcompView)
multcompLetters(PM,
276
ONE-WAY PERMUTATION TEST OF INDEPENDENCE FOR ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
compare="<",
threshold=0.05, # p-value to use as significance threshold
Letters=letters,
reversed = FALSE)
These medians are then plotted, with their confidence intervals shown as error bars. The grouping
letters from the multiple comparisons are added.
### Order groups by original order
Data$Speaker = factor(Data$Speaker,
levels=c("Pooh", "Piglet", "Tigger"))
library(rcompanion)
Sum = groupwiseMedian(data=Data,
group="Speaker",
var="Likert",
conf=0.95,
R=5000,
percentile=TRUE,
bca=FALSE,
digits=3)
Sum
X = 1:3
Y = Sum$Percentile.upper + 0.2
### Plot
library(ggplot2)
277
ONE-WAY PERMUTATION TEST OF INDEPENDENCE FOR ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
annotate("text",
x = X,
y = Y,
label = Label)
Plot of median Likert versus Speaker. Error bars indicate the 95% confidence
intervals for the median with the percentile method.
278
ONE-WAY PERMUTATION TEST OF SYMMETRY FOR PAIRED ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
A permutation test of symmetry can be used for one-way data with an ordinal dependent variable
where observations are paired within a blocking variable. It will determine if there is a difference in
the response variable among groups when controlling for the effect of the blocking variable. There can
be two or more groups.
This test is analogous to designs used with Friedman, Quade, paired t-test, repeated measures one-
way anova, and ordinal regression equivalents.
The test does not make assumptions about the distribution of values.
The test is performed with the symmetry_test function in the coin package.
Post-hoc testing can be conducted with pairwise permutation tests across groups.
Appropriate data
• One-way data plus a blocking variable. That is, one measurement variable in two or more
groups, where observations are paired within levels of a blocking variable
• Dependent variable is ordinal, and specified in R as an ordered factor variable
• Independent variable is a factor with two or more levels. That is, two or more groups. The
blocking variable is also a factor variable
• The data is arranged in a complete block design, with one or more observations per cell
Hypotheses
• Null hypothesis: The response of the dependent variable among groups are equal.
• Alternative hypothesis (two-sided): The response of the dependent variable among groups
are not equal.
Interpretation
• Reporting significant results for the omnibus test as “Significant differences were found in the
response among groups.” is acceptable. Alternatively, “A significant effect for Independent
Variable on Dependent Variable was found when controlling for the effect of Blocking
Variable.”
• Reporting significant results for mean separation post-hoc tests as “Response of Dependent
Variable for group A was different than that for group B.” is acceptable.
279
ONE-WAY PERMUTATION TEST OF SYMMETRY FOR PAIRED ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
The traditional nonparametric tests Friedman, Quade, Paired rank-sum test, or Sign test may be
alternatives depending on the design of the experiment.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(lattice)){install.packages("lattice")}
if(!require(coin)){install.packages("coin")}
if(!require(rcompanion)){install.packages("rcompanion")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(ggplot2)){install.packages("ggplot2")}
This example re-visits the Belcher data from the Friedman Test chapter. Note that each instructor is
rated by each of eight raters. Because of this, we want to stratify the responses by Rater.
It answers the question, “Are the scores significantly different among the five speakers when
controlling for the effect of the different raters?”
Note that a new variable, Likert.f, is created for the Likert scores as an ordered factor variable. It is
necessary that the dependent variable passed to symmetry_test be an ordered factor variable for it to
be treated as an ordinal variable.
Input =("
Instructor Rater Likert
'Bob Belcher' a 4
'Bob Belcher' b 5
'Bob Belcher' c 4
'Bob Belcher' d 6
'Bob Belcher' e 6
'Bob Belcher' f 6
'Bob Belcher' g 10
280
ONE-WAY PERMUTATION TEST OF SYMMETRY FOR PAIRED ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
'Bob Belcher' h 6
'Linda Belcher' a 8
'Linda Belcher' b 6
'Linda Belcher' c 8
'Linda Belcher' d 8
'Linda Belcher' e 8
'Linda Belcher' f 7
'Linda Belcher' g 10
'Linda Belcher' h 9
'Tina Belcher' a 7
'Tina Belcher' b 5
'Tina Belcher' c 7
'Tina Belcher' d 8
'Tina Belcher' e 8
'Tina Belcher' f 9
'Tina Belcher' g 10
'Tina Belcher' h 9
'Gene Belcher' a 6
'Gene Belcher' b 4
'Gene Belcher' c 5
'Gene Belcher' d 5
'Gene Belcher' e 6
'Gene Belcher' f 6
'Gene Belcher' g 5
'Gene Belcher' h 5
'Louise Belcher' a 8
'Louise Belcher' b 7
'Louise Belcher' c 8
'Louise Belcher' d 8
'Louise Belcher' e 9
'Louise Belcher' f 9
'Louise Belcher' g 8
'Louise Belcher' h 10
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Instructor = factor(Data$Instructor,
levels=unique(Data$Instructor))
### Create a new variable which is the likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered=TRUE)
library(psych)
headTail(Data)
str(Data)
281
ONE-WAY PERMUTATION TEST OF SYMMETRY FOR PAIRED ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
summary(Data)
rm(Input)
Likert.f
Instructor 4 5 6 7 8 9 10
Bob Belcher 2 1 4 0 0 0 1
Linda Belcher 0 0 1 1 4 1 1
Tina Belcher 0 1 0 2 2 2 1
Gene Belcher 1 4 3 0 0 0 0
Louise Belcher 0 0 0 1 4 2 1
prop.table(XT,
margin = 1)
Likert.f
Instructor 4 5 6 7 8 9 10
Bob Belcher 0.250 0.125 0.500 0.000 0.000 0.000 0.125
Linda Belcher 0.000 0.000 0.125 0.125 0.500 0.125 0.125
Tina Belcher 0.000 0.125 0.000 0.250 0.250 0.250 0.125
Gene Belcher 0.125 0.500 0.375 0.000 0.000 0.000 0.000
Louise Belcher 0.000 0.000 0.000 0.125 0.500 0.250 0.125
282
ONE-WAY PERMUTATION TEST OF SYMMETRY FOR PAIRED ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
Summarize(Likert ~ Instructor,
data=Data,
digits=3)
283
ONE-WAY PERMUTATION TEST OF SYMMETRY FOR PAIRED ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
library(coin)
Because the post-hoc test will produce multiple p-values, adjustments to the p-values can be made to
avoid inflating the possibility of making a type-I error. Here, the method of adjustment is indicated
with the method option. There are a variety of methods for controlling the familywise error rate or for
controlling the false discovery rate. See ?p.adjust for details on these methods.
Before conducting the pairwise tests, we will re-order the levels of the grouping variable by the
median of each group. This makes interpretation of the pairwise comparisons and compact letter
display easier.
Data$Instructor = factor(Data$Instructor,
levels = c("Linda Belcher", "Louise Belcher",
"Tina Belcher", "Bob Belcher",
"Gene Belcher"))
library(rcompanion)
PT = pairwisePermutationSymmetry(x = Data$Likert.f,
g = Data$Instructor,
b = Data$Rater,
method = "fdr")
PT
284
ONE-WAY PERMUTATION TEST OF SYMMETRY FOR PAIRED ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
library(rcompanion)
cldList(comparison = PT$Comparison,
p.value = PT$p.adjust,
threshold = 0.05)
Here the fdr p-value adjustment method is used. See ?p.adjust for details on available methods.
The code creates a matrix of p-values called PM which is then passed to the multcompLetters function
to be converted to a compact letter display.
### Order groups by median
Data$Instructor = factor(Data$Instructor,
levels = c("Linda Belcher", "Louise Belcher",
"Tina Belcher", "Bob Belcher",
"Gene Belcher"))
library(rcompanion)
PM = pairwisePermutationSymmetryMatrix(x = Data$Likert.f,
g = Data$Instructor,
285
ONE-WAY PERMUTATION TEST OF SYMMETRY FOR PAIRED ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
b = Data$Rater,
method = "fdr")
PM
library(multcompView)
multcompLetters(PM$Adjusted,
compare="<",
threshold=0.05, # p-value to use as significance threshold
Letters=letters,
reversed = FALSE)
Linda Belcher Louise Belcher Tina Belcher Bob Belcher Gene Belcher
"a" "a" "a" "b" "b"
These medians are then plotted, with their confidence intervals shown as error bars. The grouping
letters from the multiple comparisons are added.
### Order groups by original order
Data$Instructor = factor(Data$Instructor,
levels = c("Bob Belcher", "Linda Belcher", "Tina Belcher",
"Gene Belcher", "Louise Belcher"))
library(rcompanion)
Sum = groupwiseMedian(data=Data,
group="Instructor",
var="Likert",
conf=0.95,
R=5000,
percentile=TRUE,
bca=FALSE,
digits=3)
Sum
286
ONE-WAY PERMUTATION TEST OF SYMMETRY FOR PAIRED ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
X = 1:5
Y = Sum$Percentile.upper + 0.4
### Plot
library(ggplot2)
annotate("text",
x = X,
y = Y,
label = Label)
287
ONE-WAY PERMUTATION TEST OF SYMMETRY FOR PAIRED ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
Plot of median Likert versus Instructor. Error bars indicate the 95% confidence
intervals for the median with the percentile method.
288
INTRODUCTION TO LINEAR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
All linear models make some assumptions about the underlying data. For one thing, the data should
accord with the fact the model is composed of a linear combination of the effects. Non-linearity would
suggest that the model effects be modified or that a different kind of model be used. In addition, each
type of linear model usually makes some assumptions about the distribution of the data. For the
reported statistics to be valid, it is essential to understand and check these assumptions for the
specific type of model being used.
These models can be considered part of larger category of linear models called general linear model.
The dependent variable is continuous interval/ratio, and there are assumptions about the distribution
of the data. These models are usually called parametric models or tests, although technically other
types of models which assume properties about the distribution of the data are also parametric in
nature.
For example, ordinal dependent variables can be modeled with cumulative link models. Binary
(yes/no) dependent variables can be modeled with logistic regression. Dependent variables of
discrete counted quantities can be modeled with Poisson regression and similar techniques. Percent
and proportion data can be modeled with beta regression.
Fitting models
For any type of linear model, some method is used to find the value for the parameters for the model
which best fit the data. For simple models like linear regression or a simple analysis of variance, these
289
INTRODUCTION TO LINEAR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
parameters estimates could be found by hand calculations. Luckily, more complex models can be fit by
computer algorithm. General linear models are typically fit by ordinary least squares (OLS), whereas
generalized linear models are typically fit by maximum likelihood estimation (MLE). OLS is actually a
specific case of MLE that is valid only when the conditional distribution of the data are normal. These
approaches to estimation are generally reliable, but the reader should be aware that there are cases
where MLE may fail.
Most packages for specifying types of models in R use a similar grammar in the model formula.
The formula
y ~ x1 + x2 + x1:x2
Specifies y as the dependent variables, and x1, x2, and the interaction of x1 and x2 as the independent
variables.
The formula
y ~ x1 | g
y ~ x1 - 1
indicates that the intercept should not be included in the model, and
y ~ 1
indicates that only the intercept is to be included in the right side of this model.
The King article in the “References” section has some more detail on model specification syntax in R.
y ~ x
y ~ x, data = Data
290
INTRODUCTION TO LINEAR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Random effects
The syntax varies somewhat for random effects in models across packages, but
y ~ x, random = ~1|Subject
or
y ~ x +(1|Subject)
Specifies that y is the dependent variable, x is an independent variable, and Subject is an independent
variable treated as random variable, specifically with an intercept fit for each level of Subject.
More on specifying random effects is discussed in the chapter Repeated Measures ANOVA.
Each package varies on the methods used to extract information about the model, but some are
relatively common across several packages. For other types of model objects, there may be methods
to extract similar information with different functions.
Assuming that model has been defined as a model object by an appropriate function,
summary(model)
produces a summary of the model with estimates of the coefficients, and sometimes other useful
information like a p-value for the model, or an r-squared or pseudo R-squared for the model.
library(car)
Anova(model)
for some models, will produce an analysis of variance table, or an analysis of deviance table.
plot(model)
anova(model1, model2)
predict(model)
will report predicted values in the dependent variable from the model for each observation that went
in to the model.
residuals(model)
291
USING RANDOM EFFECTS IN MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
str(model)
References
As a second example, imagine we are measuring the scores of instructors multiple times, and we can
match each rater’s score at each time to the same rater’s score at the other times. So, we want to
statistically match the score of Rater A at Time 1 to the score of Rater A at Time 2, since we suppose
that someone who scores an instructor high at one time might score an instructor high at another
time. Therefore, the variable Rater will be included in the model as a random variable. Each Rater
could be thought of as a block including a measurement for each of the times.
In the previous chapter, the variable Speaker was used as a fixed effect in the model. Conceptually, the
idea is that we are interested in the effect of each of the specific levels of that variable. That is, we care
specifically about Pooh and Piglet and their scores.
292
USING RANDOM EFFECTS IN MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
In many cases of blocking variables, though, we don’t necessarily care about the values for those
specific blocks.
For example, if we conducted a study in two different schools focusing on two different curricula, we
may want to know about the effect of the curricula, but more or less chose the schools at random. We
don’t really care if Springfield Elementary had a higher score than Shelbyville Elementary. The two
schools represent any two random schools, not those two specific schools. But we definitely want to
include the School effect in the model, because we suppose that one school could do better with both
curricula than would the other school.
Another example of this is when we have instructors rated by student raters. If we are studying the
performance of the instructors, we don’t necessarily care about how Nelson or Milhouse or Ralph as
individuals rate instructors. They are representing students chosen at random. We just need to
include the effect of the variable Rater in the model to statistically account for the fact that each rater
might tend to rate all instructors lower or higher than other raters.
In these examples, School and Rater could be included in their respective models as random effects.
The examples in this book treat random variables as simple intercept-only variables, as simple blocks
that are not nested within other variables.
The following commands will install these packages if they are not already installed:
if(!require(FSA)){install.packages("FSA")}
if(!require(psych)){install.packages("psych")}
if(!require(lme4)){install.packages("lme4")}
if(!require(lmerTest)){install.packages("lmerTest")}
if(!require(nlme)){install.packages("nlme")}
if(!require(car)){install.packages("car")}
293
USING RANDOM EFFECTS IN MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For this example we will revisit the hand size example from the Independent and Paired Values
chapter. The variable hand Length will be treated as an interval/ratio variable. We will return to
using cumulative link models for Likert data in subsequent chapters.
Note that the model includes the blocking variable, Individual, and so data do not need to be in a
certain order to match the paired observations.
Also note that mixed models may make certain assumptions about the distributions of the data. For
simplicity, this example will not check if those assumption are met.
By default, an analysis of variance for a mixed model doesn’t test the significance of the random effects
in the model. However, the effect of random terms can be tested by comparing the model to a model
including only the fixed effects and excluding the random effects, or with the rand function from the
lmerTest package if the lme4 package is used to specify the model.
As a technical note, the lmerTest package has options to use Satterthwaite or Kenward–Roger degrees
of freedom, and options for type-III or type-II tests in the analysis of variance, if the lme4 package is
used to specify the model.
Input = ("
Individual Hand Length
A Left 17.5
B Left 18.4
C Left 16.2
D Left 14.5
E Left 13.5
F Left 18.9
G Left 19.5
H Left 21.1
I Left 17.8
J Left 16.8
K Left 18.4
L Left 17.3
M Left 18.9
N Left 16.4
O Left 17.5
P Left 15.0
A Right 17.6
B Right 18.5
C Right 15.9
D Right 14.9
E Right 13.7
F Right 18.9
G Right 19.5
H Right 21.5
I Right 18.5
J Right 17.1
294
USING RANDOM EFFECTS IN MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
K Right 18.9
L Right 17.5
M Right 19.5
N Right 16.5
O Right 17.4
P Right 15.6
")
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Notice the grammar in the lmer function that defines the model: the term (1|Individual) is added to the
model to indicate that Individual is the random term.
As a technical note, the 1 indicates that an intercept is to be fitted for each level of the random variable.
As another technical note, REML stands for restricted maximum likelihood. It is a method of fitting the
model, and is often considered better than fitting with a conventional ML (maximum likelihood)
method.
library(lmerTest)
anova(model)
295
USING RANDOM EFFECTS IN MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Notice the grammar in the lme function that defines the model: the option random=~1|Individual is
added to the model to indicate that Individual is the random term.
As a technical note, the 1 indicates that an intercept is to be fitted for each level of the random variable.
As another technical note, REML stands for restricted maximum likelihood. It is a method of fitting the
model, and is often considered better than fitting with a conventional ML (maximum likelihood)
method.
library(car)
Anova(model)
Chisq Df Pr(>Chisq)
Hand 11.497 1 0.0006972 ***
296
WHAT ARE LEAST SQUARE MEANS? SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
anova(model,
model.fixed)
Imagine a case where you are measuring the height of 7th-grade students in two classrooms, and want
to see if there is a difference between the two classrooms. You are also recording the sex of the
students, and at this age girls tend to be taller than boys. Say classroom A happens to have far more
girls than boys. If you were to look at the mean height in the classrooms, you might find that
classroom A had a higher mean, but this may not be an effect of the different classrooms, but because
of the difference in the counts of boys and girls in each. In this case, reporting least square means for
the classrooms may give a more representative result. Reporting least square means for studies
where there are not equal observations for each combination of treatments is sometimes
recommended. We say the design of these studies is unbalanced.
The following example details this hypothetical example. Looking at the means from the Summarize
function in FSA, we might think there is a meaningful difference between the classrooms, with a mean
height of 153.5 cm vs. 155.0 cm. But looking at the least square means (lsmeans), which are adjusted
for the difference in boys and girls in each classroom, this difference disappears. Each classroom has a
least squared mean of 153.5 cm, indicating the mean of classroom B was inflated due to the higher
proportion of girls.
Note that the following example uses a linear model with the lm function. Here, Height is being treated
as an interval/ratio variable.
This kind of analysis makes certain assumptions about the distribution of the data, but for simplicity,
this example will ignore the need to determine that the data meet these assumptions.
297
WHAT ARE LEAST SQUARE MEANS? SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The following commands will install these packages if they are not already installed:
if(!require(FSA)){install.packages("FSA")}
if(!require(psych)){install.packages("psych")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(car)){install.packages("car")}
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
298
LEAST SQUARE MEANS FOR MULTIPLE COMPARISONS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
rm(Input)
Summarize(Height ~ Classroom,
data=Data,
digits=3)
library(lsmeans)
lsmeans(model,
pairwise ~ Classroom,
adjust="tukey")
Note that an analysis of variance also would have told us that there is a difference between levels of
Sex, but not between levels of Classroom.
library(car)
Anova(model)
299
LEAST SQUARE MEANS FOR MULTIPLE COMPARISONS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Comparisons of values across groups in linear models, cumulative link models, and others can be
conducted easily with the lsmeans package. Importantly, it can make comparisons among interactions
of factors.
L.S. means stands for least square means. Least square means are means for treatment levels that are
adjusted for means of other factors in the model. For a more complete explanation, see the What are
least square means? chapter.
Note that the adjust= option should also be applied to the cld function if a compact letter display is
requested.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(ordinal)){install.packages("ordinal")}
if(!require(car)){install.packages("car")}
if(!require(RVAideMemoire)){install.packages("RVAideMemoire")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(multcompView)){install.packages("multcompView")}
300
LEAST SQUARE MEANS FOR MULTIPLE COMPARISONS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
This example uses data set and model from the One-way Ordinal ANOVA with CLM chapter.
Input =("
Speaker Likert
Pooh 3
Pooh 5
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 4
Pooh 5
Pooh 5
Piglet 2
Piglet 4
Piglet 2
Piglet 2
Piglet 1
Piglet 2
Piglet 3
Piglet 2
Piglet 2
Piglet 3
Tigger 4
Tigger 4
Tigger 4
Tigger 4
Tigger 5
Tigger 3
Tigger 5
Tigger 4
Tigger 4
Tigger 3
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Speaker = factor(Data$Speaker,
levels=unique(Data$Speaker))
### Create a new variable which is the likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered = TRUE)
library(psych)
301
LEAST SQUARE MEANS FOR MULTIPLE COMPARISONS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
headTail(Data)
str(Data)
summary(Data)
rm(Input)
library(car)
library(RVAideMemoire)
Anova(model,
type = "II")
LR Chisq Df Pr(>Chisq)
Speaker 23.395 2 8.315e-06 ***
Remember to ignore the estimate, SE, and lsmean values when using lsmeans with a clm or clmm
model object, unless specific options in lsmeans are selected.
library(lsmeans)
lsmeans(model,
pairwise ~ Speaker,
adjust="tukey") ### Tukey-adjusted comparisons
$contrasts
contrast estimate SE df z.ratio p.value
Pooh - Piglet 4.943822 1.3764706 NA 3.5916658 0.0010
Pooh - Tigger 0.633731 0.9055691 NA 0.6998152 0.7636
Piglet - Tigger -4.310091 1.3173294 NA -3.2718403 0.0031
302
FACTORIAL ANOVA: MAIN EFFECTS, INTERACTION EFFECTS, AND SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
INTERACTION PLOTS EVALUATION IN R
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Speaker,
adjust="tukey") ### Tukey-adjusted comparisons
cld(leastsquare,
alpha=0.05,
Letters=letters, ### Use lower-case letters for .group
adjust="tukey") ### Tukey-adjusted comparisons
### Remember to ignore “lsmean”, “SE”, “LCL”, and “UCL” for CLM
### unless certain options are used.
The weight gain example below show factorial data. In this example, there are three observations for
each combination of Diet and Country.
303
FACTORIAL ANOVA: MAIN EFFECTS, INTERACTION EFFECTS, AND SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
INTERACTION PLOTS EVALUATION IN R
With this kind of data, we are usually interested in testing the effect of each factor variable (main
effects) and then the effect of their combination (interaction effect).
For two-way data, an interaction plot shows the mean or median value for the response variable for
each combination of the independent variables. This type of plot, especially if it includes error bars to
indicate the variable of data within each group, gives us some understanding of the effect of the main
factors and their interaction.
When main effects or interaction effects are statistically significant, post-hoc testing can be conducted
to determine which groups differ significantly from other groups. With a factorial experiment, there
are a few guidelines for determining when to do post-hoc testing. The following guidelines are
presented for two-way data for simplicity.
• When neither the main effects nor the interaction effect is statistically significant, no post-hoc
mean-separation testing should be conducted.
• When one or more of the main effects are statistically significant and the interaction effect is
not, post-hoc mean-separation testing should be conducted on significant main effects only.
(This is shown in the first weight gain example below)
• When the interaction effect is statistically significant, post-hoc mean-separation testing should
be conducted on the interaction effect only. This is the case even when the main effects are
also statistically significant.
(This is shown in the second weight gain example below)
The final guideline above is not always followed. There are times when people will present the mean-
separation tests for significant main effects even when the interaction effect is significant. In general,
though, if there is a significant interaction, the mean-separation tests for interaction will better explain
the results of the analysis, and the mean-separation tests for the main effects will be of less interest.
The following commands will install these packages if they are not already installed:
if(!require(car)){install.packages("car")}
if(!require(psych)){install.packages("psych")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(lsmeans)){install.packages("lsmeans")}
304
FACTORIAL ANOVA: MAIN EFFECTS, INTERACTION EFFECTS, AND SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
INTERACTION PLOTS EVALUATION IN R
if(!require(FSA)){install.packages("FSA")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(phia)){install.packages("phia")}
The model will be fit with the lm function, whose syntax is similar to that of the clm function.
This kind of analysis makes certain assumptions about the distribution of the data, but for simplicity,
this example will ignore the need to determine that the data meet these assumptions.
Input =("
Diet Country Weight_change
A USA 0.120
A USA 0.125
A USA 0.112
A UK 0.052
A UK 0.055
A UK 0.044
B USA 0.096
B USA 0.100
B USA 0.089
B UK 0.025
B UK 0.029
B UK 0.019
C USA 0.149
C USA 0.150
C USA 0.142
C UK 0.077
C UK 0.080
C UK 0.066
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Country = factor(Data$Country,
levels=unique(Data$Country))
library(psych)
headTail(Data)
305
FACTORIAL ANOVA: MAIN EFFECTS, INTERACTION EFFECTS, AND SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
INTERACTION PLOTS EVALUATION IN R
str(Data)
summary(Data)
rm(Input)
This style of interaction plot does not show the variability of each group mean, so it is difficult to use
this style of plot to determine if there are significant differences among groups.
The plot shows that mean weight gain for each diet was lower for the UK compared with USA. And
that this difference was relatively constant for each diet, as is evidenced by the lines on the plot being
parallel. This suggests that there is no large or significant interaction effect. That is, the difference
among diets is consistent across countries. And vice-versa, the difference in countries is consistent
across diets.
A couple of other styles of interaction plot are shown at the end of this chapter.
interaction.plot(x.factor = Data$Country,
trace.factor = Data$Diet,
response = Data$Weight_change,
fun = mean,
type="b",
col=c("black","red","green"), ### Colors for levels of trace var.
pch=c(19, 17, 15), ### Symbols for levels of trace var.
fixed=TRUE, ### Order by factor order in data
leg.bty = "o")
306
FACTORIAL ANOVA: MAIN EFFECTS, INTERACTION EFFECTS, AND SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
INTERACTION PLOTS EVALUATION IN R
The ANOVA table indicates that the main effects are significant, but that the interaction effect is not.
library(car)
Anova(model,
type = "II")
For this, we will use the lsmeans package. The linear model under consideration is called model,
created the lm function above. The formula in the lsmeans function indicates that pairwise
307
FACTORIAL ANOVA: MAIN EFFECTS, INTERACTION EFFECTS, AND SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
INTERACTION PLOTS EVALUATION IN R
comparisons should be conducted for the variable Country in the first call, and for the variable Diet in
the second call.
library(lsmeans)
lsmeans(model,
pairwise ~ Country,
adjust="tukey") ### Tukey-adjusted comparisons
$contrasts
contrast estimate SE df t.ratio p.value
USA - UK 0.07066667 0.002755466 12 25.646 <.0001
library(lsmeans)
lsmeans(model,
pairwise ~ Diet,
adjust="tukey") ### Tukey-adjusted comparisons
$contrasts
contrast estimate SE df t.ratio p.value
A - B 0.025 0.003374743 12 7.408 <.0001
A - C -0.026 0.003374743 12 -7.704 <.0001
B - C -0.051 0.003374743 12 -15.112 <.0001
308
FACTORIAL ANOVA: MAIN EFFECTS, INTERACTION EFFECTS, AND SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
INTERACTION PLOTS EVALUATION IN R
C UK 0.077
C UK 0.080
C UK 0.066
C NZ 0.055
C NZ 0.065
C NZ 0.050
C NZ 0.054
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Country = factor(Data$Country,
levels=unique(Data$Country))
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
309
FACTORIAL ANOVA: MAIN EFFECTS, INTERACTION EFFECTS, AND SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
INTERACTION PLOTS EVALUATION IN R
library(car)
Anova(model,
type = "II")
We will use the lsmeans package, but ask for a compact letter display with the cld function. First we
create an object, named leastsquare, with the results of the call to lsmeans. Notice here that the
formula indicates that pairwise comparisons should be conducted for the interaction of Country and
Diet, indicated with Country:Diet.
310
FACTORIAL ANOVA: MAIN EFFECTS, INTERACTION EFFECTS, AND SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
INTERACTION PLOTS EVALUATION IN R
Be sure to read the Least Square Means for Multiple Comparisons chapter for correct interpretation of
least square means. For lm model objects, the values for lsmean, SE, LCL, and UCL values are
meaningful.
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Country:Diet,
adjust="tukey") ### Tukey-adjusted comparisons
leastsquare
cld(leastsquare,
alpha=0.05,
Letters=letters, ### Use lower-case letters for .group
adjust="tukey") ### Tukey-adjusted comparisons
The package ggplot2 can be used to create attractive interaction plots with error bars. Here, we will
use standard error of each mean for the error bars.
### Create a data frame called Sum with means and standard deviations
library(FSA)
311
FACTORIAL ANOVA: MAIN EFFECTS, INTERACTION EFFECTS, AND SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
INTERACTION PLOTS EVALUATION IN R
### Add standard error of the mean to the Sum data frame
Sum
Sum$Country = factor(Sum$Country,
levels=unique(Sum$Country))
library(ggplot2)
pd = position_dodge(.2)
312
FACTORIAL ANOVA: MAIN EFFECTS, INTERACTION EFFECTS, AND SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
INTERACTION PLOTS EVALUATION IN R
It is probably more common for means to be lettered so that the greatest mean is indicated with a.
However, lsmeans by default labels the least mean with a. The order of letters can be reversed
manually.
313
FACTORIAL ANOVA: MAIN EFFECTS, INTERACTION EFFECTS, AND SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
INTERACTION PLOTS EVALUATION IN R
Plot of mean weight change for three diets in three countries. Means sharing a
letter are not significantly different according to pairwise comparisons of least
square means with Tukey adjustment for multiple comparisons.
IM = interactionMeans(model)
IM
314
P-VALUES AND R-SQUARE VALUES FOR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
plot(IM)
par(mfrow=c(1,1))
315
P-VALUES AND R-SQUARE VALUES FOR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Pseudo R-squared
For many types of models, R-squared is not defined. These include relatively common models like
logistic regression and the cumulative link models used in this book. For these models, pseudo R-
squared measures can be calculated. A pseudo R-squared is not directly comparable to the R-squared
for OLS models. Nor can it can be interpreted as the proportion of the variability in the dependent
variable that is explained by model. Instead pseudo R-squared measures are relative measures among
similar models indicating how well the model explains the data.
In general I favor the Nagelkerke pseudo R-squared, but there is no agreement as to which pseudo R-
squared measurement should be used.
This book uses three pseudo R-squared measures: McFadden, Cox and Snell (also referred to as ML),
Nagelkerke (also referred to as Cragg and Uhler).
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(lmtest)){install.packages("lmtest")}
if(!require(boot)){install.packages("boot")}
if(!require(rcompanion)){install.packages("rcompanion")}
316
P-VALUES AND R-SQUARE VALUES FOR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The following example uses some hypothetical data of a sample of people for which typing speed
(Words.per.minute) and age were measured. After plotting the data, we decide to construct a
polynomial model with Words.per.minute as the dependent variable and Age and Age2 as the
independent variables. Notice in this example that all variables are treated as interval/ratio variables,
and that the independent variables are not factor variables, but are continuous variables.
The data will first be fit with a linear model with the lm function. Passing this model to the summary
function will display the p-value for the model and the R-squared value for the model.
The same data will then be fit with a generalized linear model with the glm function. This type of
model allows more latitude in the types of data that can be fit, but in this example, we’ll use the
family=gaussian option, which will mimic the model fit with the lm function, though the underlying
math is different.
Importantly, the summary of the glm function does not produce a p-value for the model nor an R-
squared for the model.
For the model fit with glm, the p-value can be determined with the anova function comparing the fitted
model to a null model. The null model is fit with only an intercept term on the right side of the model.
As an alternative, the nagelkerke function described below also reports a p-value for the model, using
the likelihood ratio test.
There is no R-squared defined for a glm model. Instead a pseudo R-squared can be calculated. The
function nagelkerke produces pseudo R-squared values for a variety of models. It reports three types:
McFadden, Cox and Snell, and Nagelkerke. In general I recommend using the Nagelkerke measure,
though there is no agreement on which pseudo R-squared measurement to use, if any at all.
Note that the Nagelkerke is the same as the Cox and Snell, except that the value is adjusted upward so
that the Nagelkerke has a maximum value of 1. It has been suggested that a McFadden value of 0.2–0.4
indicates a good fit.
Note that these models makes certain assumptions about the distribution of the data, but for
simplicity, this example will ignore the need to determine if the data met these assumptions.
Input =("
Age Words.per.minute
12 23
12 32
12 25
13 27
13 30
15 29
15 33
16 37
18 29
22 33
317
P-VALUES AND R-SQUARE VALUES FOR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
23 37
24 33
25 43
27 35
33 30
42 25
53 22
")
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
318
P-VALUES AND R-SQUARE VALUES FOR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Prepare data
### Create new variable for the square of Age
Data$Age2 = Data$Age ^ 2
library(psych)
headTail(Data)
Linear model
model = lm (Words.per.minute ~ Age + Age2,
data=Data)
plotPredy(data = Data,
y = Words.per.minute,
x = Age,
x2 = Age2,
model = model,
order = 2,
xlab = "Words per minute",
ylab = "Age")
319
P-VALUES AND R-SQUARE VALUES FOR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
anova (model,
null,
test="Chisq") ### Tests options "Rao", "LRT",
### "Chisq", "F", "Cp"
### But some work with only some model types
320
P-VALUES AND R-SQUARE VALUES FOR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The nagelkerke function also reports the McFadden, Cox and Snell, and Nagelkerke pseudo R-squared
values for the model.
library(rcompanion)
nagelkerke(model)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.112227
Cox and Snell (ML) 0.500939
Nagelkerke (Cragg and Uhler) 0.501964
$Likelihood.ratio.test
lrtest(model)
plotPredy(data = Data,
y = Words.per.minute,
x = Age,
x2 = Age2,
model = model,
order = 2,
xlab = "Words per minute",
ylab = "Age",
col = "red") ### line color
321
P-VALUES AND R-SQUARE VALUES FOR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
It is relatively easy to produce confidence intervals for p-values, R-squared values, or other parameters
from model fitting, such as coefficients for regression terms. This can be accomplished with
bootstrapping. Here the boot.ci function from the boot package is used.
Function can contain any function of interest, as long as it includes an input vector or data frame (input
in this case) and an indexing variable (index in this case). Stat is set to produce the actual statistic of
interest on which to perform the bootstrap (r.squared from the summary of the lm in this case).
The code Function(Data, 1:17) is there simply to test Function on the data frame Data. In this case, it
will produce the output of Function for the first n rows of Data. Since n is defined as the length of the
first column in Data, this should return the value for Stat for the whole data frame, if Function is set up
correctly.
Input =("
Age Words.per.minute
12 23
12 32
12 25
13 27
13 30
15 29
15 33
16 37
18 29
22 33
23 37
24 33
25 43
27 35
322
P-VALUES AND R-SQUARE VALUES FOR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
33 30
42 25
53 22
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Age2 = Data$Age ^ 2
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Linear model
R-squared value
library(boot)
n = length(Data[,1])
Function(Data, 1:n)
[1] 0.5009385
Boot = boot(Data,
Function,
R=5000)
323
P-VALUES AND R-SQUARE VALUES FOR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
mean(Boot$t[,1])
[1] 0.5754582
boot.ci(Boot,
conf = 0.95,
type = "perc")
Intervals :
Level Percentile
95% ( 0.3796, 0.7802 )
Calculations and Intervals on Original Scale
Boot
hist(Boot$t[,1],
col = "darkgray")
p-value
Unfortunately the p-value for the model is not stored neatly in the lm model object, so it has to be
calculated in the Stat definition.
library(boot)
n = length(Data[,1])
Function(Data, 1:n)
324
P-VALUES AND R-SQUARE VALUES FOR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
value
0.007710425
Boot = boot(Data,
Function,
R=5000)
mean(Boot$t[,1])
[1] 0.00672866
boot.ci(Boot,
conf = 0.95,
type = "perc")
Intervals :
Level Percentile
95% ( 0.0000, 0.0368 )
Calculations and Intervals on Original Scale
Boot
hist(Boot$t[,1],
col = "darkgray")
Pseudo.R.squared.for.model.vs.null.
The third element of this matrix is the value for the Nagelkerke pseudo R-squared. So,
nagelkerke(Result, Null)[[2]][3]
325
P-VALUES AND R-SQUARE VALUES FOR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(boot)
library(rcompanion)
n = length(Data[,1])
Function(Data, 1:n)
[1] 0.501964
Boot = boot(Data,
Function,
R=1000)
mean(Boot$t[,1])
[1] 0.5803598
boot.ci(Boot,
conf = 0.95,
type = "perc")
Intervals :
Level Percentile
95% ( 0.3836, 0.7860 )
Calculations and Intervals on Original Scale
326
P-VALUES AND R-SQUARE VALUES FOR MODELS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Boot
hist(Boot$t[,1],
col = "darkgray")
p-value
library(boot)
library(rcompanion)
n = length(Data[,1])
Function(Data, 1:n)
[1] 0.0027184
Boot = boot(Data,
Function,
R=1000)
mean(Boot$t[,1])
[1] 0.002723259
boot.ci(Boot,
conf = 0.95,
type = "perc")
327
INTRODUCTION TO CUMULATIVE LINK MODELS (CLM) FOR ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
Intervals :
Level Percentile
95% ( 0.0000, 0.0173 )
Calculations and Intervals on Original Scale
Boot
hist(Boot$t[,1],
col = "darkgray")
References
Cumulative link models are a different approach to analyzing ordinal data. Models can be chosen to
handle simple or more complex data and situations.
This approach is very powerful and flexible, and might be considered the best approach for data with
ordinal dependent variables in many cases.
However, two disadvantages to using these models is that 1) your audience may not familiar with
them, and 2) their results can be somewhat tricky to interpret or explain.
These models are also called ordinal regression models, or proportional odds models.
328
INTRODUCTION TO CUMULATIVE LINK MODELS (CLM) FOR ORDINAL SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
DATA EVALUATION IN R
• The general interpretation for significant results of these models is that there is a significant
effect of the independent variable on the dependent variable, or that there is a significant
difference among groups.
• Post-hoc tests for factors or groups can be conducted with the lsmeans package. An optional
approach for post-hoc tests is to use pairwise ordinal tests of groups, or with pairwise ordinal
tests for paired data. These can be conducted with the functions pairwiseOrdinalTest and
pairwiseOrdinalPairedTest from the rcompanion package.
• The threshold = “equidistant” and threshold = “symmetric” options can be used to indicate to
the software that levels of the response variable are equally spaced or symmetrically spaced,
respectively. This is useful to indicate when these conditions are assumed to be true, but are
also useful to try if the model procedure produces errors. Likert items using symmetrical
language in the range of responses could be considered symmetric. Likert items with several
numbered options with anchor terms only at the ends of scale might be considered
equidistant.
Analysis of deviance
The significance of the effects of independent variables will be tested with an analysis of deviance
(ANODE) approach. This is analogous to the analysis of variance (ANOVA) used in linear models.
For CLM, the assumption of concern is called the proportional odds assumption. An explanation of this
assumption can be found in the Wikipedia or IDRE articles cited below.
The ordinal package can test for the proportional odds assumption with the nominal_test and
scale_test functions (Christensen 2015b). If any independent variable fails these tests (that is, a
significant p-value is returned), that variable can be handled differently in the model using the nominal
and scale options in the clm function.
References
For more information on these models and the ordinal package, see:
329
TWO-SAMPLE ORDINAL TEST WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
• Christensen, H.R.B. 2015a. Analysis of ordinal data with cumulative link models—estimation
with the R-package ordinal. cran.r-
project.org/web/packages/ordinal/vignettes/clm_intro.pdf.
• library(ordinal); help(package="ordinal")
• Wikipedia. 2015. “The model and the proportional odds assumption” in Ordered logit.
en.wikipedia.org/wiki/Ordered_logit#The_model_and_the_proportional_odds_assumption.
Interpretation
A significant result can be interpreted as, “There was a significant difference between groups.”
Or, “There was a significant effect of Independent Variable.”
The following commands will install these packages if they are not already installed:
330
TWO-SAMPLE ORDINAL TEST WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(lattice)){install.packages("lattice")}
if(!require(ordinal)){install.packages("ordinal")}
if(!require(car)){install.packages("car")}
if(!require(RVAideMemoire)){install.packages("RVAideMemoire")}
Data = read.table(textConnection(Input),header=TRUE)
### Create a new variable which is the Likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered = TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
331
TWO-SAMPLE ORDINAL TEST WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
rm(Input)
Likert.f
Speaker 1 2 3 4 5
Piglet 1 6 2 1 0
Pooh 0 0 1 6 3
prop.table(XT,
margin = 1)
Likert.f
Speaker 1 2 3 4 5
Piglet 0.1 0.6 0.2 0.1 0.0
Pooh 0.0 0.0 0.1 0.6 0.3
332
TWO-SAMPLE ORDINAL TEST WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Summarize(Likert ~ Speaker,
data=Data,
digits=3)
The p-value for the effect of Speaker is determined using the Anova function in the packages
RVAideMemoire and car.
Define model
library(ordinal)
Analysis of deviance
333
TWO-SAMPLE PAIRED ORDINAL TEST WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(car)
library(RVAideMemoire)
Anova(model,
type = "II")
LR Chisq Df Pr(>Chisq)
Speaker 17.505 1 2.866e-05 ***
scale_test(model)
Note that the clmm function is used here instead of the clm function. The clmm function specifies a
mixed effects model. This type of model is appropriate for paired and repeated measures analyses.
Appropriate data
• Two-sample data. That is, one-way data with two groups only
• Dependent variable is ordered factor
334
TWO-SAMPLE PAIRED ORDINAL TEST WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
• Independent variable is a factor with two levels. That is, two groups
• Observations between groups are paired
Interpretation
A significant result can be interpreted as, “There was a significant difference between groups.”
Or, “There was a significant effect of Independent Variable.”
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(ordinal)){install.packages("ordinal")}
if(!require(car)){install.packages("car")}
if(!require(RVAideMemoire)){install.packages("RVAideMemoire")}
Data = read.table(textConnection(Input),header=TRUE)
335
TWO-SAMPLE PAIRED ORDINAL TEST WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
### Create a new variable which is the Likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered = TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Note that the points in the plot are jittered slightly so that points that would fall directly on top of one
another can be seen.
First, two new variables, Time.1 and Time.2, are created by extracting the values of Likert for
observations with the Time variable equal to 1 or 2, respectively.
Note that for this plot, the data must be ordered so that the first observation where Time = 1 is paired
to the first observation where Time = 2, and so on.
Time.1 = Data$Likert[Data$Time==1]
Time.2 = Data$Likert[Data$Time==2]
plot(Time.1, jitter(Time.2), # jitter offsets points so you can see them all
pch = 16, # shape of points
cex = 1.0, # size of points
xlim=c(1, 5.5), # limits of x axis
ylim=c(1, 5.5), # limits of y axis
xlab="Time 1",
ylab="Time 2"
)
abline(0,1, col="blue", lwd=2) # line with intercept of 0 and slope of 1
336
TWO-SAMPLE PAIRED ORDINAL TEST WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
barplot(Difference,
col="dark gray",
xlab="Observation",
ylab="Difference (Time 2 – Time 1)")
337
TWO-SAMPLE PAIRED ORDINAL TEST WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
It is identified as a random variable in the mixed effects model. The data= option indicates the data
frame that contains the variables. For the meaning of other options, see ?clmm.
The p-value for the effect of Time is determined using the Anova function in the packages
RVAideMemoire and car.
Define model
library(ordinal)
Analysis of deviance
library(car)
library(RVAideMemoire)
Anova(model,
type = "II")
LR Chisq Df Pr(>Chisq)
Time 9.2033 1 0.002416 **
First, model.fixed is defined, and then the two models are passed to the anova function.
model.fixed = clm(Likert.f ~ Time,
data = Data)
anova(model,
null = model.fixed)
338
ONE-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
data = Data)
nominal_test(model.clm)
scale_test(model.clm)
Warning messages:
1: (-2) Model failed to converge: degenerate Hessian with 1 negative eigenvalues
In addition: maximum number of consecutive Newton modifications reached
2: (-1) Model failed to converge with max|grad| = 6.85558e-06 (tol = 1e-06)
In addition: maximum number of consecutive Newton modifications reached
Interpretation
A significant result can be interpreted as, “There was a significant difference among groups.”
Or, “There was a significant effect of Independent Variable.”
A significant post-hoc analysis indicates, “There was a significant difference between Group A
and Group B”, and so on.
339
ONE-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(lattice)){install.packages("lattice")}
if(!require(ordinal)){install.packages("ordinal")}
if(!require(car)){install.packages("car")}
if(!require(RVAideMemoire)){install.packages("RVAideMemoire")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(rcompanion)){install.packages("rcompanion")}
340
ONE-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Tigger 4
Tigger 4
Tigger 4
Tigger 5
Tigger 3
Tigger 5
Tigger 4
Tigger 4
Tigger 3
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Speaker = factor(Data$Speaker,
levels=unique(Data$Speaker))
### Create a new variable which is the likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered = TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Likert.f
Speaker 1 2 3 4 5
Pooh 0 0 1 6 3
Piglet 1 6 2 1 0
Tigger 0 0 2 6 2
341
ONE-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
prop.table(XT,
margin = 1)
Likert.f
Speaker 1 2 3 4 5
Pooh 0.0 0.0 0.1 0.6 0.3
Piglet 0.1 0.6 0.2 0.1 0.0
Tigger 0.0 0.0 0.2 0.6 0.2
Summarize(Likert ~ Speaker,
data=Data,
digits=3)
342
ONE-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The p-value for the effect of Speaker is determined using the Anova function in the packages
RVAideMemoire and car.
Define model
library(ordinal)
Analysis of deviance
library(car)
library(RVAideMemoire)
Anova(model,
type = "II")
LR Chisq Df Pr(>Chisq)
Speaker 23.395 2 8.315e-06 ***
Here we’ll create an object of the lsmeans output called leastsquare. Then we’ll pass this object to the
cld function to create a compact letter display.
Be sure to read the Least Square Means for Multiple Comparisons chapter for correct interpretation of
least square means. For clm model objects, the lsmean, SE, LCL, and UCL values should be ignored,
unless specific options in lsmeans are selected.
343
ONE-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Speaker,
adjust="tukey") ### Tukey-adjusted comparisons
leastsquare
cld(leastsquare,
alpha=0.05,
Letters=letters, ### Use lower-case letters for .group
adjust="tukey") ### Tukey-adjusted comparisons
### Remember to ignore “lsmean”, “SE”, “LCL”, and “UCL” for CLM
### No p-value
scale_test(model)
344
ONE-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Optional analyses
Table format
Post-hoc testing can be conducted with the pairwiseOrdinalTest function in the rcompanion package,
which produces a table of p-values comparing each pair of groups, or with pairwiseOrdinalMatrix
function, which produces a matrix of p-values. This can then be converted into a compact letter
display.
To prevent the inflation of type I error rates, adjustments to the p-values can be made using the
p.adjust.method option. See ?p.adjust for details on available p-value adjustment methods.
### Order groups by median
Data$Speaker = factor(Data$Speaker,
levels=c("Pooh", "Tigger", "Piglet"))
library(rcompanion)
PT = pairwiseOrdinalTest(x = Data$Likert.f,
g = Data$Speaker,
method = "fdr")
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
PT
library(rcompanion)
cldList(comparison = PT$Comparison,
p.value = PT$p.adjust,
threshold = 0.05)
345
ONE-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data$Speaker = factor(Data$Speaker,
levels=c("Pooh", "Tigger", "Piglet"))
library(rcompanion)
PM = pairwiseOrdinalMatrix(x = Data$Likert.f,
g = Data$Speaker,
method = "fdr")
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
PM
$Adjusted
Pooh Tigger Piglet
Pooh 1.000e+00 0.4763000 8.598e-05
Tigger 4.763e-01 1.0000000 1.422e-04
Piglet 8.598e-05 0.0001422 1.000e+00
library(multcompView)
multcompLetters(PM$Adjusted,
compare="<",
threshold=0.05, # p-value to use as significance threshold
Letters=letters,
reversed = FALSE)
346
ONE-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Interpretation
A significant result can be interpreted as, “There was a significant difference among groups.”
Or, “There was a significant effect of Independent Variable.”
A significant post-hoc analysis indicates, “There was a significant difference between Group A
and Group B”, and so on.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(lattice)){install.packages("lattice")}
if(!require(ordinal)){install.packages("ordinal")}
if(!require(car)){install.packages("car")}
if(!require(RVAideMemoire)){install.packages("RVAideMemoire")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(rcompanion)){install.packages("rcompanion")}
In this example, we want to determine if there is a difference in scores among five instructors from the
Belcher family. Because we know which rater gave each score, we will use the Rater as a blocking
variable, with the suspicion that one rater might rate consistently higher than another one. If we
hadn’t recorded this information about raters, we could use a one-way ordinal anova.
Input =("
Instructor Rater Likert
'Bob Belcher' a 4
'Bob Belcher' b 5
'Bob Belcher' c 4
347
ONE-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
'Bob Belcher' d 6
'Bob Belcher' e 6
'Bob Belcher' f 6
'Bob Belcher' g 10
'Bob Belcher' h 6
'Linda Belcher' a 8
'Linda Belcher' b 6
'Linda Belcher' c 8
'Linda Belcher' d 8
'Linda Belcher' e 8
'Linda Belcher' f 7
'Linda Belcher' g 10
'Linda Belcher' h 9
'Tina Belcher' a 7
'Tina Belcher' b 5
'Tina Belcher' c 7
'Tina Belcher' d 8
'Tina Belcher' e 8
'Tina Belcher' f 9
'Tina Belcher' g 10
'Tina Belcher' h 9
'Gene Belcher' a 6
'Gene Belcher' b 4
'Gene Belcher' c 5
'Gene Belcher' d 5
'Gene Belcher' e 6
'Gene Belcher' f 6
'Gene Belcher' g 5
'Gene Belcher' h 5
'Louise Belcher' a 8
'Louise Belcher' b 7
'Louise Belcher' c 8
'Louise Belcher' d 8
'Louise Belcher' e 9
'Louise Belcher' f 9
'Louise Belcher' g 8
'Louise Belcher' h 10
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Instructor = factor(Data$Instructor,
levels=unique(Data$Instructor))
### Create a new variable which is the likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered=TRUE)
348
ONE-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Likert.f
Instructor 4 5 6 7 8 9 10
Bob Belcher 2 1 4 0 0 0 1
Linda Belcher 0 0 1 1 4 1 1
Tina Belcher 0 1 0 2 2 2 1
Gene Belcher 1 4 3 0 0 0 0
Louise Belcher 0 0 0 1 4 2 1
prop.table(XT,
margin = 1)
Likert.f
Instructor 4 5 6 7 8 9 10
Bob Belcher 0.250 0.125 0.500 0.000 0.000 0.000 0.125
Linda Belcher 0.000 0.000 0.125 0.125 0.500 0.125 0.125
Tina Belcher 0.000 0.125 0.000 0.250 0.250 0.250 0.125
Gene Belcher 0.125 0.500 0.375 0.000 0.000 0.000 0.000
Louise Belcher 0.000 0.000 0.000 0.125 0.500 0.250 0.125
349
ONE-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Summarize(Likert ~ Instructor,
data=Data,
digits=3)
350
ONE-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The p-value for the effect of Instructor is determined using the Anova function in the packages
RVAideMemoire and car.
library(car)
library(RVAideMemoire)
Anova(model,
type = "II")
LR Chisq Df Pr(>Chisq)
Instructor 38.51 4 8.794e-08 ***
First, model.fixed is defined, and then the two models are passed to the anova function.
Be sure that the threshold option in model.fixed is the same as in the original model.
model.fixed = clm(Likert.f ~ Instructor,
data = Data,
threshold = "equidistant")
anova(model,
null = model.fixed)
351
ONE-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
nominal_test(model.clm)
scale_test(model.clm)
Warning messages:
1: (-2) Model failed to converge: degenerate Hessian with 1 negative eigenvalues
In addition: maximum number of consecutive Newton modifications reached
### This test failed. It would be possible to adjust the fitting parameters,
### and try to get the model to converge.
Be sure to read the Least Square Means for Multiple Comparisons chapter for correct interpretation of
least square means. For clmm model objects, the lsmean, SE, LCL, and UCL values should be ignored,
unless specific options in lsmeans are selected.
Here we’ll create an object of the lsmeans output called leastsquare. Then we’ll pass this object to the
cld function to create a compact letter display.
The ordinal model under consideration is called model, created the clmm function above. The formula
in the lsmeans function indicates that pairwise comparisons should be conducted for the variable
Instructor.
library(multcompView)
library(lsmeans)
352
ONE-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
leastsquare = lsmeans(model,
pairwise ~ Instructor,
adjust="tukey") ### Tukey-adjusted comparisons
leastsquare
cld(leastsquare,
alpha=0.05,
Letters=letters, ### Use lower-case letters for .group
adjust="tukey") ### Tukey-adjusted comparisons
### Remember to ignore “lsmean”, “SE”, “LCL”, and “UCL” for CLMM
Optional analyses
Post-hoc test: pairwise paired ordinal tests for multiple comparisons
The post-hoc analysis can be accomplished with the pairwiseOrdinalPairedTest or
pairwiseOrdinalPairedMatrix functions. These functions extract the p-values from paired cumulative
link models for each pair of treatments.
Usually you would want to first order your treatment variable (Speaker) by median or other location
statistic for the letters in the compact letter display to be in their proper order.
Note that for this function, the data must be ordered by the blocking variable so that the first
observation where Instructor = Bob is paired to the first observation where Instructor = Linda, and so
on.
Here the p-values for the pairwise comparisons are adjusted with the fdr method. See ?p.adjust for
options and details on p-value adjustment methods.
Here the threshold = "equidistant" option is used in order to avoid errors. This option does not need to
be used routinely.
### Order groups by median
353
ONE-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data$Instructor = factor(Data$Instructor,
levels = c("Linda Belcher", "Louise Belcher",
"Tina Belcher", "Bob Belcher",
"Gene Belcher"))
library(rcompanion)
PT = pairwiseOrdinalPairedTest(x = Data$Likert.f,
g = Data$Instructor,
b = Data$Rater,
threshold = "equidistant",
method="fdr")
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
PT
library(rcompanion)
cldList(comparison = PT$Comparison,
p.value = PT$p.adjust,
threshold = 0.05)
Data$Instructor = factor(Data$Instructor,
354
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(rcompanion)
PM = pairwiseOrdinalPairedMatrix(x = Data$Likert.f,
g = Data$Instructor,
b = Data$Rater,
threshold="equidistant",
method="fdr")
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
PM
$Adjusted
Linda Belcher Louise Belcher Tina Belcher Bob Belcher Gene Belcher
Linda Belcher 1.000e+00 4.456e-01 0.5593000 0.003850 7.425e-05
Louise Belcher 4.456e-01 1.000e+00 0.3353000 0.002142 1.247e-06
Tina Belcher 5.593e-01 3.353e-01 1.0000000 0.002142 4.277e-04
Bob Belcher 3.850e-03 2.142e-03 0.0021420 1.000000 5.593e-01
Gene Belcher 7.425e-05 1.247e-06 0.0004277 0.559300 1.000e+00
library(multcompView)
multcompLetters(PM$Adjusted,
compare="<",
threshold=0.05, # p-value to use as significance threshold
Letters=letters,
reversed = FALSE)
Linda Belcher Louise Belcher Tina Belcher Bob Belcher Gene Belcher
"a" "a" "a" "b" "b"
The example here looks at ratings for three instructors across four different questions. The analysis
will attempt to answer the questions: a) Is there a significant difference in scores for different
355
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Main effects and interaction effects are explained further in the Factorial ANOVA: Main Effects,
Interaction Effects, and Interaction Plots chapter.
The clm function can specify more complex models with multiple independent variables of different
types, but this book will not explore more complex examples.
The p-values for the main and interaction effects can be determined with the Anova function from
RVAideMemoire, which produces an analysis of deviance table for these effects. In addition, a p-value
for the model as a whole will be determined, along with a pseudo R-squared for the model as a whole.
Post-hoc analysis to determine which groups are different can be conducted on each significant main
effect and on the interaction effect if it is significant.
Appropriate data
• Two-way data
• Dependent variable is ordered factor
• Independent variables are factors with at least two levels or groups each
• Observations between groups are not paired or repeated measures
Interpretation
A significant main effect can be interpreted as, “There was a significant difference among
groups.” Or, “There was a significant effect of Independent Variable.”
A significant interaction effect can be interpreted as, “There was a significant interaction effect
between Independent Variable A and Independent Variable B.”
A significant post-hoc analysis indicates, “There was a significant difference between Group A
and Group B”, and so on.
356
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(lattice)){install.packages("lattice")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(ordinal)){install.packages("ordinal")}
if(!require(car)){install.packages("car")}
if(!require(RVAideMemoire)){install.packages("RVAideMemoire")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(rcompanion)){install.packages("rcompanion")}
357
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Mugen Delivery 8
Mugen Delivery 9
Mugen Delivery 8
Mugen Delivery 7
Mugen VisualAides 5
Mugen VisualAides 4
Mugen VisualAides 4
Mugen VisualAides 5
Mugen AnswerQuest 6
Mugen AnswerQuest 7
Mugen AnswerQuest 6
Mugen AnswerQuest 7
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Instructor = factor(Data$Instructor,
levels=unique(Data$Instructor))
### Create a new variable which is the likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered = TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Likert.f
Instructor 3 4 5 6 7 8 9 10
Fuu 0 0 0 1 3 6 5 1
Jin 0 0 2 5 4 4 1 0
358
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Mugen 1 4 3 2 3 2 1 0
Likert.f
Question 3 4 5 6 7 8 9 10
AnswerQuest 0 0 0 4 3 3 2 0
Delivery 0 0 0 1 1 6 3 1
Informative 1 2 2 1 1 3 2 0
VisualAides 0 2 3 2 5 0 0 0
, , Question = AnswerQuest
Likert.f
Instructor 3 4 5 6 7 8 9 10
Fuu 0 0 0 0 0 2 2 0
Jin 0 0 0 2 1 1 0 0
Mugen 0 0 0 2 2 0 0 0
, , Question = Delivery
Likert.f
Instructor 3 4 5 6 7 8 9 10
Fuu 0 0 0 0 0 2 1 1
Jin 0 0 0 1 0 2 1 0
Mugen 0 0 0 0 1 2 1 0
, , Question = Informative
Likert.f
Instructor 3 4 5 6 7 8 9 10
Fuu 0 0 0 0 0 2 2 0
Jin 0 0 1 1 1 1 0 0
Mugen 1 2 1 0 0 0 0 0
, , Question = VisualAides
Likert.f
Instructor 3 4 5 6 7 8 9 10
Fuu 0 0 0 1 3 0 0 0
Jin 0 0 1 1 2 0 0 0
Mugen 0 2 2 0 0 0 0 0
359
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
data=Data,
layout=c(1,3) # columns and rows of individual plots
)
library(lattice)
360
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(lattice)
361
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Summarize(Likert ~ Instructor,
data=Data,
digits=3)
library(FSA)
Summarize(Likert ~ Question,
data=Data,
digits=3)
362
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(FSA)
Sum
library(ggplot2)
pd = position_dodge(.2)
ggplot(Sum, aes(x=Instructor,
y=median,
color=Question)) +
geom_errorbar(aes(ymin=Q1,
ymax=Q3),
width=.2, size=0.7, position=pd) +
geom_point(shape=15, size=4, position=pd) +
theme_bw() +
theme(axis.title = element_text(face = "bold"))+
ylab("Median Likert score")
363
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The p-values for the two main effects and the interaction effect is determined using the Anova function
in the packages RVAideMemoire and car.
Here the threshold = "symmetric" option is used in order to avoid errors. This option does not need to
be used routinely.
library(car)
library(RVAideMemoire)
364
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Anova(model,
type = "II")
LR Chisq Df Pr(>Chisq)
Instructor 32.157 2 1.040e-07 ***
Question 28.248 3 3.221e-06 ***
Instructor:Question 24.326 6 0.0004548 ***
nagelkerke(model)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.400602
Cox and Snell (ML) 0.775956
Nagelkerke (Cragg and Uhler) 0.794950
$Likelihood.ratio.test
Because the interaction term in the model was significant, the group separation for the interaction
effect is explored.
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Instructor + Question,
adjust="tukey") ### Tukey-adjusted comparisons
leastsquare
cld(leastsquare,
alpha = 0.05,
365
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
### Remember to ignore “lsmean”, “SE”, “LCL”, and “UCL” with CLM
Groups sharing a letter are not significantly different. In this case, because so many groups share a
letter, it is difficult to interpret this plot. One approach would be to look at differences among
instructors for each question. Looking at AnswerQuest, Fuu’s scores are not statistically different than
Jin’s (because they share the letters d and e), but Fuu’s scores are different than Mugen’s (because they
share no letters). So, we can conclude for this question, that Fuu’s scores are significantly greater than
Mugen’s. And so on.
366
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
scale_test(model)
367
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Warning message:
(-1) Model failed to converge with max|grad| = 1.70325e-06 (tol = 1e-06)
In addition: maximum number of consecutive Newton modifications reached
### This test failed, but the results suggest no violation of assumptions.
Optional analyses
Here, the threshold = "flexible" option is used in order to avoid errors for this data set. This option does
not need to be used routinely. Note that potentially important errors are produced in the pairwise
post-hoc tests on the interaction factor. One reason for this is that there are few observations for each
combination of Instructor and Question.
Table format
### Create a new variable for interaction term
Data$Interaction = factor(Data$Interaction,
levels=unique(Data$Interaction))
library(psych)
headTail(Data)
Data$Interaction = factor(Data$Interaction,
levels = c("Fuu.AnswerQuest", "Fuu.Delivery",
"Fuu.Informative", "Mugen.Delivery",
"Jin.Delivery", "Fuu.VisualAides",
"Mugen.AnswerQuest", "Jin.AnswerQuest",
"Jin.VisualAides"," Jin.Informative",
"Mugen.VisualAides", "Mugen.Informative"))
library(rcompanion)
PT = pairwiseOrdinalTest(x = Data$Likert.f,
g = Data$Interaction,
threshold = "flexible",
method = "fdr")
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
PT
368
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
warnings()
### Results may not be reliable for this data set for these pairwise tests
library (rcompanion)
cldList(comparison = PT$Comparison,
p.value = PT$p.adjust,
threshold = 0.05)
369
TWO-WAY ORDINAL ANOVA WITH CLM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data$Interaction = factor(Data$Interaction,
levels=unique(Data$Interaction))
library(psych)
headTail(Data)
Data$Interaction = factor(Data$Interaction,
levels = c("Fuu.AnswerQuest", "Fuu.Delivery",
"Fuu.Informative", "Mugen.Delivery",
"Jin.Delivery", "Fuu.VisualAides",
"Mugen.AnswerQuest", "Jin.AnswerQuest",
"Jin.VisualAides"," Jin.Informative",
"Mugen.VisualAides", "Mugen.Informative"
library(rcompanion)
PM = pairwiseOrdinalMatrix(x = Data$Likert.f,
g = Data$Interaction,
threshold = "flexible",
method = "fdr")
# Adjusts p-values for multiple comparisons;
# See ?p.adjust for options
PM
library(multcompView)
multcompLetters(PM$Adjusted,
compare="<",
threshold=0.05, # p-value to use as significance threshold
Letters=letters,
reversed = FALSE)
Jin.Delivery Fuu.VisualAides
"abc" "c"
Mugen.Informative Jin.Informative
"d" "bc"
370
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
warnings()
### Results may not be reliable for this data set for these pairwise tests
The example here looks at students’ knowledge scores for three speakers across two different times.
Because we know which student has each score at each time, we will use the Student as a blocking
371
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
variable, with the suspicion that one student might have consistently lower knowledge than another
student. If we hadn’t recorded this information about students, we could use a two-way ordinal anova.
As a matter of practical interpretation, we may not care about the absolute scores for each of the three
speakers, only if the knowledge scores for students increased from Time 1 to Time 2. At Time 1,
students’ knowledge scores are lower for Piglet that for the other two speakers. This isn’t the fault of
Piglet; he is just speaking about a topic that the students have little knowledge of.
The clmm function can specify more complex models with multiple independent variables of different
types, but this book will not explore more complex examples.
The p-values for the main and interaction effects can be determined with the Anova function from
RVAideMemoire, which produces an analysis of deviance table for these effects. In addition, a p-value
for the model as a whole will be determined, along with a pseudo R-squared for the model as a whole.
Post-hoc analysis to determine which groups are different can be conducted on each significant main
effect and on the interaction effect if it is significant.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(lattice)){install.packages("lattice")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(ordinal)){install.packages("ordinal")}
if(!require(car)){install.packages("car")}
if(!require(RVAideMemoire)){install.packages("RVAideMemoire")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(plyr)){install.packages("plyr")}
if(!require(boot)){install.packages("boot")}
if(!require(rcompanion)){install.packages("rcompanion")}
372
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Pooh a 2 4
Pooh b 2 5
Pooh c 2 5
Pooh d 2 5
Pooh e 2 5
Pooh f 2 4
Pooh g 2 5
Pooh h 2 5
Piglet i 1 1
Piglet j 1 2
Piglet k 1 1
Piglet l 1 1
Piglet m 1 1
Piglet n 1 2
Piglet o 1 3
Piglet p 1 1
Piglet i 2 2
Piglet j 2 4
Piglet k 2 2
Piglet l 2 1
Piglet m 2 2
Piglet n 2 2
Piglet o 2 4
Piglet p 2 2
Eeyore q 1 4
Eeyore r 1 5
Eeyore s 1 4
Eeyore t 1 4
Eeyore u 1 4
Eeyore v 1 4
Eeyore w 1 4
Eeyore x 1 4
Eeyore q 2 5
Eeyore r 2 4
Eeyore s 2 4
373
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Eeyore t 2 4
Eeyore u 2 3
Eeyore v 2 4
Eeyore w 2 4
Eeyore x 2 4
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Time = factor(Data$Time)
Data$Speaker = factor(Data$Speaker,
levels=unique(Data$Speaker))
### Create a new variable which is the likert scores as an ordered factor
Data$Likert.f = factor(Data$Likert,
ordered = TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Likert.f
Time 1 2 3 4 5
1 0 0 2 5 1
2 0 0 0 2 6
, , Speaker = Piglet
374
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Likert.f
Time 1 2 3 4 5
1 5 2 1 0 0
2 1 5 0 2 0
, , Speaker = Eeyore
Likert.f
Time 1 2 3 4 5
1 0 0 0 7 1
2 0 0 1 6 1
library(lattice)
375
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(lattice)
376
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Points above and to left of the blue line indicate cases in which the score for Time 2 is greater than that
for Time 1.
Note that the data must be ordered by Student and Speaker for this code to plot the data correctly.
That is, the first observation for Time==1 must be the same student and speaker as the first
observation for Time==2.
Also note that the points on the plot are jittered so that multiple points with the same values will be
visible.
Time.1 = Data$Likert[Data$Time==1]
Time.2 = Data$Likert[Data$Time==2]
Speaker = Data$Speaker[Data$Time==2]
plot(Time.1, jitter(Time.2),
pch = 16,
cex = 1,
col = Speaker,
xlab="Time 1",
ylab="Time 2")
legend('bottomright',
legend = unique(Speaker),
col = 1:3,
cex = 1,
pch = 16)
377
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The plot makes it easy to see the change in median score for each speaker from Time 1 to Time 2.
library(rcompanion)
library(ggplot2)
pd = position_dodge(.2)
ggplot(Sum, aes(x=Time,
y=Median,
color=Speaker)) +
geom_errorbar(aes(ymin=Percentile.lower,
ymax=Percentile.upper),
width=.2, size=0.7, position=pd) +
geom_point(shape=15, size=4, position=pd) +
theme_bw() +
378
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The p-values for the two main effects and the interaction effect is determined using the Anova function
in the packages RVAideMemoire and car.
Here the threshold = "equidistant" option is used in order to avoid errors for this data set. This option
does not need to be used routinely.
379
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
I consider it a good practice to assign unique individuals with unique labels. This practice is not
always followed, but following the practice will avoid mis-specifying models. In this case with 24
students, if they had been lettered a–h for each Speaker, and the original model with the (1|Student)
effect had been used, the results would be wrong because the model would treat Pooh’s student a as
the same individual as Piglet’s student a.
library(car)
library(RVAideMemoire)
Anova(model,
type = "II")
LR Chisq Df Pr(>Chisq)
Time 9.774 1 0.00177 **
Speaker 34.485 2 3.249e-08 ***
Time:Speaker 28.202 2 7.517e-07 ***
We want the null model to have no fixed effects except for an intercept, indicated with a 1 on the right
side of the ~. But we also may want to include the random effects, in this case (1 | Student).
We can compare our full model against a model with an overall intercept and an intercept for each
student.
Be sure that the threshold option in model.null is the same as in the original model.
model.null = clmm(Likert.f ~ 1 + (1 | Student),
data = Data,
threshold = "equidistant")
library(rcompanion)
nagelkerke(fit = model,
null = model.null)
$Pseudo.R.squared.for.model.vs.null
380
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Pseudo.R.squared
McFadden 0.544198
Cox and Snell (ML) 0.787504
Nagelkerke (Cragg and Uhler) 0.836055
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-5 -37.172 74.344 1.275e-14
Another approach to determining the p-value and pseudo R-squared for a clmm model is comparing
the model to a null model with only an intercept and neither the fixed nor the random effects.
model.null.2 = clm(Likert.f ~ 1,
data = Data,
threshold = "equidistant")
library(rcompanion)
nagelkerke(fit = model,
null = model.null.2)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.587734
Cox and Snell (ML) 0.842667
Nagelkerke (Cragg and Uhler) 0.880526
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-6 -44.385 88.771 5.4539e-17
First, model.fixed is defined, and then the two models are passed to the anova function.
Be sure that the threshold option in model.fixed is the same as in the original model.
model.fixed = clm(Likert.f ~ Time + Speaker + Time:Speaker,
data = Data,
threshold = "equidistant")
anova(model,
model.fixed)
381
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Because the interaction term in the model was significant, the group separations for the interaction is
explored.
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Speaker + Time,
adjust="tukey") ### Tukey-adjusted comparisons
leastsquare
cld(leastsquare,
alpha = 0.05,
Letters = letters, ### Use lower-case letters for .group
adjust = "tukey") ### Tukey-adjusted comparisons
### Remember to ignore “lsmean”, “LCL”, “LCL”, and “UCL” with CLMM
With the results in this format, it is easy to see that the scores for both Pooh and Piglet improved from
Time 1 to Time 2, but that the scores for poor Eeyore did not.
382
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Warning message:
(1) Hessian is numerically singular: parameters are not uniquely determined
In addition: Absolute convergence criterion was met, but relative criterion was
not met.
### The fitting of this model failed. We’ll try a model without student.
383
TWO-WAY REPEATED ORDINAL ANOVA WITH CLMM SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
nominal_test(model.clm)
scale_test(model.clm)
Warning messages:
1: (-1) Model failed to converge with max|grad| = 2.26258e-05 (tol = 1e-06)
In addition: iteration limit reached
### This test failed. It would be possible to adjust the fitting parameters,
### and try to get the model to converge.
384
INTRODUCTION TO TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Tests of symmetry, or marginal homogeneity, can determine if frequencies for one nominal variable
are greater than that for another, or if there was a change in frequencies from sampling at one time to
another.
As a more advanced approach, models can be specified with nominal dependent variables. A common
type of model with a nominal dependent variable is logistic regression.
Descriptive statistics for nominal data are discussed in the “Descriptive statistics for nominal data”
section in the Descriptive Statistics chapter.
Descriptive plots for nominal data are discussed in the “Examples of basic plots for nominal data”
section in the Basic Plots chapter.
Contingency tables
Nominal data are often arranged in a contingency table of counts of observations for each cell of the
table. For example, if there were 6 males and 4 females reading Sappho, 5 males and 4 females
reading Steven Crane, and 7 males and 10 females reading Judith Viorst, the data could be arranged as:
Sex
Male Female
Poet
Sappho 6 4
Crane 3 4
Viorst 2 5
Matrix = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
Matrix
385
INTRODUCTION TO TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Male Female
Sappho 6 4
Crane 3 4
Viorst 2 5
Male Female
11 13
rowSums(Matrix)
Bar plots
Simple bar charts and mosaic plots are also helpful.
barplot(Matrix,
beside = TRUE,
legend = TRUE,
ylim = c(0, 8), ### y-axis: used to prevent legend overlapping bars
cex.names = 0.8, ### Text size for bars
cex.axis = 0.8, ### Text size for axis
args.legend = list(x = "topright", ### Legend location
cex = 0.8, ### Legend text size
bty = "n")) ### Remove legend box
386
INTRODUCTION TO TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
barplot(Matrix.t,
beside = TRUE,
legend = TRUE,
ylim = c(0, 8), ### y-axis: used to prevent legend overlapping bars
cex.names = 0.8, ### Text size for bars
cex.axis = 0.8, ### Text size for axis
args.legend = list(x = "topright", ### Legend location
cex = 0.8, ### Legend text size
bty = "n")) ### Remove legend box
Mosaic plots
Mosaic plots are very useful for visualizing the association between two nominal variables, but can be
somewhat tricky to interpret for those unfamiliar with them. Note that the column width is
determined by the number of observation in that category. In this case, the Sappho column is wider
because more students are reading Sappho than the other two poets. Note, too, that the number of
observations in each cell is determined by the area of the cell, not its height. In this case, the Sappho–
Female cell and the Crane–Female cell have the same count (4), and so the same area. The Crane–
Female cell is taller than the Sappho–Female because it is a higher proportion of observations for that
author (4 out of 7 Crane readers compared with 4 out of 10 Sappho readers).
mosaicplot(Matrix,
color=TRUE,
cex.axis=0.8)
387
INTRODUCTION TO TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
In R, most simple analyses for nominal data expect the data to be in a matrix format. However, data
may be in a long format, either with each row representing a single observation, or with each row
containing a count of observations.
It is relatively easy to convert long-format data to a matrix. The xtabs function will produce a table
that can be used for most functions expecting data to be formatted as a matrix object.
388
INTRODUCTION TO TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Poet = factor(Data$Poet,
levels=unique(Data$Poet))
Data$Sex = factor(Data$Sex,
levels=unique(Data$Sex))
Table
Sex
Poet Male Female
Sappho 6 4
Crane 3 4
Viorst 2 5
Data = read.table(textConnection(Input),header=TRUE)
Data$Poet = factor(Data$Poet,
levels=unique(Data$Poet))
Data$Sex = factor(Data$Sex,
levels=unique(Data$Sex))
Table
Sex
Poet Male Female
389
CONFIDENCE INTERVALS FOR PROPORTIONS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Sappho 6 4
Crane 3 4
Viorst 2 5
Male Female
Sappho 6 4
Crane 3 4
Viorst 2 5
class(Matrix)
[1] "matrix"
typeof(Matrix)
[1]"integer"
attributes(Matrix)
$dim
[1] 3 2
$dimnames
$dimnames[[1]]
[1] "Sappho" "Crane" "Viorst"
$dimnames[[2]]
[1] "Male" "Female"
str(Matrix)
390
CONFIDENCE INTERVALS FOR PROPORTIONS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
the proportion of females are 12/20, or 0. 6, and the proportion of males are 8/20 or 0.4. This is a
binomial proportion.
Sex Count Proportion
Female 12 0.60
Male 8 0.40
------ -- -----------
Total 20 1.00
A multinomial proportion has counts for more than two levels of a nominal variable. For example, we
might have the following levels and counts for sex for students in a class:
Sex Count Proportion
Female 12 0.60
Male 6 0.30
Other 1 0.05
Prefer not to answer 1 0.05
-------------------- -- -----------
Total 20 1.00
The binom.test function in the native stats package will provide the Clopper-Pearson confidence
interval for a binomial proportion. Other methods for a binomial proportion are provided by the
BinomCI function in the DescTools package, as well as by various functions in the PropCIs package.
Confidence intervals for multinomial proportions can be produced with the MultinomCI function in the
DescTools package.
The following commands will install these packages if they are not already installed:
if(!require(DescTools)){install.packages("DescTools")}
if(!require(PropCIs)){install.packages("PropCIs")}
391
CONFIDENCE INTERVALS FOR PROPORTIONS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
---------- --
Total 21
Note that when calculating confidence intervals for a binomial variable, one level of the nominal
variable is chosen to be the “success” level. This is an arbitrary decision, but you should be cautious to
remember that the confidence interval is reported for the proportion of “success” responses. The
BinomCI function in the DescTools package can produce the confidence interval for both “success” and
“failure” in one step.
The binom.test function output includes a confidence interval for the proportion, and the proportion of
“success” as a decimal number. The binom.test function uses the Clopper–Pearson method for
confidence intervals.
binom.test(7, 21,
0.5,
alternative="two.sided",
conf.level=0.95)
sample estimates:
probability of success
0.3333333
The BinomCI function in the DescTools package has several methods for calculating confidence
intervals for a binomial proportion.
library(DescTools)
BinomCI(7, 21,
conf.level = 0.95,
method = "clopper-pearson")
The BinomCI function in the DescTools package can also produce the confidence intervals for “success”
and “failure” in one step.
library(DescTools)
total = sum(observed)
392
CONFIDENCE INTERVALS FOR PROPORTIONS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
BinomCI(observed, total,
conf.level = 0.95,
method = "clopper-pearson")
The PropCIs package has functions for calculating confidence intervals for a binomial proportion.
7/21
[1] 0.3333333
library(PropCIs)
exactci(7, 21,
conf.level=0.95)
[1] 0.3333333
library(PropCIs)
blakerci(7, 21,
conf.level=0.95)
393
CONFIDENCE INTERVALS FOR PROPORTIONS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Female 10
Male 9
Other 1
No answer 1
---------- -----
Total 21
library(DescTools)
observed = c(10, 9, 1, 1)
MultinomCI(observed,
conf.level=0.95,
method="sisonglaz")
Two functions in the PropCIs package can determine a confidence interval for a difference for in
independent proportions.
7/21
[1] 0.3333333
13/17
[1] 0.7647059
394
GOODNESS-OF-FIT TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
(7/21) - (13/17)
[1] -0.4313725
library(PropCIs)
sample estimates:
[1] -0.4313725
References
“Confidence Limits” in Mangiafico, S.S. 2015a. An R Companion for the Handbook of Biological Statistics,
version 1.09. rcompanion.org/rcompanion/c_04.html.
In general, there are no assumptions about the distribution of data for these tests. However, the
results of chi-square tests and G-tests can be inaccurate if cell counts are low. A rule of thumb is that
all cell counts should be 5 or greater for chi-square- and G-tests. For a more complete discussion, see
McDonald in the “Optional Readings” section for details on what constitutes low cell counts.
One approach is to use exact tests, which are not bothered by low cell counts. However, if there are
not low cell counts, using G-test or chi-square test is fine. G-test is probably technically a better test
395
GOODNESS-OF-FIT TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
than chi-square. The advantage of chi-square tests is that your audience may be more familiar with
them.
G-tests are also called likelihood ratio tests, and “Likelihood Ratio Chi-Square” by SAS.
Appropriate data
• A nominal variable with two or more levels
• Theoretical, typical, expected, or neutral values for the proportions for this variable are
needed for comparison
• G-test and chi-square test may not be appropriate if there are cells with low counts in them
Hypotheses
• Null hypothesis: The proportions for the levels for the nominal variable are not different
from the expected proportions.
• Alternative hypothesis (two-sided): The proportions for the levels for the nominal variable
are different from the expected proportions.
Interpretation
Significant results can be reported as “The proportions for the levels for the nominal variable
were statistically different from the expected proportions.”
The following commands will install these packages if they are not already installed:
if(!require(EMT)){install.packages("EMT")}
if(!require(DescTools)){install.packages("DescTools")}
if(!require(ggplot2)){install.packages("ggplot2")}
As part of a demographic survey of students in this environmental issues webinar series, Alucard
recorded the race and ethnicity of his students. He wants to compare the data for his class to the
demographic data for Cumberland County, New Jersey as a whole
Race Alucard’s_class County_proportion
White 20 0.775
Black 9 0.132
American Indian 9 0.012
Asian 1 0.054
Pacific Islander 1 0.002
Two or more races 1 0.025
----------------- --- ------
396
GOODNESS-OF-FIT TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Total 41 1.000
Race data
observed = c(20, 9, 9, 1, 1, 1)
expected = c(0.775, 0.132, 0.012, 0.054, 0.002, 0.025)
library(EMT)
multinomial.test(observed, expected)
observed = c(20, 9, 9, 1, 1, 1)
expected = c(0.775, 0.132, 0.012, 0.054, 0.002, 0.025)
library(EMT)
multinomial.test(observed, expected,
MonteCarlo = TRUE)
Ethnicity data
x = 7
n = 41
expected = 0.174
binom.test(x, n, expected)
397
GOODNESS-OF-FIT TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Race data
observed = c(20, 9, 9, 1, 1, 1)
expected = c(0.775, 0.132, 0.012, 0.054, 0.002, 0.025)
library(DescTools)
GTest(x=observed,
p=expected,
correct="none")
data: observed
G = 46.317, X-squared df = 5, p-value = 7.827e-09
Ethnicity data
observed = c(7, 34)
expected = c(0.174, 0.826)
library(DescTools)
GTest(x=observed,
p=expected,
correct="none")
Race data
observed = c(20, 9, 9, 1, 1, 1)
expected = c(0.775, 0.132, 0.012, 0.054, 0.002, 0.025)
chisq.test(x = observed,
p = expected)
398
GOODNESS-OF-FIT TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Ethnicity data
observed = c(7, 34)
expected = c(0.174, 0.826)
chisq.test(x = observed,
p = expected)
This is an example of a multinomial test that includes a bar plot showing confidence intervals. The
data is a simple vector of counts, like the above example.
For a similar example using two-way count data which is organized into a data frame, see the
"Examples of basic plots for nominal data" section in the Basic Plots chapter.
Walking to the store, Jerry Coyne observed the colors of nail polish on women’s toes (Coyne, 2016).
Presumably because that’s the kind of thing retired professors are apt to do. He concluded that red
was a more popular color but didn’t do any statistical analysis to support his conclusion.
Color of polish Count
Red 19
None or clear 3
White 1
Green 1
Purple 2
Blue 2
We will use a multinomial goodness of fit test to determine if there is an overall difference in the
proportion of colors (multinomial.test function in the EMT package). The confidence intervals for each
proportion can be found with the MultinomCI function in the DescTools package. The data then needs
to be manipulated some so that we can plot the data as counts and not proportions.
Note here that the expected counts are simply 1 divided by the number of treatments. In this case the
null hypothesis is that the observed proportions are all the same. In the examples above, the null
hypothesis was that the observed proportions were not different than expected proportions. It’s two
ways to think of the same null hypothesis.
399
GOODNESS-OF-FIT TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The confidence intervals for each proportion can be used as a post-hoc test, to determine which
proportions differ from each other, or to determine which proportions differ from 0.
nail.color = c("Red", "None", "White", "Green", "Purple", "Blue")
observed = c( 19, 3, 1, 1, 2, 2 )
expected = c( 1/6, 1/6, 1/6, 1/6, 1/6, 1/6 )
library(EMT)
multinomial.test(observed,
expected)
### This may take a while. Use Monte Carlo for large numbers.
library(DescTools)
MCI = MultinomCI(observed,
conf.level=0.95,
method="sisonglaz")
MCI
Nail.color = factor(nail.color,
levels=unique(nail.color))
### For plot, Create variables of counts, and then wrap them into a data frame
Total = sum(observed)
Count = observed
Lower = MCI[,'lwr.ci'] * Total
Upper = MCI[,'upr.ci'] * Total
Data
400
GOODNESS-OF-FIT TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(ggplot2)
geom_bar(stat = "identity",
color = "black",
fill = "gray50",
width = 0.7) +
geom_errorbar(aes(ymin = Lower,
ymax = Upper),
width = 0.2,
size = 0.7,
position = pd,
color = "black"
) +
theme_bw() +
theme(axis.title = element_text(face = "bold")) +
ylab("Count of observations") +
xlab("Nail color")
Bar plot of the count of the color of women’s toenail polish observed by Jerry
401
ASSOCIATION TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Coyne while walking to the store. Error bars indicate 95% confidence intervals
(Sison and Glaz method).
Optional readings
“Small numbers in chi-square and G–tests” in McDonald, J.H. 2014. Handbook of Biological Statistics.
www.biostathandbook.com/small.html.
References
Coyne, J. A. 2016. Why is red nail polish so popular? Why Evolution is True.
whyevolutionistrue.wordpress.com/2016/07/02/why-is-red-nail-polish-so-popular/.
“Chi-square Test of Goodness-of-Fit” in Mangiafico, S.S. 2015a. An R Companion for the Handbook of
Biological Statistics, version 1.09. rcompanion.org/rcompanion/b_03.html.
“Exact Test of Goodness-of-Fit” in Mangiafico, S.S. 2015a. An R Companion for the Handbook of
Biological Statistics, version 1.09. rcompanion.org/rcompanion/b_01.html.
“G–test of Goodness-of-Fit” in Mangiafico, S.S. 2015a. An R Companion for the Handbook of Biological
Statistics, version 1.09. rcompanion.org/rcompanion/b_04.html.
“Repeated G–tests of Goodness-of-Fit” in Mangiafico, S.S. 2015a. An R Companion for the Handbook of
Biological Statistics, version 1.09. rcompanion.org/rcompanion/b_09.html.
In general, there are no assumptions about the distribution of data for these tests.
Note that for these tests of association there shouldn’t be paired values. For example, if experimental
units—the things you are counting—are “students before” and “students after”, or “left hands” and
“right hands”, the tests in the chapter Tests for Paired Nominal Data may be more appropriate.
Also note that these tests will not be accurate if there are “structural zeros” in the contingency table. If
you were counting pregnant and non-pregnant individuals across categories like male and female, the
male–pregnant cell may contain a structural zero if you assume your population cannot have pregnant
males.
One approach when there are low counts is to use exact tests, such as Fisher’s exact test, which are not
bothered by low cell counts.
Another approach is to apply a continuity correction to the test. The chisq.test function automatically
applies the Yates's continuity correction for 2 x 2 tables. The GTest function has options for Yates or
Williams corrections.
It appears to me that this assumption is commonly ignored, but I don’t know if this is because of
ignorance of this assumption, or just that some people assume that advantages of the exact test
outweigh using it in cases when this assumption is violated.
Appropriate data
• Two nominal variables with two or more levels each.
• Experimental units aren’t paired.
• There are no structural zeros in the contingency table.
• G-test and chi-square test may not be appropriate if there are cells with low counts in them.
Hypotheses
• Null hypothesis: There is no association between the two variables.
• Alternative hypothesis (two-sided): There is an association between the two variables.
Interpretation
Significant results can be reported as “There was a significant association between variable A
and variable B.”
Post-hoc analysis
Post hoc analysis for tests on a contingency table larger than 2 x 2 can be conducted by
conducting tests for the component 2 x 2 tables. A correction for multiple tests should be
applied.
403
ASSOCIATION TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The following commands will install these packages if they are not already installed:
if(!require(DescTools)){install.packages("DescTools")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(rcompanion)){install.packages("rcompanion")}
Alexander Anderson runs the pesticide safety training course in four counties. Students must pass in
order to obtain their pesticide applicator’s license. He wishes to see if there is an association between
the county in which the course was held and the rate of passing the test. The following are his data.
Input =("
County Pass Fail
Bloom 21 5
Cobblestone 6 11
Dougal 7 8
Heimlich 27 5
")
Matrix = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
Matrix
p-value = 0.000668
alternative hypothesis: two.sided
404
ASSOCIATION TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Post-hoc analysis
Post-hoc analysis can be conducted with pairwise Fisher’s exact tests. The function
pairwiseNominalIndependence in the rcompanion package can be used to conduct this analysis.
### Order matrix
Matrix
library(rcompanion)
PT = pairwiseNominalIndependence(Matrix,
compare = "row",
fisher = TRUE,
gtest = FALSE,
chisq = FALSE,
method = "fdr", # see ?p.adjust for options
digits = 3)
PT
library(rcompanion)
cldList(comparison = PT$Comparison,
p.value = PT$p.adj.Fisher,
threshold = 0.05)
The table of adjusted p-values can be summarized to a table of letters indicating which treatments are
not significantly different.
County Percent passing Letter
Heimlich County 84% a
405
ASSOCIATION TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Bloom County 81 ab
Dougal County 47 bc
Cobblestone County 35 c
Counties sharing a letter are not significantly different by Fisher exact test,
with p-values adjusted by FDR method for multiple comparisons (Benjamini–Hochberg
false discovery rate).
This table of letters can also be found using the pairwiseNominalMatrix function along with the
multcompLetters function in the multcompView package.
### Order matrix
Matrix
library(rcompanion)
PM = pairwiseNominalMatrix(Matrix,
compare = "row",
fisher = TRUE,
gtest = FALSE,
chisq = FALSE,
method = "fdr", # see ?p.adjust for options
digits = 3)
PM
$Test
[1] "Fisher exact test"
$Method
[1] "fdr"
$Adjusted
Heimlich Bloom Dougal Cobblestone
Heimlich 1.00000 0.7400 0.0262 0.00596
Bloom 0.74000 1.0000 0.0564 0.01190
Dougal 0.02620 0.0564 1.0000 0.74000
Cobblestone 0.00596 0.0119 0.7400 1.00000
library(multcompView)
multcompLetters(PM$Adjusted,
compare="<",
threshold=0.05, ### p-value to use as significance threshold
Letters=letters,
reversed = FALSE)
406
ASSOCIATION TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
G-test of association
library(DescTools)
GTest(Matrix)
Post-hoc analysis
### Order matrix
Matrix
pairwiseNominalIndependence(Matrix,
compare = "row",
fisher = FALSE,
gtest = TRUE,
chisq = FALSE,
method = "fdr", # see ?p.adjust for options
digits = 3)
The table of adjusted p-values can be summarized to a table of letters indicating which treatments are
not significantly different.
County Percent passing Letter
Heimlich County 84% a
Bloom County 81 a
Dougal County 47 b
Cobblestone County 35 b
407
ASSOCIATION TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Post-hoc analysis
### Order matrix
Matrix
library(rcompanion)
pairwiseNominalIndependence(Matrix,
compare = "row",
fisher = FALSE,
gtest = FALSE,
chisq = TRUE,
method = "fdr", # see ?p.adjust for options
digits = 3)
The table of adjusted p-values can be summarized to a table of letters indicating which treatments are
not significantly different.
County Percent passing Letter
Heimlich County 84% a
Bloom County 81 ab
Dougal County 47 bc
Cobblestone County 35 c
408
ASSOCIATION TESTS FOR NOMINAL VARIABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Optional readings
“Small numbers in chi-square and G–tests” in McDonald, J.H. 2014. Handbook of Biological Statistics.
www.biostathandbook.com/small.html.
References
“Fisher’s Exact Test of Independence” in Mangiafico, S.S. 2015a. An R Companion for the Handbook of
Biological Statistics, version 1.09. rcompanion.org/rcompanion/b_07.html.
“G–test of Independence” in Mangiafico, S.S. 2015b. An R Companion for the Handbook of Biological
Statistics, version 1.09. rcompanion.org/rcompanion/b_06.html.
“Chi-square Test of Independence” in Mangiafico, S.S. 2015c. An R Companion for the Handbook of
Biological Statistics, version 1.09. rcompanion.org/rcompanion/b_05.html.
“Small Numbers in Chi-square and G–tests” in Mangiafico, S.S. 2015d. An R Companion for the
Handbook of Biological Statistics, version 1.09. rcompanion.org/rcompanion/b_08.html.
Exercises M
Statistically, which counties performed the best? Which performed the worst? Be sure to
indicate which post-hoc test you are using.
Plot the data in an appropriate way. What does the plot suggest to you?
2. Ryuk and Rem held a workshop on planting habitat for pollinators like bees and butterflies. They
wish to know if there is an association between the profession of the attendees and their willingness to
undertake a conservation planting. The following are the data.
Will plant?
Profession Yes No
Homeowner 13 14
Landscaper 27 6
Farmer 7 19
NGO 6 6
409
TESTS FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
Statistically, which professions were most willing? Which were least willing? Be sure to
indicate which post-hoc test you are using.
Plot the data in an appropriate way. What does the plot suggest to you?
As an example, consider a question on repeated on a pre-test and a post-test. We may want to know if
the number of correct responses changed from the pre-test to the post-test.
Did students have the correct answer to the question?
After
Before Correct Incorrect
Correct 2 0
Incorrect 21 7
Note that the row names and column names have the same levels, and that counts represent paired
responses. That is, for each observation you must know the individual’s response before and after.
Also note that the number of students included in the table are 30, or the sum of the cell counts.
In essence, those students with the same response before and after don’t affect the assessment of the
change in responses. We would focus on the “discordant” counts. That is, How many students had
incorrect answers before and correct answers after, in contrast to those who had the reverse trend.
Here, because 21 changed from incorrect to correct, and 0 changed from correct to incorrect, we might
suspect that there was a significant change in responses from incorrect to correct.
To grasp the difference between nominal tests of association and nominal tests of symmetry, be sure
to visit the coffee and tea example below in the section “An example without repeated measures,
comparing test of symmetry with test of association”.
410
TESTS FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Appropriate data
• Two nominal variables with two or more levels each, and each with the same levels.
• Observations are paired or matched between the two variables.
• McNemar and McNemar–Bowker tests may not be appropriate if discordant cells have low
counts.
Hypotheses
• Null hypothesis: The contingency table is symmetric. That is, the probability of cell [i, j] is
equal to the probability of cell [j, i].
• Alternative hypothesis (two-sided): The contingency table is not symmetric.
Interpretation
Depending on the context, significant results can be reported as e.g. “There was a significant
change from answer A to answer B.” Or, “X was more popular than Y.”
Post-hoc analysis
Post hoc analysis for tests on a contingency table larger than 2 x 2 can be conducted by
conducting tests for the component 2 x 2 tables. A correction for multiple tests should be
applied.
The following commands will install these packages if they are not already installed:
if(!require(EMT)){install.packages("EMT")}
if(!require(rcompanion)){install.packages("rcompanion")}
McNemar’s test may not be reliable if there are low counts in the “discordant” cells. Authors
recommend that these cells to sum to at least 5 or 10 or 25.
Exact tests
Exact tests of symmetry reduce to exact tests for goodness-of-fit. A 2 x 2 table is analyzed with a
binomial exact test, and a larger table is analyzed with a multinomial exact test. Examples of these are
shown in this chapter in the “Optional analyses: conducting exact tests for symmetry” section.
411
TESTS FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Alucard teaches a Master Gardener training on rain gardens for stormwater management and one on
rain barrels. He wishes to assess if people are more willing to install these green infrastructure
practices after attending the training. His data follow. Note that there are 46 attendees answering
each question.
Are you planning to install a rain barrel?
After
Before Yes No
Yes 9 5
No 17 15
After
Before Yes No Maybe
Yes 6 0 1
No 5 3 7
Maybe 11 1 12
Rain barrel
Input =("
Before After.yes After.no
Before.yes 9 5
Before.no 17 15
")
Matrix.1 = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
Matrix.1
sum(Matrix.1)
[1] 46
Rain garden
Input =("
Before Yes.after No.after Maybe.after
Yes.before 6 0 1
No.before 5 3 7
Maybe.before 11 1 12
412
TESTS FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
")
Matrix.2 = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
Matrix.2
sum(Matrix.2)
[1] 46
Rain barrel
library(rcompanion)
nominalSymmetryTest(Matrix.1,
digits = 3)
$Global.test.for.symmetry
Dimensions p.value
1 2 x 2 0.0169
Rain garden
library(rcompanion)
nominalSymmetryTest (Matrix.2,
method="fdr",
digits = 3)
$Global.test.for.symmetry
Dimensions p.value
1 3 x 3 2e-04
$Pairwise.symmetry.tests
Comparison p.value p.adjust
1 Yes.before/Yes.after : No.before/No.after 0.0625 0.0703
2 Yes.before/Yes.after : Maybe.before/Maybe.after 0.00635 0.0190
3 No.before/No.after : Maybe.before/Maybe.after 0.0703 0.0703
$p.adjustment
Method
1 fdr
413
TESTS FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Rain barrel
mcnemar.test(Matrix.1)
Rain garden
mcnemar.test(Matrix.2)
An example without repeated measures, comparing test of symmetry with test of association
As another example, consider a survey of tea and coffee drinking, in which each respondent is asked
both if they drink coffee, and if they drink tea.
Tea
Coffee Yes No
Yes 37 17
No 9 25
We would use a test of symmetry in this case if the question we wanted to answer was, Is coffee more
popular than tea? That is, is it more common for someone to drink coffee and not tea than to drink tea
and not coffee? (Those who drink both or drink neither are not relevant to this question.)
Note also that this is an example of using a test of symmetry to test the relative frequency of two
dichotomous variables when the same subjects are surveyed.
Input =("
Coffee Yes No
Yes 37 17
No 9 25
")
414
TESTS FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Matrix.3 = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
mcnemar.test(Matrix.3)
A test of association answers a very different question. Namely, Is coffee drinking associated with tea
drinking? That is, is someone more likely to drink tea if they drink coffee?
chisq.test(Matrix.3)
### Coffee drinking and tea drinking are associated, in this case people who
### drink coffee are likely to drink tea. This is a positive association.
### A negative association could also be significant.
Matrix row names are the attendees’ original religions, and the column names, with a “2” added, are
attendees’ new religions after the caucus. Note there are several 0 counts in the matrix.
Note first that the mcnemar.test function fails, namely because of the position of some 0 counts in the
matrix.
Second, note that the multinomial.test function used by the nominalSymmetryTest function would take
a long time to calculate an exact p-value for this matrix, so the MonteCarlo=TRUE option is used. The
number of samples used in the Monte Carlo approach can be adjusted with the ntrial option. Some of
the p-values in the post-hoc analysis cannot be produced because of the placement of 0 counts, but
these should also be considered non-significant results.
Input =("
Before Pastafarian2 Discordiant2 Dudist2 Jedi2
Pastafarian 7 0 23 0
Discordiant 0 7 0 33
Dudist 3 0 7 1
Jedi 0 1 0 7
415
TESTS FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
")
Matrix.4 = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
Matrix.4
Exact test
library(rcompanion)
nominalSymmetryTest(Matrix.4,
method="fdr",
digits = 3,
MonteCarlo = TRUE,
ntrial = 100000)
$Global.test.for.symmetry
Dimensions p.value
1 4 x 4 0
$Pairwise.symmetry.tests
Comparison p.value p.adjust
1 Pastafarian/Pastafarian2 : Discordiant/Discordiant2 <NA> NA
2 Pastafarian/Pastafarian2 : Dudist/Dudist2 8.8e-05 1.32e-04
3 Pastafarian/Pastafarian2 : Jedi/Jedi2 <NA> NA
4 Discordiant/Discordiant2 : Dudist/Dudist2 <NA> NA
5 Discordiant/Discordiant2 : Jedi/Jedi2 4.07e-09 1.22e-08
6 Dudist/Dudist2 : Jedi/Jedi2 1 1.00e+00
$p.adjustment
Method
1 fdr
416
TESTS FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Dudist 3 0 7 1
Jedi 0 1 0 7
After
Before Yes No
Yes 9 5
No 17 15
Rain garden
After
Before Yes No Maybe
Yes 6 0 1
No 5 3 7
Maybe 11 1 12
Rain barrel
For a 2 x 2 matrix, x = the count in one of the discordant cells, and n = the sum of the counts in
discordant cells. The expected proportion is 0.50.
binom.test(x, n, expected)
417
TESTS FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Rain garden
For an n x n matrix, observed = a vector of the counts of discordant cells. Expected is a vector of length
(n * n – n), with each value equal to (1 / (n * n – n)).
observed = c(0, 1, 5, 7, 11, 1)
expected = c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6)
library(EMT)
multinomial.test(observed, expected)
Yes–No
For post-hoc testing, reduce the matrix to a 2 x 2 matrix.
x = 0
n = 0 + 5
expected = 0.50
binom.test(x, n, expected)
Yes–Maybe
x = 1
n = 1 + 11
expected = 0.50
binom.test(x, n, expected)
No–Maybe
x = 7
n = 7 + 1
expected = 0.50
binom.test(x, n, expected)
418
TESTS FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Exercises N
How many students changed their answer on the rain barrel question from no to yes?
How would you interpret the results on the rain barrel question?
How would you interpret the result of the global test of the rain garden question?
How would you interpret the result of the post-hoc analysis of the rain garden question?
2. Considering the coffee and tea example, Do you understand the difference between the hypotheses
for tests of association and tests of symmetry? Would you be comfortable choosing the correct
approach?
3. Ryuk and Rem held a workshop on planting habitat for pollinators like bees and butterflies. They
wish to know if attendees were more likely to do a planting after the workshop than before.
Will plant?
After
Before Yes.after No.after Maybe.after
Yes 17 0 0
No 5 9 13
Maybe 15 0 7
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
419
COCHRAN–MANTEL–HAENSZEL TEST FOR 3-DIMENSIONAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The following data investigate whether there is a link between listening to podcasts and using public
transportation to get to work, collected across three cities. This can be thought of as a 2 x 2
contingency table in each of the three cities.
Data can be arranged in a table of counts or can be arranged in long-format with or without counts. If
a table is used for input, it should follow R’s ftable format, as shown.
Table format
City Bikini.Bottom Frostbite.Falls New.New.York
Listen Transport
Podcast Drive 13 17 5
Public 27 25 27
No.podcast Drive 23 22 17
Public 44 31 22
The test can be conducted with the mantelhaen.test function in the native stats package.
One assumption of the test is that there are no three-way interactions in the data. This is confirmed
with a non-significant result from a test such as the Woolf test or Breslow–Day test.
Post-hoc analysis can include looking at the individual chi-square, Fisher exact, or G-test for
association for each time or group.
420
COCHRAN–MANTEL–HAENSZEL TEST FOR 3-DIMENSIONAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Appropriate data
• Three nominal variables with two or more levels each.
• Data can be stratified as n x n tables with the third time or grouping variable
Hypotheses
• Null hypothesis: There is no association between the two inner variables.
• Alternative hypothesis (two-sided): There is an association between the two inner variables.
Interpretation
Significant results can be reported as “There was a significant association between variable A
and variable B [across groups].”
Post-hoc analysis
Post-hoc analysis can include looking at the individual chi-square, Fisher exact, or g-test for
association for each time or group.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(vcd)){install.packages("vcd")}
if(!require(DescTools)){install.packages("DescTools")}
if(!require(rcompanion)){install.packages("rcompanion")}
421
COCHRAN–MANTEL–HAENSZEL TEST FOR 3-DIMENSIONAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
Data$County = factor(Data$County,
levels=unique(Data$County))
Data$Sex = factor(Data$Sex,
levels=unique(Data$Sex))
Data$Result = factor(Data$Result,
levels=unique(Data$Result))
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
### Note that the grouping variable is last in the xtabs function
422
COCHRAN–MANTEL–HAENSZEL TEST FOR 3-DIMENSIONAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Male Pass 7 9 19 14
Fail 17 21 9 17
Cochran–Mantel–Haenszel test
mantelhaen.test(Table)
Woolf test
library(vcd)
woolf_test(Table)
Post-hoc analysis
The groupwiseCMH function will conduct analysis of the component n x n tables with Fisher exact, g-
test, or chi-square tests of association. It accepts only a 3-dimensional table. The group option
indicates which dimension should be considered the grouping variable (1, 2, or 3). It will conduct only
one type of test at a time. That is, if multiple of the options fisher, gtest, or chisq are set to TRUE, it will
conduct only one of them. As usual, method is the p-value adjustment method (see ?p.adjust for
options), and digits indicates the number of digits in the output. The correct option is used by the chi-
square test function.
library(rcompanion)
groupwiseCMH(Table,
group = 3,
fisher = TRUE,
gtest = FALSE,
chisq = FALSE,
method = "fdr",
correct = "none",
digits = 3)
423
COCHRAN’S Q TEST FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The read.ftable function can be very fussy about the formatting of the input. 1) It seems to not like a
blank first line, so the double quote symbol in the input should be on the same line as the column
names. 2) It doesn’t like leading spaces on the input lines. These may appear when you paste the code
in to the RStudio Console or R Script area. One solution is to manually delete these spaces. R Script
files that are saved without these leading spaces should be able to be opened and run without further
modification.
The Cochran–Mantel–Haenszel test, Woolf test, and post-hoc analysis would be the same as those
conducted on Table above.
Input =(
" County Bloom Cobblestone Dougal Heimlich
Sex Result
Female Pass 9 11 9 15
Fail 5 4 7 8
Male Pass 7 9 19 14
Fail 17 21 9 17
")
Table = as.table(read.ftable(textConnection(Input)))
ftable(Table)
One case would be extending the rain barrel question from the previous chapter to multiple times. A
more common case would be extending the coffee and tea example to multiple beverages. In this case,
we would test among several beverages which is more popular that the others.
Data are not typically arranged in a contingency table, but are either organized in long-format, with
each row representing a single observation, or organized in a short-format matrix with each row
representing an experimental unit with the repeated measures across columns.
Long-format
424
COCHRAN’S Q TEST FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
a Coffee Yes
a Tea Yes
a Cola No
b Coffee No
b Tea No
b Cola Yes
c Coffee Yes
c Tea No
c Cola No
Short-format
Data need to be arranged in an unreplicated complete block design. That is, for each experimental unit
(Student in this case) there needs to be one and only one response per question or time.
The package RVAideMemoire has a function cochran.qtest which will conduct Cochran’s Q test on long-
format data. The function symmetry_test in the coin package will also conduct the test using
permutation, which a type of resampling technique.
A word of caution about the symmetry_test function: be sure not to use it in cases where the response
variable is multinomial; that is, where there are more than two levels in the response. The function
will return a result, but it is not the same kind of symmetry result as the McNemar–Bowker test. I
suspect that for a multinomial response variable, coin treats it like an ordered factor, not like a
nominal variable.
When there are two questions or times, Cochran’s Q test is equivalent to the McNemar test.
Post-hoc analysis can be conducted with pairwise McNemar or exact tests on 2 x 2 tables. The function
pairwiseMcnemar in the rcompanion package will conduct pairwise McNemar, exact, or permutation
tests.
Appropriate data
• The response variable is a dichotomous nominal variable.
• The responses are paired by experimental unit.
• The responses are measured across two or more times or factors.
• The data follow an unreplicated complete block design, with each experimental unit treated
as the block.
Hypotheses
• Null hypothesis: The marginal probability of a [positive] response is unchanged across the
times or factors. That is, a positive response is equally likely across times or factors.
• Alternative hypothesis: Positive responses are not equally likely across times or factors.
425
COCHRAN’S Q TEST FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Interpretation
Significant results can be reported as “There was a significant difference in the proportion of
positive responses across times or factors.” Or, “A positive response for X was more likely
than a positive response for Y.”
Post-hoc analysis
Post hoc analysis can be conducted by conducting tests for the component 2 x 2 tables. A
correction for multiple tests should be applied.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(RVAideMemoire)){install.packages("RVAideMemoire")}
if(!require(coin)){install.packages("coin")}
if(!require(reshape2)){install.packages("reshape2")}
if(!require(rcompanion)){install.packages("rcompanion")}
Hayley Smith teaches an extension course on environmentally-friendly lawn care. After the course,
she surveys her students about their willingness to adopt good practices: Soil testing, measuring the
amount of irrigation, raising the mowing height, and returning the grass clippings to the lawn. She
wants to know which practice students were more willing to adopt.
Input = ("
Practice Student Response
SoilTest a Yes
SoilTest b No
SoilTest c Yes
SoilTest d Yes
SoilTest e No
SoilTest f Yes
SoilTest g Yes
SoilTest h Yes
SoilTest i No
SoilTest j Yes
SoilTest k Yes
SoilTest l Yes
SoilTest m Yes
426
COCHRAN’S Q TEST FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
SoilTest n Yes
Irrigation a Yes
Irrigation b No
Irrigation c No
Irrigation d No
Irrigation e No
Irrigation f No
Irrigation g No
Irrigation h No
Irrigation i Yes
Irrigation j No
Irrigation k No
Irrigation l No
Irrigation m No
Irrigation n No
MowHeight a Yes
MowHeight b No
MowHeight c Yes
MowHeight d Yes
MowHeight e Yes
MowHeight f Yes
MowHeight g Yes
MowHeight h Yes
MowHeight i No
MowHeight j Yes
MowHeight k Yes
MowHeight l Yes
MowHeight m Yes
MowHeight n Yes
Clippings a Yes
Clippings b No
Clippings c No
Clippings d Yes
Clippings e Yes
Clippings f No
Clippings g No
Clippings h Yes
Clippings i Yes
Clippings j Yes
Clippings k Yes
Clippings l No
Clippings m Yes
Clippings n No
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Practice = factor(Data$Practice,
levels=unique(Data$Practice))
Data$Response = factor(Data$Response,
levels=c("Yes", "No"))
427
COCHRAN’S Q TEST FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Table
Practice
Student SoilTest Irrigation MowHeight Clippings
a 0 0 0 0
b 1 1 1 1
c 0 1 0 1
d 0 1 0 0
e 1 1 0 0
f 0 1 0 1
g 0 1 0 1
h 0 1 0 0
i 1 0 1 0
j 0 1 0 0
k 0 1 0 0
l 0 1 0 1
m 0 1 0 0
n 0 1 0 1
Response
Practice Yes No
SoilTest 11 3
Irrigation 2 12
428
COCHRAN’S Q TEST FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
MowHeight 12 2
Clippings 8 6
Table
barplot(Table,
beside = TRUE,
legend = TRUE,
ylim = c(0, 12), ### y-axis: used to prevent legend overlapping bars
cex.names = 0.8, ### Text size for bars
cex.axis = 0.8, ### Text size for axis
args.legend = list(x = "topright", ### Legend location
cex = 0.8, ### Legend text size
bty = "n")) ### Remove legend box
Cochran’s Q test
library(RVAideMemoire)
Cochran's Q test
### Note that the function also gives you proportion of responses for the
### positive response, which is always the second of the nominal response,
### which in this case is "no", or the response coded as "1".
429
COCHRAN’S Q TEST FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
### I wouldn’t use the post-hoc analysis included with the output of the
### function in RVAideMemoire
library(coin)
Data$Practice = factor(Data$Practice,
levels = c("MowHeight", "SoilTest",
"Clippings", "Irrigation"))
library(rcompanion)
PT = pairwiseMcnemar(x = Data$Response,
g = Data$Practice,
block = Data$Student,
test = "permutation",
method = "fdr",
digits = 3)
PT
$Pairwise
Comparison p.value p.adjust
1 MowHeight - SoilTest = 0 0.317 0.3170
2 MowHeight - Clippings = 0 0.102 0.1530
3 MowHeight - Irrigation = 0 0.00389 0.0200
4 SoilTest - Clippings = 0 0.257 0.3080
5 SoilTest - Irrigation = 0 0.00666 0.0200
6 Clippings - Irrigation = 0 0.0143 0.0286
430
COCHRAN’S Q TEST FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
PT = PT$Pairwise
library(rcompanion)
cldList(comparison = PT$Comparison,
p.value = PT$p.adjust,
threshold = 0.05)
Table of results
PT = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
PT
diag(PT) = 1
PT1 = t(PT)
PT[lower.tri(PT)] = PT1[lower.tri(PT1)]
PT
431
COCHRAN’S Q TEST FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(multcompView)
multcompLetters(PT,
compare="<",
threshold=0.05, ### p-value to use as significance threshold
Letters=letters,
reversed = FALSE)
Matrix = as.matrix(read.table(textConnection(Input),
header=TRUE,
row.names=1))
### Add names to matrix dimensions that will be names of variables in long-format
names(dimnames(Matrix))[1] = "Student"
names(dimnames(Matrix))[2] = "Practice"
Matrix
library(reshape2)
Data = melt(Matrix)
432
COCHRAN’S Q TEST FOR PAIRED NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(psych)
headTail(Data)
Cochran’s Q test
library(RVAideMemoire)
Cochran's Q test
library(coin)
Data$Practice = factor(Data$Practice,
levels = c("MowHeight", "SoilTest",
"Clippings", "Irrigation"))
433
ASSOCIATION TESTS FOR ORDINAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(rcompanion)
pairwiseMcnemar(x = Data$value,
g = Data$Practice,
block = Data$Student,
test = "permutation",
method = "fdr",
digits = 3)
$Pairwise
Comparison p.value p.adjust
1 MowHeight - SoilTest = 0 0.317 0.3170
2 MowHeight - Clippings = 0 0.102 0.1530
3 MowHeight - Irrigation = 0 0.00389 0.0200
4 SoilTest - Clippings = 0 0.257 0.3080
5 SoilTest - Irrigation = 0 0.00666 0.0200
6 Clippings - Irrigation = 0 0.0143 0.0286
In Agresti, the method used is called the linear-by-linear association model. In R, the test can be
performed by permutation test with the coin package.
An association test can also be performed on a contingency table with one ordered nominal variable
and one non-ordered nominal variable. The Cochran–Armitage test is a special case of this when the
non-ordered variable has only two variables.
Most of the examples in this chapter use two-dimensional tables, although the coin package can handle
three-dimensional tables. For three-dimensional analyses, it may be easier to use data in the long
format, as is shown in the final example in this chapter.
The following commands will install these packages if they are not already installed:
434
ASSOCIATION TESTS FOR ORDINAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
if(!require(coin)){install.packages("coin")}
if(!require(rcompanion)){install.packages("rcompanion")}
The lbl_test function in the coin package with automatically treat the variables as ordered, with the
levels in the table ordered from smallest to largest. By default, the levels are equally spaced, but the
scores option can be used to specify the distance between the levels of each variable.
The null hypothesis for the linear-by-linear test is that there is no association among the variables in
the table. A significant p-value suggests that there is an association. This is similar to a chi-square test,
except that the categories are ordered in nature.
Note the placement of the initial quote mark in the Input function.
Input =(
"Adopt Always Sometimes Never
Size
Hobbiest 0 1 5
Mom-and-pop 2 3 4
Small 4 4 4
Medium 3 2 0
Large 2 0 0
")
Tabla = as.table(read.ftable(textConnection(Input)))
Tabla
sum (Tabla)
prop.table(Tabla,
margin = NULL) ### proportion in the table
library(coin)
spineplot(Tabla)
435
ASSOCIATION TESTS FOR ORDINAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Spine plot for each Size showing the proportion of Always (dark gray), Sometimes
(medium gray), and Never (light gray).
library(coin)
LxL = lbl_test(Tabla)
LxL
data: Adopt (ordered) by Size (Hobbiest < Mom-and-pop < Small < Medium < Large)
statistic(LxL)^2
11.07262
ChiSq
436
ASSOCIATION TESTS FOR ORDINAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Tabla = as.table(read.ftable(textConnection(Input)))
Tabla
sum (Tabla)
prop.table(Tabla,
margin = NULL) ### proportion in the table
Tired
Happy 1 2 3 5
1 0.00 0.00 0.00 0.15
2 0.00 0.00 0.00 0.10
3 0.00 0.00 0.15 0.00
4 0.10 0.00 0.00 0.00
5 0.15 0.10 0.00 0.25
library(coin)
LxL = lbl_test(Tabla)
LxL
437
ASSOCIATION TESTS FOR ORDINAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The chisq_test function in the coin package can be used to conduct a test of association for a
contingency table with one ordered nominal variable and one non-ordered nominal variable. The
Cochran–Armitage test is a special case of this where the non-ordered variable has only two
categories.
The scores option is used to indicate which variable should be treated as ordered, and the spacing of
the levels of this variable.
Note the placement of the initial quote mark in the Input function.
Input =(
"Breakfast Never Rarely Sometimes Often Always
Travel
Walk 6 9 6 5 2
Bus 2 5 8 5 3
Drive 2 4 6 8 8
")
Tabla = as.table(read.ftable(textConnection(Input)))
Tabla
sum (Tabla)
prop.table(Tabla,
margin = 1) ### proportion in each row
Breakfast
Travel Never Rarely Sometimes Often Always
Walk 0.21428571 0.32142857 0.21428571 0.17857143 0.07142857
Bus 0.08695652 0.21739130 0.34782609 0.21739130 0.13043478
Drive 0.07142857 0.14285714 0.21428571 0.28571429 0.28571429
library(coin)
spineplot(Tabla)
438
ASSOCIATION TESTS FOR ORDINAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(coin)
Test = chisq_test(Tabla,
scores = list("Breakfast" = c(-2, -1, 0, 1, 2)))
Test
statistic(Test)^2
[1] 75.23709
Post-hoc analysis
For a contingency table with one ordered variable and one non-ordered variable, it makes sense to
analyze the component tables with pairwise comparisons of the levels of the non-ordered variable.
Results of the compact letter display will be easier to interpret if the table is ordered so that the first
row, in this case, ranks highest or lowest in the ordered variable, and so on.
439
ASSOCIATION TESTS FOR ORDINAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(rcompanion)
PT = pairwiseOrdinalIndependence(Tabla,
compare="row")
PT
library(rcompanion)
cldList(comparison = PT$Comparison,
p.value = PT$p.value,
threshold = 0.05)
Note the placement of the initial quote mark in the Input function.
Input =(
"Likert 1 2 3 4 5
Speaker
Pooh 0 0 1 6 3
Piglet 1 6 2 1 0
Tigger 0 0 2 6 2
")
Tabla = as.table(read.ftable(textConnection(Input)))
Tabla
sum(Tabla)
prop.table(Tabla,
margin = 1) ### proportion in each row
Likert
Speaker 1 2 3 4 5
Pooh 0.0 0.0 0.1 0.6 0.3
Piglet 0.1 0.6 0.2 0.1 0.0
440
ASSOCIATION TESTS FOR ORDINAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(coin)
Test = chisq_test(Tabla,
scores = list("Likert" = c(1, 2, 3, 4, 5)))
Test
library(rcompanion)
PT = pairwiseOrdinalIndependence(Tabla,
compare="row")
PT
library(rcompanion)
cldList(comparison = PT$Comparison,
p.value = PT$p.value,
threshold = 0.05)
The tests in this chapter can also be performed on data in long format rather than in table format.
Each row of data can represent a single observation, or one variable can hold counts of each category,
as the Count variable does below.
For the independence_test function in the coin package to handle ordered and non-ordered variables
properly, the user need only specify which variables are to be considered ordered.
The independence_test function can also handle a stratification variable. That is, a two-dimensional
table within each level of the stratification variable.
441
ASSOCIATION TESTS FOR ORDINAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Input =("
Crop Size Adopt Count
Nursery Hobbiest Always 0
Nursery Hobbiest Sometimes 1
Nursery Hobbiest Never 3
Nursery Mom-and-pop Always 1
Nursery Mom-and-pop Sometimes 1
Nursery Mom-and-pop Never 2
Nursery Small Always 2
Nursery Small Sometimes 3
Nursery Small Never 2
Nursery Medium Always 2
Nursery Medium Sometimes 1
Nursery Medium Never 0
Nursery Large Always 1
Nursery Large Sometimes 0
Nursery Large Never 0
Vegetable Hobbiest Always 0
Vegetable Hobbiest Sometimes 0
Vegetable Hobbiest Never 2
Vegetable Mom-and-pop Always 1
Vegetable Mom-and-pop Sometimes 2
Vegetable Mom-and-pop Never 2
Vegetable Small Always 2
Vegetable Small Sometimes 1
Vegetable Small Never 2
Vegetable Medium Always 1
Vegetable Medium Sometimes 1
Vegetable Medium Never 0
Vegetable Large Always 1
Vegetable Large Sometimes 0
Vegetable Large Never 0
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Size = factor(Data$Size,
ordered = TRUE,
levels=unique(Data$Size))
Data$Adopt = factor(Data$Adopt,
ordered = TRUE,
levels=unique(Data$Adopt))
### Make sure the ordered levels are in the proper order
str(Data$Adopt)
442
ASSOCIATION TESTS FOR ORDINAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
levels(Data$Adopt)
str(Data$Size)
levels(Data$Size)
XT
Adopt
Size Always Sometimes Never
Hobbiest 0 1 5
Mom-and-pop 2 3 4
Small 4 4 4
Medium 3 2 0
Large 2 0 0
spineplot(XT)
library(coin)
independence_test(Adopt ~ Size,
data = Data,
weights = ~ Count)
data: Adopt (ordered) by Size (Hobbiest < Mom-and-pop < Small < Medium < Large)
443
ASSOCIATION TESTS FOR ORDINAL TABLES SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Nursery Hobbiest 0 1 3
Mom-and-pop 1 1 2
Small 2 3 2
Medium 2 1 0
Large 1 0 0
Vegetable Hobbiest 0 0 2
Mom-and-pop 1 2 2
Small 2 1 2
Medium 1 1 0
Large 1 0 0
library(coin)
data: Adopt (ordered) by Size (Hobbiest < Mom-and-pop < Small < Medium < Large)
stratified by Crop
XT1
spineplot(XT1,
main = "Nursery")
XT2
spineplot(XT2,
main = "Vegetable")
References
444
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The following commands will install these packages if they are not already installed:
if(!require(car)){install.packages("car")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(VGAM)){install.packages("VGAM")}
if(!require(lmtest)){install.packages("lmtest")}
if(!require(psych)){install.packages("psych")}
if(!require(lme4)){install.packages("lme4")}
if(!require(MASS)){install.packages("MASS")}
if(!require(rcompanion)){install.packages("rcompanion")}
Analysis of contingency tables with two or more dimensions can be accomplished with log-linear
models. These will test for association. When there are multiple dimensions, this approach is
sometimes called multiway frequency analysis. It is analogous to chi-square tests for two-way tables or
Cochran–Mantel–Haenszel test for three way tables.
One advantage of log-linear models is that they handle tables with a higher number of dimensions.
Another advantage is that the models can be specified to test for more complicated patterns of
association. When each term in the model is included, and there are no interactions, the model tests
for mutual independence of the terms. Models can also test for conditional independence and partial
independence.
For more information on specifying models for conditional independence or partial independence, see:
445
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For a more complete discussion of log-linear models for multiway frequency analysis see:
Tabachnick, B.G. and S.F. Fidell. 2001. Using multivariate statistics. 4th Edition. Allyn and Bacon,
Boston, MA.
Zero frequencies in cells may cause the maximum likelihood estimation to fail, or may cause bias in the
results.
Structural zeros can be handled with the start argument in the loglm function. See library(MASS);
?loglm , library(MASS); ?loglm1 , and ?loglin .
Data = read.table(textConnection(Input),header=TRUE)
Data$County = factor(Data$County,
levels=unique(Data$County))
Data$Sex = factor(Data$Sex,
levels=unique(Data$Sex))
Data$Result = factor(Data$Result,
levels=unique(Data$Result))
446
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Create table
Table = xtabs(Count ~ Sex + Result + County,
data=Data)
Log-linear model
The model here tests for mutual independence of the three variables.
library(MASS)
Statistics:
X^2 df P(> X^2)
Likelihood Ratio 20.96662 10 0.0213275
Pearson 20.79050 10 0.0226027
Post-hoc analysis
For a post-hoc analysis, you could slice up a multi-dimensional table in any way that makes sense for
the hypotheses you want to test. Here, we’ll look for an association of Sex and Result within each
county.
Third dimension in our table is County, so Table[,,1] yields the piece of the table where the third
dimension is equal to 1, that is, where County = Bloom. And so on.
447
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Bloom = Table[,,1]
Cobblestone = Table[,,2]
Dougal = Table[,,3]
Heimlich = Table[,,4]
Logistic regression
448
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Logistic regression is a common option for building models with a nominal dependent variable. For
standard logistic regression, the dependent variable must have only two levels. That is, it must be
dichotomous.
“Simple Logistic Regression” in Mangiafico, S.S. 2015. An R Companion for the Handbook of
Biological Statistics, version 1.09. rcompanion.org/rcompanion/e_06.html.
“Multiple Logistic Regression” in Mangiafico, S.S. 2015. An R Companion for the Handbook of
Biological Statistics, version 1.09. rcompanion.org/rcompanion/e_07.html.
The following example revisits Alexander Anderson pesticide safety training course data across sex
and four counties, for which we had used the Cochran–Mantel–Haenszel Test.
Input = ("
County Sex Result Count
Bloom Female Pass 9
Bloom Female Fail 5
Bloom Male Pass 7
Bloom Male Fail 17
Cobblestone Female Pass 11
Cobblestone Female Fail 4
Cobblestone Male Pass 9
Cobblestone Male Fail 21
Dougal Female Pass 9
Dougal Female Fail 7
Dougal Male Pass 19
Dougal Male Fail 9
Heimlich Female Pass 15
Heimlich Female Fail 8
Heimlich Male Pass 14
Heimlich Male Fail 17
")
Data = read.table(textConnection(Input),header=TRUE)
449
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data$County = factor(Data$County,
levels=unique(Data$County))
Data$Sex = factor(Data$Sex,
levels=unique(Data$Sex))
Data$Result = factor(Data$Result,
levels=unique(Data$Result))
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Logistic regression
Logistic regression can be conducted with the glm function in the native stats package. The summary
function will return coefficients for the model. A p-value and a pseudo R-squared value for the model
can be produced with the nagelkerke function. The Anova function in the car package will produce an
analysis of deviance table with either likelihood ratio, Wald, or F tests.
model = glm(Result ~ County + Sex + County:Sex,
weight = Count,
data = Data,
family = binomial(link="logit")
)
summary(model)
library(rcompanion)
nagelkerke(model)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.0797857
Cox and Snell (ML) 0.7136520
Nagelkerke (Cragg and Uhler) 0.7136520
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
450
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(car)
Anova(model,
type="II",
test="LR")
LR Chisq Df Pr(>Chisq)
County 4.9627 3 0.174548
Sex 7.8108 1 0.005193 **
County:Sex 7.2169 3 0.065296 .
Post-hoc analysis
Post-hoc analysis can be conducted with the lsmeans package. Note that estimates for lsmeans,
differences, or standard errors are on a log or logit scale. For notes on using lsmeans with different
model types, see ?lsmeans::models.
For this example, no adjustment in p-values was made for multiple comparisons. Typically, an
adjustment such as Tukey would be applied. See ?lsmeans::summary for p-value adjustment options in
lsmeans. Note that the adjustment should be applied for each output; that is, for each of lsmeans and
cld.
Also note that normally, because the County x Sex interaction was not significant, we wouldn’t want to
explore the mean separations of the County x Sex interaction.
The results are truncated to emphasize the differences between sexes within counties.
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ County:Sex,
adjust="none") ### No adjustment for multiple comparisons
leastsquare
$contrasts
contrast estimate SE df z.ratio p.value
451
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
cld(leastsquare,
alpha=0.05,
Letters=letters, ### Use lower-case letters for .group
adjust="none") ### No adjustment for multiple comparisons
p.adj = p.adjust(p.value,
method = "fdr")
p.adj = signif(p.adj,
2)
p.adj
452
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
If the nominal dependent variable has more than two levels, multinomial logistic regression can be
used.
VGAM package
The VGAM package provides a flexible framework for building models with categorical data. The
vignette for the package provides an overview of the package, as well as brief overview of categorical
analysis in R.
Yee, T.W. The VGAM package for Categorical Data Analysis. The Comprehensive R Archive
Network. cran.r-project.org/web/packages/VGAM/vignettes/categoricalVGAM.pdf.
The following article may also be helpful for those interested in using the VGAM package.
Yee, T.W. 2008. The VGAM Package. R News, 8(2), 28–39. URL. cran.r-
project.org/doc/Rnews/Rnews_2008-2.pdf.
The VGAM package can be explored through the help files for the package.
if(!require(VGAM)){install.packages("VGAM")}
help(package="VGAM")
?multinomial
Two other packages that can perform multinomial regression are mlogit and nnet. The packages can
be explored with the following vignette, article, and the package help files.
[IDRE] Institute for Digital Research and Education. 2015. “R Data Analysis Examples:
Multinomial Logistic Regression”. UCLA. www.ats.ucla.edu/stat/r/dae/mlogit.htm.
if(!require(nnet)){install.packages("nnet")}
help(package="nnet")
453
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Because the dependent variable, Result, has only two levels, it could be modeled with standard
binomial regression. However, a multinomial approach with VGAM will be used here as an example.
Note that in multinomial regression, a reference level for the dependent variable must be specified.
Coefficients of the model will change if a different reference level is specified.
Note that model assumptions and pitfalls of this approach are not discussed here. The reader is urged
to understand the assumptions of this kind of modeling before proceeding.
The summary function will provide estimates of coefficients and standard errors for the coefficients. A
p-value for the overall model as well as pseudo R-squared value is provided by the nagelkerke function.
The lrtest function in VGAM can be used to compare two nested models with likelihood ratio test. If
only one model is given, it provides a p-value for the overall model, compared with a null model. The
Anova function in the car package will provide chi-square tests for individual factors in the model. The
user might prefer to use likelihood ratio tests with nested models instead of chi-square tests. This
could be accomplished with the lrtest function in the lmtest package, or by specifying the second model
with the null= option in the nagelkerke function.
Input = ("
County Sex Result Count
Bloom Female Pass 9
Bloom Female Fail 5
Bloom Male Pass 7
Bloom Male Fail 17
Cobblestone Female Pass 11
Cobblestone Female Fail 4
Cobblestone Male Pass 9
Cobblestone Male Fail 21
Dougal Female Pass 9
Dougal Female Fail 7
Dougal Male Pass 19
Dougal Male Fail 9
Heimlich Female Pass 15
Heimlich Female Fail 8
Heimlich Male Pass 14
Heimlich Male Fail 17
")
Data = read.table(textConnection(Input),header=TRUE)
Data$County = factor(Data$County,
levels=unique(Data$County))
454
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data$Sex = factor(Data$Sex,
levels=unique(Data$Sex))
Data$Result = factor(Data$Result,
levels=unique(Data$Result))
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Multinomial regression
library(VGAM)
summary(model)
library(car)
Anova(model,
type="II",
test="Chisq")
Response: Result
Df Chisq Pr(>Chisq)
Sex 1 6.7132 0.00957 **
County 3 4.1947 0.24120
Sex:County 3 7.1376 0.06764 .
Library(rcompanion)
nagelkerke(model)
455
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.0797857
Cox and Snell (ML) 0.7136520
Nagelkerke (Cragg and Uhler) 0.7136520
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
7 -10.004 20.009 0.0055508
library(lmtest)
lrtest(model)
Post-hoc analysis
At the time of writing, the lsmeans package cannot be used with vglm models.
One option for post-hoc analysis would be to conduct analyses on reduced models, including only two
levels of a factor. For example, if the variable County x Sex term had been significant, the following
code could be used to create a reduced dataset with only Bloom–Female and Bloom–Male, and analyze
this data with vglm.
Data.b = Data[Data$County=="Bloom" &
(Data$Sex=="Female"| Data$Sex=="Male") , ]
Data.b$County = factor(Data.b$County)
Data.b$Sex = factor(Data.b$Sex)
summary(Data.b)
library(VGAM)
456
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
lrtest(model.b)
p.adj = p.adjust(p.value,
method = "fdr")
p.adj = signif(p.adj,
2)
p.adj
A mixed-effects generalized linear model, as in the case of logistic regression with random effects, can
be specified. This example revisits Hayley Smith’s friendly lawn care course, for which we had used
Cochran’s Q test.
This example uses the glmer function in the package mle4, which can fit binomial dependent variables,
with the binomial family of models, or other families of models. As far as I know, it will not fit
multinomial regression. For more information on families of models, see ?family and ?glm. For more
457
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
information on glmer, see ?glmer. For a list of methods used to extract information from the model
object, see methods(class="merMod").
The Anova function in the car package will provide chi-square tests for individual factors in the model.
The user might prefer to use likelihood ratio tests with nested models instead of chi-square tests. This
could be accomplished with the anova function, or by specifying the second model with the null=
option in the nagelkerke function.
Input = ("
Practice Student Response
SoilTest a Yes
SoilTest b No
SoilTest c Yes
SoilTest d Yes
SoilTest e No
SoilTest f Yes
SoilTest g Yes
SoilTest h Yes
SoilTest i No
SoilTest j Yes
SoilTest k Yes
SoilTest l Yes
SoilTest m Yes
SoilTest n Yes
Irrigation a Yes
Irrigation b No
Irrigation c No
Irrigation d No
Irrigation e No
Irrigation f No
Irrigation g No
Irrigation h No
Irrigation i Yes
Irrigation j No
Irrigation k No
Irrigation l No
Irrigation m No
Irrigation n No
MowHeight a Yes
MowHeight b No
MowHeight c Yes
MowHeight d Yes
MowHeight e Yes
MowHeight f Yes
MowHeight g Yes
MowHeight h Yes
MowHeight i No
MowHeight j Yes
MowHeight k Yes
MowHeight l Yes
MowHeight m Yes
MowHeight n Yes
Clippings a Yes
Clippings b No
458
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Clippings c No
Clippings d Yes
Clippings e Yes
Clippings f No
Clippings g No
Clippings h Yes
Clippings i Yes
Clippings j Yes
Clippings k Yes
Clippings l No
Clippings m Yes
Clippings n No
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Practice = factor(Data$Practice,
levels=unique(Data$Practice))
Data$Response = factor(Data$Response,
levels=c("Yes", "No"))
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
summary(model)
library(car)
459
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Anova(model,
type="II",
test="Chisq")
Chisq Df Pr(>Chisq)
Practice 10.713 3 0.01338 *
model.null = glm(Response ~ 1,
data = Data,
family = binomial)
library(rcompanion)
nagelkerke(model,
model.null)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.258315
Cox and Snell (ML) 0.295184
Nagelkerke (Cragg and Uhler) 0.397900
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-4 -9.7949 19.59 0.00060164
anova(model, model.fixed)
Post-hoc analysis
Post-hoc analysis can be conducted with the lsmeans package. Note that estimates for lsmeans,
differences, or standard errors are on a log or logit scale. For notes on using lsmeans with different
model types, see ?lsmeans::models.
460
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Practice,
adjust="tukey")
### Tukey adjustment for multiple comparisons
leastsquare
$contrasts
contrast estimate SE df z.ratio p.value
SoilTest - Irrigation -3.4004979 1.1888138 NA -2.860 0.0220
SoilTest - MowHeight 0.5256967 1.0384143 NA 0.506 0.9576
SoilTest - Clippings -1.1165227 0.9087443 NA -1.229 0.6086
Irrigation - MowHeight 3.9261946 1.2853129 NA 3.055 0.0121
Irrigation - Clippings 2.2839752 1.0431115 NA 2.190 0.1261
MowHeight - Clippings -1.6422194 1.0071891 NA -1.630 0.3614
cld(leastsquare,
alpha=0.05,
Letters=letters, ### Use lower-case letters for .group
adjust="tukey") ### Tukey adjustment for multiple comparisons
The vcd package has several function that are useful when working with categorical data. The package
can be explored with the following vignette and the package help files.
Friendly, M. Working with categorical data with R and the vcd and vcdExtra packages. The
Comprehensive R Archive Network. cran.r-
project.org/web/packages/vcdExtra/vignettes/vcd-tutorial.pdf.
461
MODELS FOR NOMINAL DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
if(!require(vcd)){install.packages("vcd")}
help(package="vcd")
462
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Parametric Tests
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(rcompanion)){install.packages("rcompanion")}
Parametric statistical tests are among the most common you’ll encounter. They include t-test, analysis
of variance, and linear regression.
They are used when the dependent variable is a measured interval/ratio data variable. This might
include variables measured in science such as fish length, child height, crop yield weight, or pollutant
concentration in water.
One advantage of using parametric statistical tests is that your audience will likely be familiar with the
techniques and interpretation of the results. These tests are also often more flexible and more
powerful than their nonparametric analogues.
Their major drawback is that all parametric tests assume something about the distribution of the
underlying data. If these assumptions are violated, the resultant test statistics will not be valid, and
the tests will not be as powerful as for cases when assumptions are met.
Commonly this is the case when subjects are being counted. An example of this might be counting the
number of students who pass or fail a test given one of two instruction methods.
463
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Permissible examples might include test scores, age, or number of steps taken during the day.
Technically, each of these measurements is bound by zero, and are discrete rather than continuous
measurements. However, if other conditions are met, it is reasonable to handle them as if they were
continuous measurement variables.
This kind of count data will sometimes need to be transformed to meet the assumptions of parametric
analysis. Square root and logarithmic transformations are common. However, if there are many
counts at or near zero, transformation is unlikely to help. With the use of computers, we have
appropriate methods for count data available to us. It is therefore usually not worth the effort to
attempt to force count data to meet the assumptions of parametric analysis with transformations.
All parametric analyses have assumptions about the underlying data, and these assumptions should
always be confirmed when using these tests. If these assumptions are violated, the resulting statistics
and conclusions will not be valid, and the tests may lack power relative to alternative tests.
The exact assumptions vary among tests, but a general discussion follows.
464
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
Data
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Random sampling
All statistical tests assume that the data captured in the sample are randomly chosen from the
population as a whole. Selection bias will obviously affect the validity of the outcome of the analysis.
Independent observations
Tests will also assume that observations are independent of one another, except when the analysis
takes non-independence into account. One common case of non-independent observations is in
repeated measures experiments, in which the same subject is observed over time. If you were
measuring test scores of students over time, you might expect students with a high test score on one
date to have a high test score on subsequent dates. In this case the observation on one date would not
be independent of observations on other dates.
The independence of observation is often assumed from good experimental design. Also, data or
residuals can be plotted, for example to see if observations from one date are correlated to those for
another date.
A select number of tests will require that data itself be normally distributed. This will be limited to
one-sample t-test, two-sample t-test, and paired t-test. For other tests, the distribution of the residuals
will be investigated.
465
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Residuals from an analysis are also commonly called errors. They are the difference between the
observations and the value predicted by the model. For example, if the calculated mean of a sample is
10, and one observation is 12, the residual for this observation is 2. If another observation is 7, the
residual for this observation is –3.
The parametric tests discussed here require that the residuals be normally distributed. This will
usually be assessed with a histogram of residuals, a density plot as shown below, or with quantile–
quantile plot.
Be careful not to get confused about this assumption. You may see discussion about how “data” should
be normally distributed for parametric tests. This is usually wrong-headed. The t-test assumes that
the observations for each group are normally distributed, but if there is a difference in the groups, we
might expect a bi-modal distribution, not a simple normal distribution, for the combined data. This is
why in most cases we look at the distribution of the residuals, not the raw data.
ggplot(Data,
aes(Steps, fill = Sex)) +
geom_density(position="dodge",
alpha = 0.6)
The following code creates a variable Mean for Steps for each Sex in the Catbus data set, and then
subtracts Steps from each mean, calling the result Residual. This is for illustrative purposes only; you
won’t normally need to manipulate data in this manner.
M1 = mean(Data$Steps[Data$Sex=="female"])
M2 = mean(Data$Steps[Data$Sex=="male"])
Data$Mean[Data$Sex=="female"] = M1
466
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data$Mean[Data$Sex=="male"] = M2
### Density plot of residuals from mean for each sex for Catbus data set
ggplot(Data,
aes(Residual)) +
geom_density(fill = "gray")
Homogeneity of variance
Parametric analyses will also assume a homogeneity of variance among groups. That is, for a t-test
comparing two groups, each group should have the same variance.
A good approach to assess this assumption is to plot residuals vs. predicted values. The residuals
should have approximately the same spread across all predicted values.
### Residuals from mean for each sex vs. mean for Catbus data set
plot(jitter(Residual) ~ Mean,
data = Data)
467
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
A violation of this assumption is sometimes indicated when a plot of residuals versus predicted values
exhibits a curved pattern.
Outliers
Outliers are observations whose value is far outside what is expected. They can play havoc with
parametric analyses since they affect the distribution of the data and strongly influence the mean.
There are a variety of formal tests for detecting outliers, but they will not be discussed here. The best
approach is one that looks at residuals after an analysis. Good tools are the “Residuals vs. leverage”
plot and other plots in the “Other diagnostic plots” section below.
It’s my opinion that outliers should not be removed from data unless there is a good reason, usually
when a value is impossible or a measurement error of some kind is suspected.
The upshot is that model assumptions should always be checked, but you may be able to tolerate small
violations in the distribution of residuals or homoscedasticity. Large violations will make the test
invalid, though. It is important to be honest with your assessments when checking model
assumptions. It is better to transform data, change your model, use a robust method, or use a
nonparametric test than to not have confidence in your analysis.
468
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For this example, we’ll revisit the Catbus data. We’ll then define a linear model where Steps is the
dependent variable and Sex and Teacher are the independent variables.
Input = ("
Student Sex Teacher Steps Rating
a female Catbus 8000 7
b female Catbus 9000 10
c female Catbus 10000 9
d female Catbus 7000 5
e female Catbus 6000 4
f female Catbus 8000 8
g male Catbus 7000 6
h male Catbus 5000 5
i male Catbus 9000 10
j male Catbus 7000 8
k female Satsuki 8000 7
l female Satsuki 9000 8
m female Satsuki 9000 8
n female Satsuki 8000 9
o male Satsuki 6000 5
p male Satsuki 8000 9
q male Satsuki 7000 6
r female Totoro 10000 10
s female Totoro 9000 10
t female Totoro 8000 8
u female Totoro 8000 7
v female Totoro 6000 7
w male Totoro 6000 8
x male Totoro 8000 10
y male Totoro 7000 7
z male Totoro 7000 7
")
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
469
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
In general, I don’t recommend using these tests because their results are dependent on sample size.
When the sample size is large, the tests may indicate a statistically significant departure from
normality, even if that departure is small. And when sample sizes are small, they won’t detect
departures from normality.
The article from the Fells Stats blog, in the “References” section has pretty convincing examples of
these problems.
In general, I would not recommend relying on skew and kurtosis calculations, but instead use
histograms and other plots. In the chapter on Normality in the “Optional readings” section, John
McDonald recommends not calculating skew or kurtosis at all.
If I were forced to give advice for skewness calculations, I might say, be cautious if the absolute value is
> 0.5, and consider it not normally distributed if the absolute value is > 1.0. Some authors use 2.0 as a
cutoff for normality, and others use a higher limit for kurtosis.
x = residuals(model)
library(psych)
describe(x,
type=2) # Type of skew and kurtosis
vars n mean sd median trimmed mad min max range skew kurtosis
1 1 26 0 1132.26 -39.58 11.07 1114.18 -2202.4 2123.25 4325.65 -0.13 -0.11
470
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(rcompanion)
plotNormalHistogram(x)
The plotNormalDensity function will produce this plot. Options include those for the plot function, as
well as adjust, bw, and kernel which are passed to the density function. col1, col2, and col3 change plot
colors, and lwd changes line thickness.
x = residuals(model)
library(rcompanion)
plotNormalDensity(x,
adjust = 1) ### Decrease this number
### to make line less smooth
471
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
472
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The following plots are histograms of the same residuals shown in the previous plots. A) Residuals.a
are reasonably-close to normally distributed. B) Residuals.b are highly skewed (left, or negative). C)
Residuals.c are moderately negatively skewed. This distribution would probably not cause too much
havoc with most parametric tests, but, depending on the circumstances, I would probably try to
transform a variable or find a better-fitting model. D) Residuals.d are symmetric, but leptokurtic. I
probably wouldn’t be too concerned with the distribution.
Results from the describe function in the psych package are shown below the histograms.
473
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
### describe(Residuals.a)
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 200 0 6.82 0.63 0.07 6.91 -20 18.51 38.51 -0.14 0.03 0.48
### describe(Residuals.b)
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 200 0 3.77 1.38 0.86 1.62 -20 2.62 22.62 -2.53 7.2 0.27
### describe(Residuals.c)
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 200 0 6.79 1.25 0.48 7.75 -20 12.88 32.88 -0.54 -0.42 0.48
### describe(Residuals.d)
vars n mean sd median trimmed mad min max range skew kurtosis se
1 1 200 0 5.53 0.11 0.21 3.83 -20 16.57 36.57 -0.48 1.94 0.39
While these are useful diagnostic plots, their interpretation will not be discussed here.
plot(model)
474
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
475
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
476
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Descriptive statistics for interval/ratio data are discussed in the “Descriptive statistics for
interval/ratio data” section in the Descriptive Statistics chapter.
Descriptive plots for interval/ratio data are discussed in the “Examples of basic plots for interval/ratio
and ordinal data” section in the Basic Plots chapter.
Optional readings
References
“One-way Analysis with Permutation Test” in Mangiafico, S.S. 2015a. An R Companion for the Handbook
of Biological Statistics, version 1.09. rcompanion.org/rcompanion/d_06a.html.
“Two-way Anova with Robust Estimation” in Mangiafico, S.S. 2015b. An R Companion for the Handbook
of Biological Statistics, version 1.09. rcompanion.org/rcompanion/d_08a.html.
Tabachnick, B.G. and S.F. Fidell. 2001. Using multivariate statistics. 4th Edition. Allyn and
Bacon, Boston, MA.
“Normality tests don't do what you think they do.” 2012. Fells Stats. blog.fellstat.com/?p=61.
Code for conducting formal tests of normality are included here. I don’t recommend using them, but
they may be instructive in training the eye to interpret histograms and Q-Q plots, or to respond to a
less-savvy reviewer.
In each case, the null hypothesis is that the data distribution is not different from normal. That is, a
significant p-value (p < 0.05) suggests that data are not normally distributed.
As mentioned previously, a limitation to using these tests is that as the number of observations are
increased, the ability of the test to return a significant p-value increases, even for small deviations
from a normal distribution.
477
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Input = ("
Student Sex Teacher Steps Rating
a female Catbus 8000 7
b female Catbus 9000 10
c female Catbus 10000 9
d female Catbus 7000 5
e female Catbus 6000 4
f female Catbus 8000 8
g male Catbus 7000 6
h male Catbus 5000 5
i male Catbus 9000 10
j male Catbus 7000 8
k female Satsuki 8000 7
l female Satsuki 9000 8
m female Satsuki 9000 8
n female Satsuki 8000 9
o male Satsuki 6000 5
p male Satsuki 8000 9
q male Satsuki 7000 6
r female Totoro 10000 10
s female Totoro 9000 10
t female Totoro 8000 8
u female Totoro 8000 7
v female Totoro 6000 7
w male Totoro 6000 8
x male Totoro 8000 10
y male Totoro 7000 7
z male Totoro 7000 7
")
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
478
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
x = residuals(model)
shapiro.test(x)
if(!require(nortest)){install.packages("nortest")}
library(nortest)
x = residuals(model)
ad.test(x)
x = residuals(model)
ks.test(x,
"pnorm",
mean = mean(x),
sd = sd(x))
if(!require(fBasics)){install.packages("fBasics")}
library(fBasics)
x = residuals(model)
dagoTest(x)
Title:
D'Agostino Normality Test
479
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Test Results:
STATISTIC:
Chi2 | Omnibus: 0.1071
Z3 | Skewness: -0.3132
Z4 | Kurtosis: 0.0948
P VALUE:
Omnibus Test: 0.9479
Skewness Test: 0.7541
Kurtosis Test: 0.9245
Code for formal tests of homogeneity of variance among groups is presented here. I don’t recommend
using them, but instead recommend using diagnostic plots.
In each case, the null hypothesis is that the variance among groups is not different. That is, a
significant p-value (p < 0.05) suggests that the variance among groups is different.
Input = ("
Student Sex Teacher Steps Rating
a female Catbus 8000 7
b female Catbus 9000 10
c female Catbus 10000 9
d female Catbus 7000 5
e female Catbus 6000 4
f female Catbus 8000 8
g male Catbus 7000 6
h male Catbus 5000 5
i male Catbus 9000 10
j male Catbus 7000 8
k female Satsuki 8000 7
l female Satsuki 9000 8
m female Satsuki 9000 8
n female Satsuki 8000 9
o male Satsuki 6000 5
p male Satsuki 8000 9
q male Satsuki 7000 6
r female Totoro 10000 10
s female Totoro 9000 10
t female Totoro 8000 8
u female Totoro 8000 7
v female Totoro 6000 7
w male Totoro 6000 8
x male Totoro 8000 10
y male Totoro 7000 7
z male Totoro 7000 7
")
Data = read.table(textConnection(Input),header=TRUE)
480
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
library(car)
x = residuals(model)
Df F value Pr(>F)
group 5 0.4457 0.8113
481
INTRODUCTION TO PARAMETRIC TESTS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
20
library(car)
x = residuals(model)
Df F value Pr(>F)
group 5 0.4015 0.842
20
if(!require(lawstat)){install.packages("lawstat")}
library(lawstat)
x = residuals(model)
Fligner-Killeen test
The Fligner-Killeen test is another test for homogeneity of variances that is robust to departures in
normality of the data.
x = residuals(model)
482
ONE-SAMPLE T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
One-sample t-test
One-sample tests are not used too often, but are useful to compare a set of values to a given default
value. For example, one might ask if a set of student scores are significantly different from a “default”
or “neutral” score of 75.
Appropriate data
• One-sample data
• Data are interval/ratio, and are continuous
• Data are normally distributed
• Moderate skewness is permissible if the data distribution is unimodal without outliers
Hypotheses
• Null hypothesis: The mean is equal to the default value.
• Alternative hypothesis (two-sided): The mean is not equal to the default value.
Interpretation
Reporting significant results as, e.g., “Mean score for Variable A was significantly different from a
default value of 75” is acceptable.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(rcompanion)){install.packages("rcompanion")}
In the following example, Brendon Small has his SNAP-Ed students keep diaries of what they eat for a
week, and then calculate the daily sodium intake in milligrams. As a first step in the analysis, he wants
to compare mean sodium intake by his students to the American Heart Association recommendation of
1500 mg.
A one-sample t-test can be conducted with the t.test function in the native stats package. Conveniently
the output includes the mean of the sample, a confidence interval for that mean, and a p-value for the t-
test.
483
ONE-SAMPLE T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
We will use a histogram with an imposed normal curve to confirm data are approximately normal.
Input = ("
Instructor Student Sodium
'Brendon Small' a 1200
'Brendon Small' b 1400
'Brendon Small' c 1350
'Brendon Small' d 950
'Brendon Small' e 1400
'Brendon Small' f 1150
'Brendon Small' g 1300
'Brendon Small' h 1325
'Brendon Small' i 1425
'Brendon Small' j 1500
'Brendon Small' k 1250
'Brendon Small' l 1150
'Brendon Small' m 950
'Brendon Small' n 1150
'Brendon Small' o 1600
'Brendon Small' p 1300
'Brendon Small' q 1050
'Brendon Small' r 1300
'Brendon Small' s 1700
'Brendon Small' t 1300
")
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Histogram of data
x = Data$Sodium
library(rcompanion)
484
ONE-SAMPLE T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
plotNormalHistogram(x)
qqnorm(x)
qqline(x, col="red")
One-sample t-test
t.test(Data$Sodium,
mu = 1500,
conf.int = 0.95)
485
ONE-SAMPLE T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
sample estimates:
mean of x
1287.5
Reference’s
“Student’s t–test for One Sample” in Mangiafico, S.S. 2015. An R Companion for the Handbook of
Biological Statistics, version 1.09. rcompanion.org/rcompanion/d_01.html.
Exercises O
Was the mean sodium intake significantly different from the American Heart Association
recommendation or 1500 mg per day?
2. As part of a professional skills program, a 4-H club tests its members for typing proficiency. Dr.
Katz wants to test his students’ mean typing speed against a nominal speed of 40 words per minute.
Instructor Student Words.per.minute
'Dr. Katz Professional Therapist' a 35
'Dr. Katz Professional Therapist' b 50
'Dr. Katz Professional Therapist' c 55
'Dr. Katz Professional Therapist' d 60
'Dr. Katz Professional Therapist' e 65
'Dr. Katz Professional Therapist' f 60
'Dr. Katz Professional Therapist' g 70
'Dr. Katz Professional Therapist' h 55
'Dr. Katz Professional Therapist' i 45
'Dr. Katz Professional Therapist' j 55
'Dr. Katz Professional Therapist' k 60
'Dr. Katz Professional Therapist' l 45
'Dr. Katz Professional Therapist' m 65
'Dr. Katz Professional Therapist' n 55
'Dr. Katz Professional Therapist' o 50
'Dr. Katz Professional Therapist' p 60
486
TWO-SAMPLE T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
Was the mean typing speed significantly different from the nominal rate of 40 words per
minute?
Two-sample t-test
The two-sample unpaired t-test is a commonly used test that compares the means of two samples.
Appropriate data
• Two-sample data. That is, one measurement variable in two groups or samples
• Dependent variable is interval/ratio, and is continuous
• Independent variable is a factor with two levels. That is, two groups
• Data for each population are normally distributed
• For the traditional test, the two samples need to have the same variance. However, Welch’s t-
test, which is used by default in R, does not assume equal variances.
• Observations between groups are independent. That is, not paired or repeated measures
data
• Moderate skewness is permissible if the data distribution is unimodal without outliers
Hypotheses
• Null hypothesis: The means of the measurement variable for each sample are equal.
• Alternative hypothesis (two-sided): The means of the measurement variable for each sample
are not equal.
Interpretation
Reporting significant results as “Mean of variable Y for group A was different than that for group B.” is
acceptable.
487
TWO-SAMPLE T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(lattice)){install.packages("lattice")}
In the following example, Brendon Small and Coach McGuirk have their SNAP-Ed students keep diaries
of what they eat for a week, and then calculate the daily sodium intake in milligrams. Since the classes
have received different nutrition education programs, they want to see if the mean sodium intake is
the same for both classes.
A two-sample t-test can be conducted with the t.test function in the native stats package. The default is
to use Welch’s t-test, which doesn’t require equal variance between groups. Conveniently the output
includes the mean of each sample, a confidence interval for the difference in means, and a p-value for
the t-test.
We will use a histograms with imposed normal curves to confirm data are approximately normal.
Input = ("
Instructor Student Sodium
'Brendon Small' a 1200
'Brendon Small' b 1400
'Brendon Small' c 1350
'Brendon Small' d 950
'Brendon Small' e 1400
'Brendon Small' f 1150
'Brendon Small' g 1300
'Brendon Small' h 1325
'Brendon Small' i 1425
'Brendon Small' j 1500
'Brendon Small' k 1250
'Brendon Small' l 1150
'Brendon Small' m 950
'Brendon Small' n 1150
'Brendon Small' o 1600
'Brendon Small' p 1300
'Brendon Small' q 1050
'Brendon Small' r 1300
'Brendon Small' s 1700
'Brendon Small' t 1300
'Coach McGuirk' u 1100
488
TWO-SAMPLE T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Summarize(Sodium ~ Instructor,
data=Data,
digits=3)
489
TWO-SAMPLE T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
panel.mathdensity(dmath = dnorm,
col = "blue",
lwd = 2,
args = list(mean=mean(x),
sd=sd(x)), ...)})
490
TWO-SAMPLE T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
sample estimates:
mean in group Brandon Small mean in group Coach McGuirk
1287.50 1246.25
Optional readings
“Student's t–test for two samples” in McDonald, J.H. 2014. Handbook of Biological Statistics.
www.biostathandbook.com/twosamplettest.html.
References
“Student’s t–test for Two Samples” in Mangiafico, S.S. 2015a. An R Companion for the Handbook of
Biological Statistics, version 1.09. rcompanion.org/rcompanion/d_02.html.
“Mann–Whitney and Two-sample Permutation Test” in Mangiafico, S.S. 2015a. An R Companion for
the Handbook of Biological Statistics, version 1.09. rcompanion.org/rcompanion/d_02a.html.
Exercises P
491
TWO-SAMPLE T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
What is the 95% confidence interval for the difference in sodium intake?
2. As part of a professional skills program, a 4-H club tests its members for typing proficiency. Dr.
Katz and Laura want to compare their students’ mean typing speed between their classes.
Instructor Student Words.per.minute
'Dr. Katz Professional Therapist' a 35
'Dr. Katz Professional Therapist' b 50
'Dr. Katz Professional Therapist' c 55
'Dr. Katz Professional Therapist' d 60
'Dr. Katz Professional Therapist' e 65
'Dr. Katz Professional Therapist' f 60
'Dr. Katz Professional Therapist' g 70
'Dr. Katz Professional Therapist' h 55
'Dr. Katz Professional Therapist' i 45
'Dr. Katz Professional Therapist' j 55
'Dr. Katz Professional Therapist' k 60
'Dr. Katz Professional Therapist' l 45
'Dr. Katz Professional Therapist' m 65
'Dr. Katz Professional Therapist' n 55
'Dr. Katz Professional Therapist' o 50
'Dr. Katz Professional Therapist' p 60
'Laura the Receptionist' q 55
'Laura the Receptionist' r 60
'Laura the Receptionist' s 75
'Laura the Receptionist' t 65
'Laura the Receptionist' u 60
'Laura the Receptionist' v 70
'Laura the Receptionist' w 75
'Laura the Receptionist' x 70
'Laura the Receptionist' y 65
'Laura the Receptionist' z 72
'Laura the Receptionist' aa 73
'Laura the Receptionist' ab 65
'Laura the Receptionist' ac 80
'Laura the Receptionist' ad 50
'Laura the Receptionist' ae 55
'Laura the Receptionist' af 70
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
492
PAIRED T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Was the mean typing speed significantly different between the classes?
Paired t-test
The paired t-test is commonly used. It compares the means of two populations of paired observations
by testing if the difference between pairs is statistically different from zero or another number.
Appropriate data
• Two-sample data. That is, one measurement variable in two groups or samples
• Dependent variable is interval/ratio, and is continuous
• Independent variable is a factor with two levels. That is, two groups
• Data are paired. That is, the measurement for each observation in one group can be paired
logically or by subject to a measurement in the other group
• The distribution of the difference of paired measurements is normally distributed
• Moderate skewness is permissible if the data distribution is unimodal without outliers
Hypotheses
• Null hypothesis: The difference between paired observations is equal to zero.
• Alternative hypothesis (two-sided): The difference between paired observations is not equal
to zero.
Interpretation
Reporting significant results as “Mean of variable Y for group A was different than that for group B.” is
acceptable.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(rcompanion)){install.packages("rcompanion")}
493
PAIRED T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
In the following example, Dumbland Extension had adult students fill out a financial literacy
knowledge questionnaire both before and after completing a home financial management workshop.
Each student’s score before and after was paired by student.
Note in the following data that the students’ names are repeated, so that there is a before score for
student a and an after score for student a.
Since the data is in long form, we’ll order by Time, then Student to be sure the first observation for
Before is student a and the first observation for After is student a, and so on.
Input = ("
Time Student Score
Before a 65
Before b 75
Before c 86
Before d 69
Before e 60
Before f 81
Before g 88
Before h 53
Before i 75
Before j 73
After a 77
After b 98
After c 92
After d 77
After e 65
After f 77
After g 100
After h 73
After i 93
After j 75
")
Data = read.table(textConnection(Input),header=TRUE)
library(psych)
headTail(Data)
str(Data)
summary(Data)
494
PAIRED T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
rm(Input)
First, two new variables, Before and After, are created by extracting the values of Score for
observations with the Time variable equal to Before or After, respectively.
Before = Data$Score[Data$Time=="Before"]
After = Data$Score[Data$Time=="After"]
x = Difference
library(rcompanion)
plotNormalHistogram(x,
xlab="Difference (After - Before)")
Note that the points in the plot are jittered slightly so that points that would fall directly on top of one
another can be seen.
495
PAIRED T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
First, two new variables, Before and After, are created by extracting the values of Score for
observations with the Time variable equal to Before or After, respectively.
After = Data$Score[Data$Time=="After"]
Names = Data$Student[Data$Time=="Before"]
plot(Before, jitter(After), # jitter offsets points so you can see them all
pch = 16, # shape of points
cex = 1.0, # size of points
xlim=c(50, 110), # limits of x-axis
ylim=c(50, 110), # limits of y-axis
xlab="Before", # label for x-axis
ylab="After" # label for y-axis
)
New variables are first created for Before, After, and their Difference.
496
PAIRED T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Before = Data$Score[Data$Time=="Before"]
After = Data$Score[Data$Time=="After"]
Names = Data$Student[Data$Time=="Before"]
Paired t-test
t.test(Score ~ Time,
data=Data,
paired = TRUE,
conf.level = 0.95)
Paired t-test
sample estimates:
mean of the differences
10.2
497
PAIRED T-TEST SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Optional readings
References
“Paired t–test” in Mangiafico, S.S. 2015. An R Companion for the Handbook of Biological Statistics,
version 1.09. rcompanion.org/rcompanion/d_09.html.
Exercises Q
What was the mean difference in score before and after the training?
Was the mean score significantly different before and after the training?
2. Residential properties in Dougal County rarely need phosphorus for good turfgrass growth. As part
of an extension education program, Early and Rusty Cuyler asked homeowners to report their
phosphorus fertilizer use, in pounds of P2O5 per acre, before the program and then one year later.
Date Homeowner P2O5
'2014-01-01' a 0.81
'2014-01-01' b 0.86
'2014-01-01' c 0.79
'2014-01-01' d 0.59
'2014-01-01' e 0.71
'2014-01-01' f 0.88
'2014-01-01' g 0.63
'2014-01-01' h 0.72
'2014-01-01' i 0.76
'2014-01-01' j 0.58
'2015-01-01' a 0.67
'2015-01-01' b 0.83
'2015-01-01' c 0.81
'2015-01-01' d 0.50
'2015-01-01' e 0.71
'2015-01-01' f 0.72
'2015-01-01' g 0.67
498
ONE-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
'2015-01-01' h 0.67
'2015-01-01' i 0.48
'2015-01-01' j 0.68
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
What was the mean difference in P2O5 before and after the training?
Was the mean P2O5 use significantly different before and after the training?
One-way ANOVA
A one-way analysis of variance (ANOVA) is similar to a t-test, except that it is capable of comparing
more than two groups.
We will conduct the ANOVA by constructing a general linear model with the lm function in the native
stats package. The general linear model is the basis for more advanced parametric models that can
include multiple independent variables that can be continuous or factor variables.
Appropriate data
• One-way data. That is, one measurement variable in two or more groups
• Dependent variable is interval/ratio, and is continuous
• Independent variable is a factor with two or more levels. That is, two or more groups
• In the general linear model approach, residuals are normally distributed
• In the general linear model approach, groups have the same variance. That is,
homoscedasticity
• Observations among groups are independent. That is, not paired or repeated measures data
• Moderate deviation from normally-distributed residuals is permissible
Hypotheses
• Null hypothesis: The means of the measurement variable for each group are equal.
• Alternative hypothesis (two-sided): The means of the measurement variable for among
groups are not equal.
499
ONE-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Interpretation
• Reporting significant results for the omnibus test as “Significant differences were found
among means for groups.” is acceptable. Alternatively, “A significant effect for Independent
Variable on Dependent Variable was found.”
• Reporting significant results for mean separation post-hoc tests as “Mean of variable Y for
group A was different than that for group B.” is acceptable.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(Rmisc)){install.packages("Rmisc")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(car)){install.packages("car")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(rcompanion)){install.packages("rcompanion")}
In the following example, Brendon Small, Coach McGuirk, and Melissa Robbins have their SNAP-Ed
students keep diaries of what they eat for a week, and then calculate the daily sodium intake in
milligrams. Since the classes have received different nutrition education programs, they want to see if
the mean sodium intake is the same among classes.
The analysis will be conducted by constructing a general linear model with the lm function in the
native stats package, and conducting the analysis of variance with the Anova function in the car
package.
500
ONE-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Plots will be used to check model assumptions for normality of residuals and homoscedasticity.
Finally, if the effect of Instructor is significant, a mean comparison test will be conducted to determine
which means differ from which others.
Input = ("
Instructor Student Sodium
'Brendon Small' a 1200
'Brendon Small' b 1400
'Brendon Small' c 1350
'Brendon Small' d 950
'Brendon Small' e 1400
'Brendon Small' f 1150
'Brendon Small' g 1300
'Brendon Small' h 1325
'Brendon Small' i 1425
'Brendon Small' j 1500
'Brendon Small' k 1250
'Brendon Small' l 1150
'Brendon Small' m 950
'Brendon Small' n 1150
'Brendon Small' o 1600
'Brendon Small' p 1300
'Brendon Small' q 1050
'Brendon Small' r 1300
'Brendon Small' s 1700
'Brendon Small' t 1300
'Coach McGuirk' u 1100
'Coach McGuirk' v 1200
'Coach McGuirk' w 1250
'Coach McGuirk' x 1050
'Coach McGuirk' y 1200
'Coach McGuirk' z 1250
'Coach McGuirk' aa 1350
'Coach McGuirk' ab 1350
'Coach McGuirk' ac 1325
'Coach McGuirk' ad 1525
'Coach McGuirk' ae 1225
'Coach McGuirk' af 1125
'Coach McGuirk' ag 1000
'Coach McGuirk' ah 1125
'Coach McGuirk' ai 1400
'Coach McGuirk' aj 1200
'Coach McGuirk' ak 1150
'Coach McGuirk' al 1400
'Coach McGuirk' am 1500
'Coach McGuirk' an 1200
'Melissa Robins' ao 900
'Melissa Robins' ap 1100
'Melissa Robins' aq 1150
'Melissa Robins' ar 950
'Melissa Robins' as 1100
'Melissa Robins' at 1150
'Melissa Robins' au 1250
501
ONE-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
Data$Instructor = factor(Data$Instructor,
levels=unique(Data$Instructor))
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Summarize(Sodium ~ Instructor,
data=Data,
digits=3)
502
ONE-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Sum
library(ggplot2)
503
ONE-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Anova(model,
type = "II") ### Type II sum of squares
Response: Sodium
504
ONE-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Histogram of residuals
x = residuals(model)
library(rcompanion)
plotNormalHistogram(x)
plot(fitted(model),
residuals(model))
505
ONE-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The code defines an object leastsquare which will hold the output of the lsmeans function. The lsmeans
function here calls for all pairwise comparisons for the independent variable Instructor in the
previously defined model object.
There are two ways to view the lsmeans output. Calling the leastsquare object to be printed produces
two sections of output: $lsmeans, which shows estimates for the LS means along with their standard
errors and confidence intervals; and $contrasts, which indicate the pairwise comparisons with p-
values for the compared LS means being different.
Using the cld function produces a compact letter display. LS means sharing a letter are not
significantly different from one another at the alpha level indicated. A Tukey adjustment is made for
multiple comparisons with the adjust="tukey" option. And Letters indicates the symbols to use for
groups.
To adjust the confidence intervals for the LS means, use e.g. summary(leastsquare, level=0.99).
Note that because Instructor is the only independent variable in the model, the LS means are equal to
the arithmetic means produced by the Summarize function in this case.
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Instructor,
adjust = "tukey")
leastsquare
$lsmeans
Instructor lsmean SE df lower.CL upper.CL
Brendon Small 1287.50 36.12615 57 1215.159 1359.841
Coach McGuirk 1246.25 36.12615 57 1173.909 1318.591
Melissa Robins 1123.75 36.12615 57 1051.409 1196.091
$contrasts
contrast estimate SE df t.ratio p.value
Brendon Small - Coach McGuirk 41.25 51.09009 57 0.807 0.7000
Brendon Small - Melissa Robins 163.75 51.09009 57 3.205 0.0062
Coach McGuirk - Melissa Robins 122.50 51.09009 57 2.398 0.0510
cld(leastsquare,
alpha = 0.05,
Letters = letters, ### Use lower-case letters for .group
adjust="tukey") ### Tukey-adjusted comparisons
506
ONE-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
References
“One-way Anova” in Mangiafico, S.S. 2015a. An R Companion for the Handbook of Biological Statistics,
version 1.09. rcompanion.org/rcompanion/d_05.html.
“One-way Analysis with Permutation Test” in Mangiafico, S.S. 2015b. An R Companion for the
Handbook of Biological Statistics, version 1.09. rcompanion.org/rcompanion/d_06a.html.
Exercises R
Which instructors had classes with significantly different mean sodium intake from which
others?
Does this result correspond to what you would have thought from looking at the plot of the
means and standard errors or the box plots?
How does the mean separation change if you change the alpha level to 0.06?
2. As part of a professional skills program, a 4-H club tests its members for typing proficiency. Dr.
Katz, Laura, and Ben want to compare their students’ mean typing speed between their classes.
Instructor Student Words.per.minute
'Dr. Katz Professional Therapist' a 35
'Dr. Katz Professional Therapist' b 50
'Dr. Katz Professional Therapist' c 55
'Dr. Katz Professional Therapist' d 60
'Dr. Katz Professional Therapist' e 65
'Dr. Katz Professional Therapist' f 60
'Dr. Katz Professional Therapist' g 70
'Dr. Katz Professional Therapist' h 55
'Dr. Katz Professional Therapist' i 45
507
ONE-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
Which instructors had classes with significantly different mean typing speed from which
others?
508
ONE-WAY ANOVA WITH BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Does this result correspond to what you would have thought from looking at the plot of the
means and standard errors or box plots?
Traditionally, in agricultural experiments, plots would be arranged into blocks according to factors in
the field that could not be controlled. For example, if one side of the field were close to the tree line,
those plots might get less sun, or be more protected from wind. So the experimenter would consider
those plots near the tree line as one block, and another set in a more open spot another block. In this
way, all kinds of variation in the soils and locations could be accounted for somewhat. The experiment
might be designed in a randomized complete block design in which each block had a plot with each
treatment.
When using blocks, the experimenter isn’t concerned necessarily with the effect of the blocks or even
the factors behind assigning those blocks. That is, the east end of the field might be by the tree line
and may have soils with slightly poorer drainage. If the experimenter were testing the effects of
different fertilizers, she isn’t going to try to report the effect of “next to the tree line” or “in poorly
drained soil,” but she does want to account for these effects statistically.
Blocks in extension education for measurements on students might include school or class or grade.
There might be differences according to these factors, but we might not care to investigate if one
particular school has a different result than another particular one. Still, we want to take to these
differences into account statistically.
Appropriate data
• One-way data, with blocks. That is, one measurement variable in two or more groups, where
each group is also distributed among at least two blocks
• Dependent variable is interval/ratio, and is continuous
• Independent variable is a factor with two or more levels. That is, two or more groups
• A second independent variable is a blocking factor variable with two or more levels
• In the general linear model approach, residuals are normally distributed
• In the general linear model approach, groups have the same variance. That is,
homoscedasticity
• Observations among groups are independent. That is, not paired or repeated measures data,
except that observations are within blocks
• Moderate deviation from normally-distributed residuals is permissible
Hypotheses
• Null hypothesis: The means of the measurement variable for each group are equal
509
ONE-WAY ANOVA WITH BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
• Alternative hypothesis (two-sided): The means of the measurement variable for among
groups are not equal
• An additional null hypothesis is tested for the effect of blocks: The means of the measurement
variable for each block are equal
Interpretation
• Reporting significant results for the omnibus test as “Significant differences were found
among means for groups.” is acceptable. Alternatively, “A significant effect for independent
variable on dependent variable was found.”
• Reporting significant results for mean separation post-hoc tests as “Mean of variable Y for
group A was different than that for group B.” is acceptable.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(car)){install.packages("car")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(rcompanion)){install.packages("rcompanion")}
This example will revisit the sodium intake data set with Brendon Small and the other instructors.
This time, though, they have recorded the town each student is from, and they would like to use this as
a blocking variable. They suspect that the town of residence may have some effect on sodium intake
since each town has varying income, ethnic makeup, and other demographic factors.
The instructors are focused on the effect of their different nutrition education programs. They are not
concerned about sodium intake in one specific town or another per se, but they want to take account
this effect into account statistically.
510
ONE-WAY ANOVA WITH BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Note that when the blocking variable is added to the model, the calculated p-value for the effect of
Instructor is different than it was for the case of the one-way ANOVA without the blocking variable.
Input = ("
Instructor Town Sodium
'Brendon Small' Squiggleville 1200
'Brendon Small' Squiggleville 1400
'Brendon Small' Squiggleville 1350
'Brendon Small' Metalocalypse 950
'Brendon Small' Squiggleville 1400
'Brendon Small' Squiggleville 1150
'Brendon Small' Squiggleville 1300
'Brendon Small' Metalocalypse 1325
'Brendon Small' Metalocalypse 1425
'Brendon Small' Squiggleville 1500
'Brendon Small' Squiggleville 1250
'Brendon Small' Metalocalypse 1150
'Brendon Small' Metalocalypse 950
'Brendon Small' Squiggleville 1150
'Brendon Small' Metalocalypse 1600
'Brendon Small' Metalocalypse 1300
'Brendon Small' Metalocalypse 1050
'Brendon Small' Metalocalypse 1300
'Brendon Small' Squiggleville 1700
'Brendon Small' Squiggleville 1300
'Coach McGuirk' Squiggleville 1100
'Coach McGuirk' Squiggleville 1200
'Coach McGuirk' Squiggleville 1250
'Coach McGuirk' Metalocalypse 1050
'Coach McGuirk' Metalocalypse 1200
'Coach McGuirk' Metalocalypse 1250
'Coach McGuirk' Squiggleville 1350
'Coach McGuirk' Squiggleville 1350
'Coach McGuirk' Squiggleville 1325
'Coach McGuirk' Squiggleville 1525
'Coach McGuirk' Squiggleville 1225
'Coach McGuirk' Squiggleville 1125
'Coach McGuirk' Metalocalypse 1000
'Coach McGuirk' Metalocalypse 1125
'Coach McGuirk' Squiggleville 1400
'Coach McGuirk' Metalocalypse 1200
'Coach McGuirk' Squiggleville 1150
'Coach McGuirk' Squiggleville 1400
'Coach McGuirk' Squiggleville 1500
'Coach McGuirk' Squiggleville 1200
'Melissa Robins' Metalocalypse 900
'Melissa Robins' Metalocalypse 1100
'Melissa Robins' Metalocalypse 1150
'Melissa Robins' Metalocalypse 950
'Melissa Robins' Metalocalypse 1100
'Melissa Robins' Metalocalypse 1150
'Melissa Robins' Squiggleville 1250
'Melissa Robins' Squiggleville 1250
511
ONE-WAY ANOVA WITH BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
Data$Instructor = factor(Data$Instructor,
levels=unique(Data$Instructor))
Data$Town = factor(Data$Town,
levels=unique(Data$Town))
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
512
ONE-WAY ANOVA WITH BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Anova(model,
type = "II") ### Type II sum of squares
Response: Sodium
library(rcompanion)
plotNormalHistogram(x)
513
ONE-WAY ANOVA WITH BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
plot(fitted(model),
residuals(model))
In this example, you’ll note that the LS means are different from the arithmetic means calculated for
Instructor in the last chapter. This is a result of the fact that instructors have different numbers of
students from each town. That is, the design is unbalanced.
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Instructor,
adjust = "tukey")
leastsquare
$lsmeans
Instructor lsmean SE df lower.CL upper.CL
Brendon Small 1279.644 32.21856 56 1215.103 1344.186
Coach McGuirk 1214.827 33.10792 56 1148.504 1281.150
Melissa Robins 1155.173 33.10792 56 1088.850 1221.496
$contrasts
contrast estimate SE df t.ratio p.value
Brendon Small - Coach McGuirk 64.81742 45.86048 56 1.413 0.3410
Brendon Small - Melissa Robins 124.47097 46.53123 56 2.675 0.0261
Coach McGuirk - Melissa Robins 59.65356 48.12705 56 1.240 0.4352
514
ONE-WAY ANOVA WITH BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
cld(leastsquare,
alpha = 0.05,
Letters = letters, ### Use lower-case letters for .group
adjust = "tukey") ### Tukey-adjusted p-values
When a design is unbalanced, it is usually desirable to present LS means. For further discussion on
this, see the chapter What are Least Square Means?
### Extract variables from leastsquare and create new data frame called Sum
Instructor = summary(leastsquare$lsmeans)["Instructor"]
lsmean = summary(leastsquare$lsmeans)["lsmean"]
lower.CL = summary(leastsquare$lsmeans)["lower.CL"]
upper.CL = summary(leastsquare$lsmeans)["upper.CL"]
Sum
library(ggplot2)
515
ONE-WAY ANOVA WITH BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Least square mean daily sodium intake for students in each of three classes
following different nutrition education programs. Error bars represent confidence
intervals of the least square mean.
Exercises S
1. Considering Brendon, Melissa, and McGuirk’s data with means adjusted for students’ towns,
What was the LS mean sodium intake for students for each instructor?
Which instructors had classes with significantly different mean sodium intake from which
others?
Does this result correspond to what you would have thought from looking at the plot of the LS
means and confidence intervals?
2. As part of a professional skills program, a 4-H club tests its members for typing proficiency. Dr.
Katz, and Laura, and Ben want to compare their students’ mean typing speed between their classes.
They have also recorded the year or grade of each student. They are not interested in the effect of Year
per se, but they want to account for the effect statistically.
516
ONE-WAY ANOVA WITH BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Note that the following code is necessary for R to recognize Year as a factor variable.
### Convert Year from an integer variable to a factor variable
517
ONE-WAY ANOVA WITH RANDOM BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data$Year = factor(Data$Year,
levels=c("7", "8", "9", "10"))
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
What was the LS mean typing speed for each instructor’s class?
Which instructors had classes with significantly different mean typing speed from which
others?
Does this result correspond to what you would have thought from looking at the plot of the LS
means and standard errors?
In analysis of variance, blocking variables are often treated as random variables. This is because the
blocking variable represents a random selection of levels of that variable. The analyst wants to take
the effects of the blocking variable into account, but the identity of the specific levels of the blocks are
not of interest.
In the example in this chapter, the instructors are focused on the effect of their different nutrition
education programs, which are the treatments; they are not concerned about the effect of one specific
town or another per se, but do want to want to take into account any differences due to the different
towns.
For a more complete discussion of random effects, see the Using Random Effects in Models chapter.
518
ONE-WAY ANOVA WITH RANDOM BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(lme4)){install.packages("lme4")}
if(!require(lmerTest)){install.packages("lmerTest")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(nlme)){install.packages("nlme")}
if(!require(car)){install.packages("car")}
if(!require(rcompanion)){install.packages("rcompanion")}
519
ONE-WAY ANOVA WITH RANDOM BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
Data$Instructor = factor(Data$Instructor,
levels=unique(Data$Instructor))
Data$Town = factor(Data$Town,
levels=unique(Data$Town))
library(psych)
headTail(Data)
520
ONE-WAY ANOVA WITH RANDOM BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
str(Data)
summary(Data)
rm(Input)
Technical note
lmerTest::anova by default returns p-values for Type III sum of squares with a Satterthwaite
approximation for the degrees of freedom.
library(lme4)
library(lmerTest)
anova(model)
rand(model)
521
ONE-WAY ANOVA WITH RANDOM BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
One approach is to define the null model as one with no fixed effects except for an intercept, indicated
with a 1 on the right side of the ~. And to also include the random effects, in this case (1|Town).
model.null = lmer(Sodium ~ 1 + (1|Town),
data = Data,
REML = TRUE)
anova(model,
model.null)
library(rcompanion)
nagelkerke(fit = model,
null = model.null)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.00971559
Cox and Snell (ML) 0.11818400
Nagelkerke (Cragg and Uhler) 0.11818400
Another approach to determining the p-value and pseudo R-squared for an lmer model is to compare
the model to a null model with only an intercept and neither the fixed nor the random effects. To
accomplish this, the null model has to be specified with lm and not lmer. However, it may not be
permissible to compare models specified with different functions in this way. The anova function in
lmerTest does allow these comparisons. The nagelkerke function will also allow these comparisons,
although I do not know if the results are valid.
model.null.2 = lm(Sodium ~ 1,
data = Data)
anova(model,
model.null.2)
library(rcompanion)
nagelkerke(fit = model,
null = model.null.2)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.0239772
522
ONE-WAY ANOVA WITH RANDOM BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Post-hoc analysis
The lsmeans package is able to handle lmer objects. For further details, see ?lsmeans::models. For a
review of mean separation tests and least square means, see the chapters What are Least Square
Means?; the chapter Least Square Means for Multiple Comparisons; and the “Post-hoc analysis: mean
separation tests” section in the One-way ANOVA chapter.
The lmerTest package has a function Difflsmeans which also compares leastsquare means for
treatments, but doesn’t include output for a compact letter display or adjusted p-values.
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Instructor,
adjust="tukey") ### Tukey-adjusted comparisons
cld(leastsquare,
alpha=0.05,
Letters=letters, ### Use lower-case letters for .group
adjust="tukey") ### Tukey-adjusted comparisons
leastsquare
$contrasts
contrast estimate SE df t.ratio p.value
Brendon Small - Coach McGuirk 63.33828 45.93368 56.11 1.379 0.3587
Brendon Small - Melissa Robins 126.93620 46.73138 56.29 2.716 0.0234
Coach McGuirk - Melissa Robins 63.59792 48.62097 56.62 1.308 0.3967
A similar analysis can be conducted with the difflsmeans function in the lmerTest package.
library(lmerTest)
difflsmeans(model,
test.effs="Instructor")
523
ONE-WAY ANOVA WITH RANDOM BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Differences of LSMEANS:
Estimate Standard Error DF t-value Lower CI Upper CI p-value
Instructor Brendon Small - Coach McGuirk 63.3 45.8 56.1 1.38 -28.5 155 0.172
Instructor Brendon Small - Melissa Robins 126.9 46.5 56.3 2.73 33.9 220 0.008 **
Instructor Coach McGuirk - Melissa Robins 63.6 48.0 56.6 1.33 -32.5 160 0.190
The following code extracts a data frame from the difflsmeans output, and then adds a column of p-
values adjusted by the fdr method. See ?p.adjust for other adjustment options.
comparison = difflsmeans(model,
test.effs="Instructor")[[1]]
p.value = comparison$"p-value"
comparison$p.adj = p.adjust(p.value,
method = "fdr")
comparison
Estimate Standard Error DF t-value Lower CI Upper CI p-value p.adj
Instructor Brendon Small - Coach McGuirk 63.3383 45.8366 56.1 1.38 -28.4807 155.1573 0.1725 0.1902
Instructor Brendon Small - Melissa Robins 126.9362 46.4659 56.3 2.73 33.8631 220.0093 0.0084 0.0252
Instructor Coach McGuirk - Melissa Robins 63.5979 47.9651 56.6 1.33 -32.4661 159.6619 0.1902 0.1902
The fixed effects in the model can be tested with the Anova function in the car package.
library(nlme)
library(car)
Anova(model)
Chisq Df Pr(>Chisq)
Instructor 7.4827 2 0.02372 *
The random effects in the model can be tested by specifying a null model with only fixed effects and
comparing it to the full model with anova. The nlme package has a function gls that creates model
objects without random effects in a manner analogous to those specified with lme.
model.null = gls(Sodium ~ Instructor,
data=Data,
method="REML")
524
ONE-WAY ANOVA WITH RANDOM BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
anova(model,
model.null)
nagelkerke(model)
$Models
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.00971559
Cox and Snell (ML) 0.11818400
Nagelkerke (Cragg and Uhler) 0.11818400
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-2 -3.7732 7.5463 0.02298
$Messages
[1] "Note: For models fit with REML, these statistics are based on refitting with
ML"
Another approach to determining the p-value and pseudo R-squared for an lme model is to compare
the model to a null model with only an intercept and neither the fixed nor the random effects. To
accomplish this, the null model has to be specified with the gls function.
model.null.2 = gls(Sodium ~ 1,
data=Data,
method="REML")
library(rcompanion)
nagelkerke(fit = model,
null = model.null.2)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.0239772
Cox and Snell (ML) 0.2701590
Nagelkerke (Cragg and Uhler) 0.2701600
525
ONE-WAY ANOVA WITH RANDOM BLOCKS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-3 -9.4479 18.896 0.00028731
Post-hoc analysis
The lsmeans package is able to handle lme objects. For futher details, see ?lsmeans::models. For a
review of mean separation tests and least square means, see the chapters What are Least Square
Means?; the chapter Least Square Means for Multiple Comparisons; and the “Post-hoc analysis: mean
separation tests” section in the One-way ANOVA chapter.
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Instructor,
adjust="tukey") ### Tukey-adjusted comparisons
cld(leastsquare,
alpha = 0.05,
Letters = letters, ### Use lower-case letters for .group
adjust = "tukey") ### Tukey-adjusted comparisons
leastsquare
$contrasts
contrast estimate SE df t.ratio p.value
Brendon Small - Coach McGuirk 63.33828 45.83662 56 1.382 0.3572
Brendon Small - Melissa Robins 126.93620 46.46588 56 2.732 0.0225
Coach McGuirk - Melissa Robins 63.59792 47.96514 56 1.326 0.3869
526
TWO-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Mean separation a a a
for Instructor ab ab ab
b b b
Two-way ANOVA
A two-way anova can investigate the main effects of each of two independent factor variables, as well
as the effect of the interaction of these variables.
For an overview of the concepts in multi-way analysis of variance, review the chapter Factorial
ANOVA: Main Effects, Interaction Effects, and Interaction Plots.
For a review of mean separation tests and least square means, see the chapters What are Least Square
Means?; the chapter Least Square Means for Multiple Comparisons; and the “Post-hoc analysis: mean
separation tests” section in the One-way ANOVA chapter.
Appropriate data
• Two-way data. That is, one dependent variable measured across two independent factor
variables
• Dependent variable is interval/ratio, and is continuous
• Independent variables are a factor with two or more levels. That is, two or more groups for
each independent variable
• In the general linear model approach, residuals are normally distributed
• In the general linear model approach, groups have the same variance. That is,
homoscedasticity
• Observations among groups are independent. That is, not paired or repeated measures data
• Moderate deviation from normally-distributed residuals is permissible
Hypotheses
• Null hypothesis 1: The means of the dependent variable for each group in the first
independent variable are equal
• Alternative hypothesis 1 (two-sided): The means of the dependent variable for each group in
the first independent variable are not equal
• Similar hypotheses are considered for the other independent variable
• Similar hypotheses are considered for the interaction effect
527
TWO-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Interpretation
• Reporting significant results for the effect of one independent variable as “Significant
differences were found among means for groups.” is acceptable. Alternatively, “A significant
effect for independent variable on dependent variable was found.”
• Reporting significant results for the interaction effect as “A significant effect for the
interaction of independent variable 1 and independent variable 2 on dependent variable was
found.” Is acceptable.
• Reporting significant results for mean separation post-hoc tests as “Mean of variable Y for
group A was different than that for group B.” is acceptable.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(FSA)){install.packages("FSA")}
if(!require(car)){install.packages("car")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(Rmisc)){install.packages("Rmisc")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(rcompanion)){install.packages("rcompanion")}
This example will revisit the sodium intake data set with Brendon Small and the other instructors.
This time, though, they want to test not only the different nutrition education programs (indicated by
Instructor), but also four supplements to the program, each of which they have used with some
students.
Input = ("
Instructor Supplement Sodium
'Brendon Small' A 1200
'Brendon Small' A 1400
'Brendon Small' A 1350
'Brendon Small' A 950
'Brendon Small' A 1400
'Brendon Small' B 1150
528
TWO-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
529
TWO-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
Data$Instructor = factor(Data$Instructor,
levels=unique(Data$Instructor))
Data$Supplement = factor(Data$Supplement,
levels=unique(Data$Supplement))
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
530
TWO-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Anova(model,
type = "II") ### Type II sum of squares
Response: Sodium
Sum Sq Df F value Pr(>F)
Instructor 290146 2 6.0173 0.004658 **
Supplement 306125 3 4.2324 0.009841 **
Instructor:Supplement 24438 6 0.1689 0.983880
Residuals 1157250 48
Histogram of residuals
x = residuals(model)
library(rcompanion)
plotNormalHistogram(x)
plot(fitted(model),
residuals(model))
531
TWO-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Instructor,
adjust = "tukey")
leastsquare
cld(leastsquare,
alpha = 0.05,
Letters = letters, ### Use lowercase letters for .group
adjust = "tukey") ### Tukey-adjusted comparisons
532
TWO-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Supplement,
adjust = "tukey")
leastsquare
cld(leastsquare,
alpha = 0.05,
Letters = letters, ### Use lowercase letters for .group
adjust = "tukey") ### Tukey-adjusted comparisons
Here we’ll use the summarySE function in Rmisc to get the traditional confidence intervals for the
means, and then use ggplot to plot the means.
Note that the last few lines of code add mean separation letters to the plot. This code will need to be
changed manually in other instances.
library(Rmisc)
Sum
library(ggplot2)
533
TWO-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
annotate("text",
x = 1:3,
y = Sum$Sodium + Sum$ci + 20,
label = c("b", "b", "a"))
library(Rmisc)
Sum
library(ggplot2)
534
TWO-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
annotate("text",
x = 1:4,
y = Sum$Sodium + Sum$ci + 20,
label = c("a", "b", "a", "ab"))
Extracting the LS means from the output of the lsmeans function takes a few extra steps. We’ll create a
data frame called Sum, and pass that data frame to ggplot to plot the LS means and confidence
intervals.
leastsquare = lsmeans(model,
pairwise ~ Supplement:Instructor,
adjust = "tukey")
### Extract variables from leastsquare and create new data frame called Sum
535
TWO-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Instructor = summary(leastsquare$lsmeans)["Instructor"]
Supplement = summary(leastsquare$lsmeans)["Supplement"]
lsmean = summary(leastsquare$lsmeans)["lsmean"]
lower.CL = summary(leastsquare$lsmeans)["lower.CL"]
upper.CL = summary(leastsquare$lsmeans)["upper.CL"]
Sum
library(ggplot2)
pd = position_dodge(.2)
ggplot(Sum, aes(x=Instructor,
y=lsmean,
color=Supplement)) +
geom_errorbar(aes(ymin=lower.CL,
ymax=upper.CL),
width=.2, size=0.7, position=pd) +
geom_point(shape=15, size=4, position=pd) +
theme_bw() +
theme(axis.title = element_text(face = "bold"))+
ylab("LS mean sodium, mg")
Least square mean daily sodium intake for students in each of three classes with
four different supplemental programs. Error bars indicate confidence intervals
for the least square means.
536
TWO-WAY ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Exercises T
1. Considering Brendon, Melissa, and McGuirk’s data with for instructional programs and supplements,
Does the interaction of Instructor and Supplement have a significant effect on sodium intake?
Are the residuals from the analysis reasonably normal and homoscedastic?
Which instructors had classes with significantly different mean sodium intake from which
others?
Does this result correspond to what you would have thought from looking at the plot of the LS
means and confidence intervals?
2. As part of a professional skills program, a 4-H club tests its members for typing proficiency. Dr.
Katz, and Laura, and Ben want to compare their students’ mean typing speed between their classes, as
well as for the software they used to teach typing.
Instructor Software Words.per.minute
'Dr. Katz Professional Therapist' A 35
'Dr. Katz Professional Therapist' A 50
'Dr. Katz Professional Therapist' A 55
'Dr. Katz Professional Therapist' A 60
'Dr. Katz Professional Therapist' B 65
'Dr. Katz Professional Therapist' B 60
'Dr. Katz Professional Therapist' B 70
'Dr. Katz Professional Therapist' B 55
'Dr. Katz Professional Therapist' C 45
'Dr. Katz Professional Therapist' C 55
'Dr. Katz Professional Therapist' C 60
'Dr. Katz Professional Therapist' C 45
'Laura the Receptionist' A 55
'Laura the Receptionist' A 60
'Laura the Receptionist' A 75
'Laura the Receptionist' A 65
'Laura the Receptionist' B 60
'Laura the Receptionist' B 70
'Laura the Receptionist' B 75
'Laura the Receptionist' B 70
537
REPEATED MEASURES ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
What was the LS mean typing speed for each instructor’s class?
Which instructors had classes with significantly different mean typing speed from which
others?
Does this result correspond to what you would have thought from looking at the plot of the LS
means and standard errors?
In previous chapters, our approach to deal with non-independent observations was to treat Student as
a blocking variable or as a random (blocking) variable.
538
REPEATED MEASURES ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The approach in this chapter is to include an autocorrelation structure in the model using the nmle
package.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(nlme)){install.packages("nlme")}
if(!require(car)){install.packages("car")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(rcompanion)){install.packages("rcompanion")}
In this example, students were asked to document their daily caloric intake once a month for six
months. Students were divided into three groups with each receiving instruction in nutrition
education using one of three curricula.
There are different ways we might approach this problem. If we simply wanted to see if one
curriculum was better at decreasing caloric intake in students, we might do a simple analysis of
variance on the difference between each student’s final and initial intake.
In the approach here we will use a repeated measures analysis with all the measurements, treating
Student as a random variable to take into account native differences among students, and including an
autocorrelation structure.
Input = ("
Instruction Student Month Calories.per.day
'Curriculum A' a 1 2000
'Curriculum A' a 2 1978
'Curriculum A' a 3 1962
'Curriculum A' a 4 1873
'Curriculum A' a 5 1782
'Curriculum A' a 6 1737
'Curriculum A' b 1 1900
539
REPEATED MEASURES ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
540
REPEATED MEASURES ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
Data$Instruction = factor(Data$Instruction,
levels=unique(Data$Instruction))
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
The autocorrelation structure is described with the correlation statement. In this case, corAR1 is used
to indicate a temporal autocorrelation structure of order one, often abbreviated as AR(1). This
statement takes the form:
correlation = Structure(form = ~ time | subjvar)
where:
• Structure is the autocorrelation structure. Options are listed in library(nlme); ?corClasses
• time is the variable indicating time. In this case, Month. For the corAR1 structure, the time
variable must be an integer variable.
541
REPEATED MEASURES ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
• subjvar indicates the variable for experimental units, in this case Student. Autocorrelation is
modeled within levels of the subjvar, and not between them.
For the corAR1 structure, a value for the first order correlation can be specified. In this case, the value
of 0.429 is found using the ACF function in the “Optional analysis: determining autocorrelation in
residuals” section below.
Autocorrelation structures can be chosen by either of two methods. The first is to choose a structure
based on theoretical expectations of how the data should be correlated. The second is to try various
autocorrelation structures and compare the resulting models with a criterion like AIC, AICc, or BIC to
choose the structure that best models the data.
library(nlme)
library(car)
Anova(model)
library(car)
Anova(model)
Response: Calories.per.day
Chisq Df Pr(>Chisq)
Instruction 10.4221 2 0.005456 **
Month 0.0198 1 0.888045
542
REPEATED MEASURES ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
anova(model,
model.fixed)
One approach is to define the null model as one with no fixed effects except for an intercept, indicated
with a 1 on the right side of the ~. And to also include the random effects, in this case 1|Student.
library(rcompanion)
model.null = lme(Calories.per.day ~ 1,
random = ~1|Student,
data = Data)
nagelkerke(model,
model.null)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.123969
Cox and Snell (ML) 0.768652
Nagelkerke (Cragg and Uhler) 0.768658
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-6 -52.698 105.4 1.8733e-20
Another approach to determining the p-value and pseudo R-squared for an lme model is to compare
the model to a null model with only an intercept and neither the fixed nor the random effects. To
accomplish this, the null model has to be specified with the gls function.
library(rcompanion)
model.null.2 = gls(Calories.per.day ~ 1,
543
REPEATED MEASURES ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
data = Data)
nagelkerke(model,
model.null.2)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.168971
Cox and Snell (ML) 0.877943
Nagelkerke (Cragg and Uhler) 0.877946
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-7 -75.717 151.43 2.0282e-29
Post-hoc analysis
The lsmeans package is able to handle lme objects. For further details, see ?lsmeans::models. For a
review of mean separation tests and least square means, see the chapters What are Least Square
Means?; the chapter Least Square Means for Multiple Comparisons; and the “Post-hoc analysis: mean
separation tests” section in the One-way ANOVA chapter.
Because Month is an integer variable, not a factor variable, it is listed in the lsmeans cld table as its
average only.
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Instruction:Month,
adjust="tukey") ### Tukey-adjusted comparisons
cld(leastsquare,
alpha = 0.05,
Letters = letters, ### Use lower-case letters for .group
adjust = "tukey") ### Tukey-adjusted comparisons
Interaction plot
For this plot, we will use the groupwiseMean function to calculate the natural mean of each Instruction
x Month combination, along with the confidence interval of each mean with the percentile method.
544
REPEATED MEASURES ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(rcompanion)
Sum
library(ggplot2)
pd = position_dodge(.2)
545
REPEATED MEASURES ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Histogram of residuals
Residuals from a mixed model fit with nlme should be normally distributed. Plotting residuals vs.
fitted values, to check for homoscedasticity and independence, is probably also advisable.
x = residuals(model)
library(rcompanion)
plotNormalHistogram(x)
plot(fitted(model),
residuals(model))
546
REPEATED MEASURES ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The ACF function in the nlme package will indicate the autocorrelation for lags in the time variable.
Note that for a gls model, the form of the autocorrelation structure can be specified. For an lme model,
the function uses the innermost group level and assumes equally spaced intervals.
library(nlme)
ACF(model.a,
form = ~ Month | Student)
lag ACF
1 0 1.0000000
2 1 0.8989822
3 2 0.7462712
4 3 0.6249217
5 4 0.5365430
6 5 0.4564673
library(nlme)
ACF(model.b)
lag ACF
1 0 1.0000000
2 1 0.4286522
3 2 -0.1750233
547
REPEATED MEASURES ANOVA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
4 3 -0.6283754
5 4 -0.8651088
6 5 -0.6957774
A similar specification in with the gls function in nlme package in R would be:
correlation = corAR1(form = ~ date | id)
Likewise, a simple mixed effects repeated analysis statement in proc mixed in SAS could be specified
with:
random id
repeated date / subject = id type = AR(1)
A similar specification in with the lme function in nlme package in R would be:
random = ~1 | id,
correlation = corAR1(form = ~ date | id)
548
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
In SAS:
treatment(block)
is equivalent to
block block*treatment
In R:
block/treatment
is equivalent to
block + block:treatment
Correlation determines if one variable varies systematically as another variable changes. It does not
specify that one variable is the dependent variable and the other is the independent variable. The
three forms of correlation presented here are Pearson, Kendall, and Spearman. Pearson is a
parametric test that assumes that data are bivariate normal. Kendall and Spearman are
nonparametric tests. It is often useful to look at which variables are correlated to others in a data set,
and it is especially useful to see which variables correlate to a particular variable of interest.
In contrast, linear regression specifies one variable as the independent variable and another as the
dependent variable. The resultant model relates the variables with a linear relationship. Linear
regression is a parametric test that assumes normality, homoscedasticity, and independence of
residuals, as well as a linear relationship between the two variables.
Appropriate data
• For Pearson correlation, two interval/ratio variables. Together the data in the variables are
bivariate normal. The relationship between the two variables is linear. Outliers can
detrimentally affect results.
549
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
• For linear regression, two interval/ratio variables. The relationship between the two
variables is linear. Residuals are normal, independent, and homoscedastic. Outliers can
affect the results unless robust methods are used.
Hypotheses
• For correlation, null hypothesis: The correlation coefficient (r, tau, or rho) is zero. Or, there
is no correlation between the two variables.
• For linear regression, null hypothesis: The slope of the fit line is zero. Or, there is no linear
relationship between the two variables.
Interpretation
• For correlation, reporting significant results as “Variable A was significantly correlated to
Variable B” is acceptable. Alternatively, “A significant correlation between Variable A and
Variable B was found.” Or, “Variables A, B, and C were significantly correlated.”
• For linear regression, reporting significant results as “Variable A was significantly linearly
related to Variable B” is acceptable. Alternatively, “A significant linear regression between
Variable A and Variable B was found.”
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(PerformanceAnalytics)){install.packages("PerformanceAnalytics")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(ggExtra)){install.packages("ggExtra")}
if(!require(rcompanion)){install.packages("rcompanion")}
Brendon Small and company recorded several measurements for students in their classes related to
their nutrition education program: Grade, Weight in kilograms, intake of Calories per day, daily Sodium
intake in milligrams, and Score on the assessment of knowledge gain.
Input = ("
Instructor Grade Weight Calories Sodium Score
'Brendon Small' 6 43 2069 1287 77
'Brendon Small' 6 41 1990 1164 76
550
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
Data$Instructor = factor(Data$Instructor,
levels=unique(Data$Instructor))
551
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Multiple correlation
The pairs function can plot multiple numeric or integer variables on a single plot to look for
correlations among the variables.
pairs(data=Data,
~ Grade + Weight + Calories + Sodium + Score)
552
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The corr.test function in the psych package can be used in a similar manner, with the output being a
table of correlation coefficients and a table of p-values. p-values can be adjusted with the adjust=
option. Options for correlation methods are “pearson”, “kendall”, and “spearman”. The function can
produce confidence intervals for the correlation coefficients, but I recommend using the cor.ci function
in the psych package for this task (not shown here).
The corr.test function requires that the data frame contain only numeric or integer variables, so we
will first create a new data frame called Data.num containing only the numeric and integer variables.
Data.num = Data[c("Grade", "Weight", "Calories", "Sodium", "Score")]
library(psych)
corr.test(Data.num,
use = "pairwise",
method = "pearson",
adjust = "none")
Correlation matrix
553
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Sample Size
[1] 45
A useful plot of histograms, correlations, and correlation coefficients can be produced with the
chart.Correlation function in the PerformanceAnalytics package.
In the output, the numbers represent the correlation coefficients (r, tau, or rho, depending on whether
Pearson, Kendall, or Spearman correlation is selected, respectively). The stars represent the p-value of
the correlation:
* p < 0.05
** p < 0.01
*** p < 0.001
The function requires that the data frame contain only numeric or integer variables, so we will first
create a new data frame called Data.num containing only the numeric and integer variables.
Data.num = Data[c("Grade", "Weight", "Calories", "Sodium", "Score")]
library(PerformanceAnalytics)
chart.Correlation(Data.num,
method="pearson",
histogram=TRUE,
pch=16)
554
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Pearson correlation
Pearson correlation is a parametric analysis that requires that the relationship between the variables
is linear, and that the data be bivariate normal. Variables should be interval/ratio. The test is
sensitive to outliers.
The correlation coefficient, r, can range from +1 to –1, with +1 being a perfect positive correlation and
–1 being a perfect negative correlation. An r of 0 represents no correlation whatsoever, but a non-
significant result should also be considered a lack of statistical correlation.
555
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Pearson correlation
cor.test( ~ Sodium + Calories,
data=Data,
method = "pearson",
conf.level = 0.95)
sample estimates:
cor
0.8489548
Plot residuals
It’s not a bad idea to look at the residuals from Pearson correlation to be sure the data meet the
assumption of bivariate normality. Unfortunately, the cor.test function doesn’t supply residuals. One
solution is to use the lm function, which actually redoes the analysis as a linear regression.
model = lm(Sodium ~ Calories,
data = Data)
x = residuals(model)
library(rcompanion)
plotNormalHistogram(x)
556
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Kendall correlation
Kendall correlation is considered a nonparametric analysis. It is a rank-based test that does not
require assumptions about the distribution of the data. Variables can be interval/ratio or ordinal.
The correlation coefficient from the test is tau, which can range from +1 to –1, with +1 being a perfect
positive correlation and –1 being a perfect negative correlation. A tau of 0 represents no correlation
whatsoever, but a non-significant result should also be considered a lack of statistical correlation.
Technically, the cor.test function in R calculates tau-b, which handles ties in ranks well.
The test is relatively robust to outliers in the data. The test is sometimes cited for being reliable when
there are small number of samples or when there are many ties in ranks.
Kendall correlation
cor.test( ~ Sodium + Calories,
data=Data,
method = "kendall")
sample estimates:
tau
0.6490902
Spearman correlation
Spearman correlation is considered a nonparametric analysis. It is a rank-based test that does not
require assumptions about the distribution of the data. Variables can be interval/ratio or ordinal.
557
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The correlation coefficient from the test, rho, can range from +1 to –1, with +1 being a perfect positive
correlation and –1 being a perfect negative correlation. A rho of 0 represents no correlation
whatsoever, but a non-significant result should also be considered a lack of statistical correlation.
Spearman correlation is probably most often used with ordinal data. It tests for a monotonic
relationship between the variables. It is relatively robust to outliers in the data.
Spearman correlation
cor.test( ~ Sodium + Calories,
data=Data,
method = "spearman")
sample estimates:
rho
0.8201766
Linear regression
Linear regression is a very common approach to model the relationship between two interval/ratio
variables. The method assumes that there is a linear relationship between the dependent variable and
the independent variable, and finds a best fit model for this relationship.
Interpretation of coefficients
The outcome of linear regression includes estimating the intercept and the slope of the linear model.
Linear regression can then be used as predictive model, whereby the model can be used to predict a y
value for any given x. In practice, the model shouldn’t be used to predict values beyond the range of
the x values used to develop the model.
If the independent variable were of nominal type, then the linear regression would become a one-way
analysis of variance.
558
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Handling independent variables of ordinal type can be complicated. Often they are treated as either
nominal type or interval/ratio type, although there are drawbacks to each approach.
Assumptions
Linear regression assumes linear relationship between the two variables, normality of the residuals,
independence of the residuals, and homoscedasticity of residuals.
Define model, and produce model coefficients, p-value, and r-squared value
Linear regression can be performed with the lm function, which was the same function we used for
analysis of variance.
The summary function for lm model objects includes estimates for model parameters (intercept and
slope), as well as an r-squared value for the model and p-value for the model. Note that even for
bivariate regression, the output calls the r-squared value “Multiple R-squared”.
model = lm(Sodium ~ Calories,
data = Data)
summary(model)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 519.07547 78.78211 6.589 5.09e-08 ***
Calories 0.35909 0.03409 10.534 1.74e-13 ***
abline(model,
col = "blue",
lwd = 2)
559
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
There is a clear nonlinearity to the data, suggesting that the simple linear model is not the best fit. This
nonlinearity is apparent in the plot of residuals vs. fitted values as well.
The next chapter will include fitting linear plateau, quadratic plateau, and a curvilinear models with
this data.
The following section will attempt to improve the model fit by adding polynomial terms.
Plots of residuals
x = residuals(model)
library(rcompanion)
plotNormalHistogram(x)
560
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
plot(fitted(model),
residuals(model))
Polynomial regression
Polynomial regression adds additional terms to the model, so that the terms include some set of the
linear, quadratic, cubic, and quartic, etc., forms of the independent variable. These terms are
mathematically the independent variable, the square of the independent variable, the cube of the
independent variable, and so on.
Our new variables will be Calories2 for the square of Calories, Calories3 for the cube of Calories, and so
on.
Data$Calories = as.numeric(Data$Calories)
We can use the compareLM function to list AIC, AICc, and BIC for a series of models.
model.1 = lm(Sodium ~ Calories,
data = Data)
561
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
data = Data)
library(rcompanion)
$Models
Formula
1 "Sodium ~ Calories"
2 "Sodium ~ Calories + Calories2"
3 "Sodium ~ Calories + Calories2 + Calories3"
4 "Sodium ~ Calories + Calories2 + Calories3 + Calories4"
$Fit.criteria
Rank Df.res AIC AICc BIC R.squared Adj.R.sq p.value Shapiro.W Shapiro.p
1 2 43 461.1 461.7 466.5 0.7207 0.7142 1.737e-13 0.9696 0.2800
2 3 42 431.4 432.4 438.6 0.8621 0.8556 8.487e-19 0.9791 0.5839
3 4 41 433.2 434.7 442.2 0.8627 0.8526 1.025e-17 0.9749 0.4288
4 5 40 428.5 430.8 439.4 0.8815 0.8696 5.566e-18 0.9625 0.1518
If we use BIC as our model fit criterion, we would choose model.2 as the best model for these data,
since it had the lowest BIC. AIC or AICc could have been used as criteria instead.
BIC tends to penalize models more than the other criteria for having additional parameters, so it will
tend to choose models with fewer terms.
There is not generally accepted advice as to which model fitting criterion to use. My advice might be to
use BIC when a more parsimonious model (one with fewer terms) is a priority, and in other cases use
AICc.
For final model, produce model coefficients, p-value, and R-squared value
summary(model.2)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -3.707e+03 6.463e+02 -5.736 9.53e-07 ***
Calories 4.034e+00 5.604e-01 7.198 7.58e-09 ***
Calories2 -7.946e-04 1.211e-04 -6.563 6.14e-08 ***
562
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
plotPredy(data = Data,
y = Sodium,
x = Calories,
x2 = Calories2,
model = model.2,
order = 2,
xlab = "Calories per day",
ylab = "Sodium intake per day")
ggplot(Data,
aes(x = Calories,
y = Sodium)) +
geom_point() +
geom_smooth(method = "lm",
formula = y ~ poly(x, 2, raw=TRUE), ### polynomial of order 2
se = TRUE)
563
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Plots of residuals
x = residuals(model.2)
library(rcompanion)
plotNormalHistogram(x)
plot(fitted(model.2),
residuals(model.2))
564
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Correlation
xkcd.com/552/
Extrapolating
xkcd.com/605/
Cat proximity
xkcd.com/231/
Exercises U
1. Considering the data from Brendon, Jason, Melissa, Paula, and McGuirk,
Does the quadratic polynomial model fit the Sodium vs. Calories data better than the linear
model?
565
CORRELATION AND LINEAR REGRESSION SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
2. As part of a professional skills program, a 4-H club tests its members for typing proficiency
(Words.per.minute), Proofreading skill, proficiency with using a Spreadsheet, and acumen in Statistics.
Instructor Grade Words.per.minute Proofreading Spreadsheet Statistics
'Dr. Katz' 6 35 53 75 61
'Dr. Katz' 6 50 77 24 51
'Dr. Katz' 6 55 71 62 55
'Dr. Katz' 6 60 78 27 91
'Dr. Katz' 6 65 84 44 95
'Dr. Katz' 6 60 79 38 50
'Dr. Katz' 6 70 96 12 94
'Dr. Katz' 6 55 61 55 76
'Dr. Katz' 6 45 73 59 75
'Dr. Katz' 6 55 75 55 80
'Dr. Katz' 6 60 85 35 84
'Dr. Katz' 6 45 61 49 80
'Laura' 7 55 59 79 57
'Laura' 7 60 60 60 60
'Laura' 7 75 90 19 64
'Laura' 7 65 87 32 65
'Laura' 7 60 70 33 94
'Laura' 7 70 84 27 54
'Laura' 7 75 87 24 59
'Laura' 7 70 97 38 74
'Laura' 7 65 86 30 52
'Laura' 7 72 91 36 66
'Laura' 7 73 88 20 57
'Laura' 7 65 86 19 71
'Ben Katz' 8 55 84 20 76
'Ben Katz' 8 55 63 44 94
'Ben Katz' 8 70 95 31 88
'Ben Katz' 8 55 63 69 93
'Ben Katz' 8 65 65 47 70
'Ben Katz' 8 60 61 63 92
'Ben Katz' 8 70 80 35 60
'Ben Katz' 8 60 88 38 58
'Ben Katz' 8 60 71 65 99
'Ben Katz' 8 62 78 46 54
'Ben Katz' 8 63 89 17 60
'Ben Katz' 8 65 75 33 77
For each of the following, answer the question, and show the output from the analyses you used to
answer the question.
566
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Do the residuals suggest that the linear regression model is an appropriate model?
What can you conclude about the results of the linear regression?
References
“Correlation and Linear Regression” in Mangiafico, S.S. 2015. An R Companion for the Handbook
of Biological Statistics, version 1.09. rcompanion.org/rcompanion/e_01.html.
“Multiple Regression” in Mangiafico, S.S. 2015. An R Companion for the Handbook of Biological
Statistics, version 1.09. rcompanion.org/rcompanion/e_05.html.
567
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The following commands will install these packages if they are not already installed:
if(!require(robustbase)){install.packages("robustbase")}
if(!require(pysch)){install.packages("pysch")}
if(!require(nlstools)){install.packages("nlstools")}
if(!require(minpack.lm)){install.packages("minpack.lm")}
if(!require(car)){install.packages("car")}
if(!require(rcompanion)){install.packages("rcompanion")}
if(!require(mblm)){install.packages("mblm")}
Except for t-tests, the approach of this book for parametric statistics has been to develop linear models
(with the lm function) or mixed effects models (with the nlme or lme4 packages) and then to apply
analysis of variance, model checking, and post-hoc testing.
These relatively simple models can be expanded to include more independent variables of either
continuous or factor type. Likewise, more complex designs can include nested and crossed factors.
Some traditional experimental designs include Latin square, split plot, and incomplete block.
Analysis of co-variance
Analysis of covariance combines continuous independent variables with factor independent variables.
A related design is the paired watershed design which, while it was developed for watershed studies,
can be adapted to various situations in a variety of fields.
Clausen, J.C. and J. Spooner. 1993. Paired Watershed Study Design. 841-F-93-009. United States
Environmental Protection Agency, Office of Water. Washington, DC.
nepis.epa.gov/Exe/ZyPURL.cgi?Dockey=20004PR6.TXT.
Dressing, S.A. and D.W. Meals. 2005. Designing water quality monitoring programs for
watershed projects. U.S. Environmental Protection Agency. Fairfax, VA.
www.epa.gov/sites/production/files/2015-10/documents/technote2_wq_monitoring.pdf.
Nonlinear and curvilinear regression are similar to linear regression, except that the relationship
between the dependent variable and the independent variable is curved in some way. Specific
methods include polynomial regression, spline regression, and nonlinear regression.
568
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Polynomial regression was covered briefly in the previous chapter, while some examples of curvilinear
regression are shown below in the “Linear plateau and quadratic plateau models” section in this
chapter.
Multiple regression
Multiple regression is similar to linear regression, except that the model contains multiple
independent variables.
The polynomial regression example in the previous chapter is one example of multiple regression.
“Multiple Regression” in Mangiafico, S.S. 2015. An R Companion for the Handbook of Biological
Statistics, version 1.09. rcompanion.org/rcompanion/e_05.html.
Logistic regression
Logistic regression is similar to linear regression, except that the dependent variable is categorical. In
standard logistic regression, the dependent variable has only two levels, and in multinomial logistic
regression, the dependent variable can have more than two levels.
For examples of logistic regression, see the chapter Models for Nominal Data in this book.
When the dependent variable is a counted quantity and not a measured quantity, it is appropriate to
use Poisson regression or related techniques.
For a discussion of when standard parametric approaches may or may not be used with count data,
see the “Count data may not be appropriate for common parametric tests” section in the Introduction
to Parametric Tests chapter.
For examples of appropriate ways to model count data, see the Hermite and Poisson Regression for
Count Data chapter in this book.
Robust techniques
Standard parametric analyses can be sensitive to outliers and other deviations from assumptions of
the analyses. The example below shows that a single point can affect the predicted line from standard
linear regression. It is said that this point has high leverage, as so has high influence in the model.
569
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
A third way is to determine if certain data points are true outliers, and then give them further
examination.
A fourth way is to use a robust technique such as robust regression. The references below discuss
robust regression and robust techniques appropriate for designs lending themselves to an analysis of
variance approach.
“One-way Analysis with Permutation Test” in Mangiafico, S.S. 2015. An R Companion for the
Handbook of Biological Statistics, version 1.09. rcompanion.org/rcompanion/d_06a.html.
“Two-way Anova with Robust Estimation” in Mangiafico, S.S. 2015. An R Companion for the
Handbook of Biological Statistics, version 1.09. rcompanion.org/rcompanion/d_08a.html.
Data = read.table(textConnection(Input),header=TRUE)
Linear regression
model = lm(Y ~ X,
data = Data)
summary(model)
570
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -3.1364 3.6614 -0.857 0.413888
X 3.1182 0.6189 5.038 0.000701 ***
R2 = summary(model)$r.squared
plot(Y ~ X,
data = Data,
pch = 16)
abline(model,
col="blue",
lwd=2)
text(0, 38, labels = t1, pos=4)
text(0, 33, labels = t2, pos=4)
text(0, 28, labels = t3, pos=4)
text(0, 23, labels = t4, pos=4)
571
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Cook’s distance
Cook’s distance is a measure of influence of each data point, specifically determining how much
predicted values would change if the observation were deleted. It is sometimes recommended that a
Cook’s distance of 1 or more merits further examination of the data point. However, there are other
recommendations for critical values for Cook’s distance.
In this case, the high Cook’s distance for observation 11 makes it a candidate for further evaluation.
barplot(cooks.distance(model))
Plot of residuals
This plot suggests that the residuals are not independent of the fitted values. We would probably want
to adjust the model by adding additional terms, using a nonlinear or curvilinear approach, or using a
robust analysis to minimize the influence of certain data points.
plot(fitted(model),
residuals(model))
Robust regression
library(robustbase)
572
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
model.r = lmrob(Y ~ X,
data = Data)
summary(model.r)
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.09015 0.32804 0.275 0.79
X 2.04249 0.10970 18.619 1.7e-08 ***
R2 = summary(model.r)$r.squared
plot(Y ~ X,
data = Data,
pch = 16)
abline(model.r,
col="blue",
lwd=2)
text(0, 38, labels = t1, pos=4)
text(0, 33, labels = t2, pos=4)
text(0, 28, labels = t3, pos=4)
text(0, 23, labels = t4, pos=4)
573
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Contrasts can be used in linear models to compare groups of treatments to one another. For example,
if you were testing four curricula and wanted to compare the effect of One and Two vs. Three and Four,
you could accomplish this with a single-degree-of freedom contrast.
“Contrasts in Linear Models” in Mangiafico, S.S. 2015. An R Companion for the Handbook of
Biological Statistics, version 1.09. rcompanion.org/rcompanion/h_01.html.
Linear plateau and quadratic plateau models are segmented models for bivariate data, typically where
the y-value increases with an increase in x to some critical value, beyond which the y-value ceases to
increase. The statistics of interest include the critical value (the x-value above which there is no
further increase in y), and the plateau value (the statistically highest value that y reaches).
For another example of these models, see the “Use the best model” section of the Basic Plots chapter.
Also included in this section is an example of curvilinear regression, with a Mitscherlich–Bray model
fit to the same data.
574
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
Data$Instructor = factor(Data$Instructor,
levels=unique(Data$Instructor))
library(psych)
575
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
headTail(Data)
str(Data)
summary(Data)
rm(Input)
The code below starts with an attempt to find reasonable starting values for the parameters. If these
starting values don’t yield appropriate results, other starting values can be tried.
The fitted parameters a, b, and clx designate the best fit intercept, slope, and critical x value. The
plateau value is calculated as a + b * clx.
### Find reasonable initial values for parameters
a.ini = fit.lm$coefficients[1]
b.ini = fit.lm$coefficients[2]
clx.ini = mean(Data$Calories)
summary(model)
576
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Parameters:
Estimate Std. Error t value Pr(>|t|)
a -128.85535 116.10819 -1.11 0.273
b 0.65768 0.05343 12.31 1.6e-15 ***
clx 2326.51521 16.82086 138.31 < 2e-16 ***
m.ini = mean(Data$Sodium)
library(rcompanion)
nagelkerke(model,
null)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.195417
Cox and Snell (ML) 0.892004
Nagelkerke (Cragg and Uhler) 0.892014
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-2 -50.077 100.15 1.785e-22
confint2(model,
level = 0.95)
2.5 % 97.5 %
a -363.1711583 105.4604611
b 0.5498523 0.7654999
clx 2292.5693351 2360.4610904
577
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(nlstools)
Boot = nlsBoot(model)
summary(Boot)
------
Bootstrap statistics
Estimate Std. error
a -143.5189740 111.18292997
b 0.6643267 0.05132983
clx 2326.8124177 16.71482729
------
Median of bootstrap estimates and percentile confidence intervals
Median 2.5% 97.5%
a -143.5474796 -376.3224461 73.437548
b 0.6638483 0.5652069 0.771089
clx 2327.1279329 2292.5244830 2363.929178
plotPredy(data = Data,
x = Calories,
y = Sodium,
model = model,
xlab = "Calories per day",
ylab = "Sodium intake per day")
A plot of daily sodium intake vs. daily caloric intake for students. Best fit
linear plateau model is shown. Critical x value = 2327 calories, and Plateau y =
1401 mg. p < 0.0001. Pseudo R-squared (Nagelkerke) = 0.892.
578
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Plots of residuals
x = residuals(model)
library(rcompanion)
plotNormalHistogram(x)
plot(fitted(model),
residuals(model))
The fitted parameters a, b, and clx designate the best fit intercept, linear coefficient, and critical x value.
The quadratic coefficient is calculated as –0.5 * b / clx. The plateau value is calculated as a + b * clx –
0.5 * b * clx.
579
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
a.ini = fit.lm$coefficients[1]
b.ini = fit.lm$coefficients[2]
clx.ini = mean(Data$Calories)
summary(model)
Parameters:
Estimate Std. Error t value Pr(>|t|)
a -4217.7196 935.1139 -4.510 5.13e-05 ***
b 4.4920 0.8251 5.444 2.49e-06 ***
clx 2504.4059 48.9679 51.144 < 2e-16 ***
m.ini = mean(Data$Sodium)
library(rcompanion)
580
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
nagelkerke(model,
null)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.175609
Cox and Snell (ML) 0.864674
Nagelkerke (Cragg and Uhler) 0.864683
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-2 -45.001 90.003 2.8583e-20
confint2(model,
level = 0.95)
2.5 % 97.5 %
a -6104.855900 -2330.583368
b 2.826818 6.157185
clx 2405.584577 2603.227200
library(nlstools)
Boot = nlsBoot(model)
summary(Boot)
------
Bootstrap statistics
Estimate Std. error
a -4294.518746 956.6204011
b 4.560459 0.8448001
clx 2510.154733 53.3134728
------
Median of bootstrap estimates and percentile confidence intervals
Median 2.5% 97.5%
a -4250.030936 -6311.47965 -2546.971334
b 4.515874 3.03532 6.359601
clx 2504.795426 2425.75206 2626.813217
plotPredy(data = Data,
581
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
x = Calories,
y = Sodium,
model = model,
xlab = "Calories per day",
ylab = "Sodium intake per day")
A plot of daily sodium intake vs. daily caloric intake for students. Best fit
quadratic plateau model is shown. Critical x value = 2504 calories, and Plateau
y = 1403 mg. p < 0.0001. Pseudo R-squared (Nagelkerke) = 0.865.
Plots of residuals
x = residuals(model)
library(rcompanion)
plotNormalHistogram(x)
582
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
plot(fitted(model),
residuals(model))
Mitscherlich–Bray model
This model is an example of curvilinear regression, specifically with a general exponential model. The
model here is specified as y = a – be–cx.
In the function below, the constant D is set to the minimum of Calories + 1. This will be subtracted
from the x values because the numerically large values for Calories make the fitting of the exponential
function difficult without doing this subtraction.
### Find reasonable initial values for parameters
a.ini = 1400
b.ini = -120
c.ini = 0.01
D = 1974
expon = function(x, a, b, c) {
a + b * exp(-1 * c * (x - D))}
583
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
summary(model)
Parameters:
Estimate Std. Error t value Pr(>|t|)
a 1.450e+03 2.309e+01 62.790 < 2e-16 ***
b -2.966e+02 2.142e+01 -13.846 < 2e-16 ***
c 3.807e-03 7.973e-04 4.776 2.2e-05 ***
m.ini = mean(Data$Sodium)
library(rcompanion)
nagelkerke(model,
null)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.162515
Cox and Snell (ML) 0.842908
Nagelkerke (Cragg and Uhler) 0.842918
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-2 -41.646 83.292 8.1931e-19
confint2(model,
level = 0.95)
2.5 % 97.5 %
a 1.403060e+03 1.496244e+03
b -3.398171e+02 -2.533587e+02
c 2.198512e-03 5.416355e-03
584
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(nlstools)
Boot = nlsBoot(model)
summary(Boot)
------
Bootstrap statistics
Estimate Std. error
a 1.453109e+03 2.522906e+01
b -3.005815e+02 2.244473e+01
c 3.850955e-03 7.832523e-04
------
Median of bootstrap estimates and percentile confidence intervals
Median 2.5% 97.5%
a 1.449234e+03 1.415464e+03 1.513570e+03
b -2.998719e+02 -3.474264e+02 -2.566514e+02
c 3.827519e-03 2.390379e-03 5.410459e-03
plotPredy(data = Data,
x = Calories,
y = Sodium,
model = model,
xlab = "Calories per day",
ylab = "Sodium intake per day")
A plot of daily sodium intake vs. daily caloric intake for students. Best fit
exponential model is shown. p < 0.0001. Pseudo R-squared (Nagelkerke) = 0.843.
585
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Plots of residuals
x = residuals(model)
library(rcompanion)
plotNormalHistogram(x)
plot(fitted(model),
residuals(model))
586
ADVANCED PARAMETRIC METHODS SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Typically, I will put my data into a spreadsheet and make a plot of the function I am trying to fit. I can
then change parameter values and watch the curve change until I get the curve reasonably close to the
data.
The package minpack.lm has a function nlsLM, which uses the Levenberg–Marquardt method. It is
more successful at fitting parameters for difficult functions when the initial values for parameters are
poor. An example of using this package is shown in the “Fitting curvilinear models with the
minpack.lm package” below.
(The package nlmrt uses the Marquardt–Nash method, which is also tolerant of difficult functions and
poor starting values for parameters. However, at the time of writing, this package is not available for
newer versions of R.)
expon = function(x, a, b, c) {
a + b * exp(-1 * c * (x - D))}
summary(model)
Parameters:
Estimate Std. Error t value Pr(>|t|)
a 1.450e+03 2.309e+01 62.789 < 2e-16 ***
b -2.966e+02 2.142e+01 -13.846 < 2e-16 ***
c 3.807e-03 7.972e-04 4.776 2.2e-05 ***
Cate–Nelson analysis
Cate–Nelson analysis is a useful approach for some bivariate data that don’t fit neatly into linear
model, linear–plateau model, or curvilinear model. The analysis divides the data into two populations:
those x values that are likely to have high y values, and those x values that are likely to have low y
values.
“Cate–Nelson Analysis” in Mangiafico, S.S. 2015. An R Companion for the Handbook of Biological
Statistics, version 1.09. rcompanion.org/rcompanion/h_02.html.
library(rcompanion)
cateNelson(x = Data$Calories,
y = Data$Sodium,
plotit=TRUE,
hollow=TRUE,
xlab="Calories per day",
ylab="Sodium intake, mg per day",
trend="positive",
clx=1,
cly=1,
xthreshold=0.10,
ythreshold=0.15)
Final model:
A plot of daily sodium intake vs. daily caloric intake for students, with a Cate–
Nelson analysis applied. Students with a Caloric intake greater than 2210
calories tended to have a sodium intake greater than 1317 mg per day.
Transforming Data
Transforming data is one step in addressing data that do not fit model assumptions, and is also used to
coerce different variables to have similar distributions. Before transforming data, see the “Steps to
handle violations of assumption” section in the Assessing Model Assumptions chapter.
588
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Transforming data
Most parametric tests require that residuals be normally distributed and that the residuals be
homoscedastic.
One approach when residuals fail to meet these conditions is to transform one or more variables to
better follow a normal distribution. Often, just the dependent variable in a model will need to be
transformed. However, in complex models and multiple regression, it is sometimes helpful to
transform both dependent and independent variables that deviate greatly from a normal distribution.
There is nothing illicit in transforming variables, but you must be careful about how the results from
analyses with transformed variables are reported. For example, looking at the turbidity of water
across three locations, you might report, “Locations showed a significant difference in log-transformed
turbidity.” To present means or other summary statistics, you might present the mean of transformed
values, or back transform means to their original units.
Some measurements in nature are naturally normally distributed. Other measurements are naturally
log-normally distributed. These include some natural pollutants in water: There may be many low
values with fewer high values and even fewer very high values.
For right-skewed data—tail is on the right, positive skew—, common transformations include square
root, cube root, and log.
For left-skewed data—tail is on the left, negative skew—, common transformations include square
root (constant – x), cube root (constant – x), and log (constant – x).
Because log (0) is undefined—as is the log of any negative number—, when using a log transformation,
a constant should be added to all values to make them all positive before transformation. It is also
sometimes helpful to add a constant when using other transformations.
Another approach is to use a general power transformation, such as Tukey’s Ladder of Powers or a
Box–Cox transformation. These determine a lambda value, which is used as the power coefficient to
transform values. X.new = X ^ lambda for Tukey, and X.new = (X ^ lambda – 1) / lambda for Box–Cox.
The transformTukey function in the rcompanion package finds the lambda which makes a single vector
of values—that is, one variable—as normally distributed as possible with a simple power
transformation.
The Box–Cox procedure is included in the MASS package with the function boxcox. It uses a log-
likelihood procedure to find the lambda to use to transform the dependent variable for a linear model
(such as an ANOVA or linear regression). It can also be used on a single vector.
589
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
• car
• MASS
• rcompanion
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("car")}
if(!require(MASS)){install.packages("MASS")}
if(!require(rcompanion)){install.packages("rcompanion")}
This example uses hypothetical data of river water turbidity. Turbidity is a measure of how cloudy
water is due to suspended material in the water. Water quality parameters such as this are often
naturally log-normally distributed: values are often low, but are occasionally high or very high.
The first plot is a histogram of the Turbidity values, with a normal curve superimposed. Looking at the
gray bars, this data is skewed strongly to the right (positive skew), and looks more or less log-normal.
The gray bars deviate noticeably from the red normal curve.
The second plot is a normal quantile plot (normal Q–Q plot). If the data were normally distributed, the
points would follow the red line fairly closely.
Turbidity = c(1.0, 1.2, 1.1, 1.1, 2.4, 2.2, 2.6, 4.1, 5.0, 10.0, 4.0, 4.1, 4.2, 4.1,
5.1, 4.5, 5.0, 15.2, 10.0, 20.0, 1.1, 1.1, 1.2, 1.6, 2.2, 3.0, 4.0, 10.5)
library(rcompanion)
plotNormalHistogram (Turbidity)
qqnorm(Turbidity,
ylab="Sample Quantiles for Turbidity")
590
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
qqline(Turbidity,
col="red")
library(rcompanion)
plotNormalHistogram(T_sqrt)
591
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(rcompanion)
plotNormalHistogram(T_cub)
Log transformation
The log transformation is a relatively strong transformation. Because certain measurements in nature
are naturally log-normal, it is often a successful transformation for certain data sets. While the
transformed data here does not follow a normal distribution very well, it is probably about as close as
we can get with these particular data.
T_log = log(Turbidity)
library(rcompanion)
plotNormalHistogram(T_log)
592
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
The approach of Tukey’s Ladder of Powers uses a power transformation on a data set. For example,
raising data to a 0.5 power is equivalent to applying a square root transformation; raising data to a
0.33 power is equivalent to applying a cube root transformation.
Here, I use the transformTukey function, which performs iterative Shapiro–Wilk tests, and finds the
lambda value that maximizes the W statistic from those tests. In essence, this finds the power
transformation that makes the data fit the normal distribution as closely as possible with this type of
transformation.
Left skewed values should be adjusted with (constant – value), to convert the skew to right skewed,
and perhaps making all values positive. In some cases of right skewed data, it may be beneficial to add
a constant to make all data values positive before transformation. For large values, it may be helpful to
scale values to a more reasonable range.
In this example, the resultant lambda of –0.1 is slightly stronger than a log transformation, since a log
transformation corresponds to a lambda of 0.
### Some options require NCStats
library(rcompanion)
T_tuk =
transformTukey(Turbidity,
plotit=FALSE)
lambda W Shapiro.p.value
397 -0.1 0.935 0.08248
library(rcompanion)
plotNormalHistogram(T_tuk)
593
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Transforming the turbidity values to be more normally distributed, both improves the distribution of
the residuals of the analysis and makes a more powerful test, lowering the p-value.
Input =("
Location Turbidity
a 1.0
a 1.2
a 1.1
a 1.1
a 2.4
a 2.2
a 2.6
a 4.1
a 5.0
a 10.0
b 4.0
b 4.1
b 4.2
b 4.1
b 5.1
b 4.5
b 5.0
b 15.2
b 10.0
b 20.0
c 1.1
c 1.1
c 1.2
c 1.6
c 2.2
c 3.0
c 4.0
c 10.5
594
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
")
Data = read.table(textConnection(Input),header=TRUE)
library(car)
Anova(model, type="II")
x = (residuals(model))
library(rcompanion)
plotNormalHistogram(x)
qqnorm(residuals(model),
ylab="Sample Quantiles for residuals")
595
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
qqline(residuals(model),
col="red")
plot(fitted(model),
residuals(model))
Transform data
### Some options require NCStats
library(rcompanion)
Data$Turbidity_tuk =
transformTukey(Data$Turbidity,
plotit=FALSE)
596
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
lambda W Shapiro.p.value
397 -0.1 0.935 0.08248
library(car)
Anova(model, type="II")
597
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
x = residuals(model)
library(rcompanion)
plotNormalHistogram(x)
qqnorm(residuals(model),
ylab="Sample Quantiles for residuals")
qqline(residuals(model),
col="red")
plot(fitted(model),
residuals(model))
598
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Box–Cox transformation
The Box–Cox procedure is similar in concept to the Tukey Ladder of Power procedure described
above. However, instead of transforming a single variable, it maximizes a log-likelihood statistic for a
linear model (such as ANOVA or linear regression). It will also work on a single variable using a
formula of x ~ 1.
The Box–Cox procedure is available with the boxcox function in the MASS package. However, a few
steps are needed to extract the lambda value and transform the data set.
library(rcompanion)
plotNormalHistogram(Turbidity)
599
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
qqnorm(Turbidity,
ylab="Sample Quantiles for Turbidity")
qqline(Turbidity,
col="red")
Box.x Box.y
59 -0.2 -41.35829
library(rcompanion)
plotNormalHistogram(T_box)
600
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
601
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(car)
Anova(model, type="II")
x = residuals(model)
library(rcompanion)
plotNormalHistogram(x)
qqnorm(residuals(model),
ylab="Sample Quantiles for residuals")
qqline(residuals(model),
col="red")
602
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
plot(fitted(model),
residuals(model))
Transform data
library(MASS)
603
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Cox2[1,]
boxplot(Turbidity_box ~ Location,
data = Data,
ylab="Box–Cox-transformed Turbidity",
xlab="Location")
library(car)
Anova(model, type="II")
x = residuals(model)
library(rcompanion)
plotNormalHistogram(x)
604
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
qqnorm(residuals(model),
ylab="Sample Quantiles for residuals")
qqline(residuals(model),
col="red")
plot(fitted(model),
residuals(model))
605
TRANSFORMING DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Conclusions
Both the Tukey’s Ladder of Powers principle as implemented by the transformTukey function and the
Box–Cox procedure were successful at transforming a single variable to follow a more normal
distribution. They were also both successful at improving the distribution of residuals from a simple
ANOVA.
The Box–Cox procedure has the advantage of dealing with the dependent variable of a linear model,
while the transformTukey function works only for a single variable without considering other
variables. Because of this, the Box–Cox procedure may be advantageous when a relatively simple
model is considered. In cases where there are complex models or multiple regression, it may be
helpful to transform both dependent and independent variables independently.
606
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Count data
In general, common parametric tests like t-test and anova shouldn’t be used for count data. One
reason is technical in nature: that parametric analyses require continuous data. Count data is by its
nature discrete and is left-censored at zero. (That is, usually counts can’t be less than zero.)
A second reason is more practical in nature. Count data are often highly skewed, and often produce
skewed residuals if a parametric approach is attempted. In this case, the hypothesis tests will not be
accurate.
For further discussion, see the “Count data may not be appropriate for common parametric tests”
section in the Introduction to Parametric Tests chapter.
Cautionary note
Note that model assumptions and pitfalls of these regression techniques are not discussed in depth
here. The reader is urged to understand the assumptions of this kind of modeling before proceeding.
Generalized linear models are used when the dependent variable is count, binary, multinomial, etc.
More information on using the glm function can be found by using help(glm) and help(family). For
examples of logistic regression, see the chapter Models for Nominal Data; the chapter Beta Regression
for Percent and Proportion Data; or Mangiafico (2015) in the “References” section. For a table of
common uses for family and link function in generalized linear models, see the Wikipedia article in the
“References” section for this chapter.
607
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
• pysch
• hermite
• lattice
• plyr
• boot
• DescTools
• ggplot2
• car
• multcompView
• lsmeans
• MASS
• pscl
• rcompanion
• robust
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(hermite)){install.packages("hermite")}
if(!require(lattice)){install.packages("lattice")}
if(!require(plyr)){install.packages("plyr")}
if(!require(boot)){install.packages("boot")}
if(!require(DescTools)){install.packages("DescTools")}
if(!require(ggplot2)){install.packages("ggplot2")}
if(!require(car)){install.packages("car")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(MASS)){install.packages("MASS")}
if(!require(pscl)){install.packages("pscl")}
if(!require(rcompanion)){install.packages("rcompanion")}
if(!require(robust)){install.packages("robust")}
Hermite regression
Fitting models with the hermite package can be somewhat difficult. One issue is that model fitting may
fail without some parameters being specified. Often specifying an appropriate value for the m option
will help.
A further difficulty with this approach is that, at the time writing, the package isn’t supported by the
anova function to compare models, the Anova function to test effects, or other useful functions like
lsmeans for factor effects. The function nagelkerkeHermite can be used to compare models to test
effects.
608
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
In this example, extension researchers have set up garden plots with different suites of plants, with
each suite identified as a level of the variable Garden below. In September, they counted the number of
monarch butterflies in each garden plot.
Input = ("
Garden Monarchs
A 0
A 4
A 2
A 2
A 0
A 6
A 0
A 0
B 5
B 9
B 7
B 5
B 7
B 5
B 9
B 5
C 10
C 14
C 12
C 12
C 10
C 16
C 10
C 10
")
Data = read.table(textConnection(Input),header=TRUE)
Data$Garden = factor(Data$Garden,
levels=unique(Data$Garden))
library(psych)
headTail(Data)
str(Data)
summary(Data)
609
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
rm(Input)
Hermite regression
Here, the hermite package is used to conduct hermite regression. The nagelkerkeHermite function is
used to test the effect of Garden by likelihood ratio test, comparing the full model to null model with
the Garden effect excluded. It also produces a pseudo R-squared value for the model by comparison to
a null model.
Here, the m=3 option is specified. Often the default m=NULL can be used. In this case, if the m value is
not specified, the function cannot complete the model fitting, and errors are produced. Using m=2
often works. Here, m=3 was used because it produced a model with a lower AIC than did the m=2
option.
library(hermite)
summary(model)
null = glm.hermite(Monarchs ~ 1,
data = Data,
link = "log",
m=3)
library(rcompanion)
nagelkerkeHermite(model, null)
$Models
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.249943
Cox and Snell (ML) 0.766548
Nagelkerke (Cragg and Uhler) 0.768829
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-2 -17.457 34.915 2.6204e-08
$AIC
AIC
610
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Model: 112.78
Null: 143.69
### Note that because the null model is the same as the full model
### without the Garden effect, this test can also be considered a
### test of the Garden effect, and the pseudo R-squared akin to
### a partial pseudo R-squared due to the effect of Garden.
Histograms
library(lattice)
One imperfect approach for post-hoc analysis would be to examine median counts for treatments and
the confidence intervals of these medians. We can conclude that groups with non-overlapping 95%
confidence intervals for their medians are significantly different.
However, this approach does not represent any information learned from the Hermite regression.
A second issue is that, because the dependent variable is not continuous, the distribution of the
bootstrapped confidence intervals is not likely to be continuous, and so is may not be reliable.
611
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
To get confidence intervals for the medians for each group, we will use the groupwiseMedian function.
Here I used the percentile method for confidence intervals.
library(rcompanion)
Sum = groupwiseMedian(data=Data,
group="Garden",
var="Monarchs",
conf=0.95,
R=5000,
percentile=TRUE,
bca=FALSE,
digits=3)
Sum
annotate("text",
x = 1:3,
y = c(5, 10, 15),
label = c("Group 3", "Group 2", "Group 1"))
612
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(hermite)
null1 = glm.hermite(Monarchs ~ 1,
data = Data1,
link = "log",
m=3)
library(rcompanion)
nagelkerkeHermite(model1,
null1)
613
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-1 -6.7312 13.462 0.00024339
library(hermite)
null2 = glm.hermite(Monarchs ~ 1,
data = Data2,
link = "log",
m=3)
library(rcompanion)
nagelkerkeHermite(model2,
null2)
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-1 -13.42 26.84 2.2099e-07
library(hermite)
null3 = glm.hermite(Monarchs ~ 1,
data = Data3,
link = "log",
m=3)
library(rcompanion)
nagelkerkeHermite(model3,
null3)
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-1 -6.0386 12.077 0.00051042
p.adj = p.adjust(p.value,
614
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
method = "fdr")
p.adj
Poisson regression makes certain assumptions about the relationship between the mean and the
dispersion of the dependent variable. Because this assumption may not be met for all data sets,
Poisson regression may not be recommended for routine use. Particularly, classic Poisson regression
should be avoided if there is overdispersion in the data or if there are several zero counts in the
dependent variable.
An alternative approach for data with many zeros is zero-inflated Poisson regression.
For further discussion, see the “Count data may not be appropriate for common parametric tests”
section in the Introduction to Parametric Tests chapter.
Note that model assumptions and pitfalls of this approach are not discussed here. The reader is urged
to understand the assumptions of this kind of modeling before proceeding.
model.p = glm(Monarchs ~ Garden,
data=Data,
family="poisson")
library(car)
Anova(model.p,
type="II",
test="LR")
LR Chisq Df Pr(>Chisq)
Garden 66.463 2 3.697e-15 ***
library(rcompanion)
nagelkerke(model.p)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.387929
Cox and Snell (ML) 0.937293
Nagelkerke (Cragg and Uhler) 0.938037
$Likelihood.ratio.test
615
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model.p,
pairwise ~ Garden,
adjust="tukey") ### Tukey adjustment for comparisons
leastsquare
cld(leastsquare,
alpha=0.05,
Letters=letters, ### Use lower-case letters for .group
adjust="tukey") ### Tukey adjustment for multiple comparisons
Negative binomial regression is similar in application to Poisson regression, but allows for
overdispersion in the dependent count variable.
This example will use the glm.nb function in the MASS package. The Anova function in the car package
will be used for an analysis of deviance, and the nagelkerke function will be used to determine a p-
value and pseudo R-squared value for the model. Post-hoc analysis can be conducted with the lsmeans
package.
Note that model assumptions and pitfalls of this approach are not discussed here. The reader is urged
to understand the assumptions of this kind of modeling before proceeding.
library(MASS)
616
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(car)
Anova(model.nb,
type="II",
test="LR")
LR Chisq Df Pr(>Chisq)
Garden 66.464 2 3.694e-15 ***
Library(rcompanion)
nagelkerke(model.nb)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.255141
Cox and Snell (ML) 0.776007
Nagelkerke (Cragg and Uhler) 0.778217
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-2 -17.954 35.907 1.5952e-08
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model.nb,
pairwise ~ Garden,
adjust="tukey") ### Tukey adjustment
leastsquare
cld(leastsquare,
alpha=0.05,
Letters=letters, ### Use lower-case letters for .group
adjust="tukey") ### Tukey adjustment for multiple comparisons
617
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Zero-inflated regression is similar in application to Poisson regression, but allows for an abundance of
zeros in the dependent count variable.
This example will use the zeroinfl function in the pscl package. The Anova function in the car package
will be used for an analysis of deviance, and the nagelkerke function will be used to determine a p-
value and pseudo R-squared value for the model. Post-hoc analysis can be conducted with the lsmeans
package.
library(pscl)
summary(model.zi)
Call:
zeroinfl(formula = Monarchs ~ Garden | Garden, data = Data, dist = "poisson")
library(car)
Anova(model.zi,
type="II",
test="Chisq")
Df Chisq Pr(>Chisq)
Garden 2 23.914 6.414e-06 ***
618
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(rcompanion)
nagelkerke(model.zi)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.284636
Cox and Snell (ML) 0.797356
Nagelkerke (Cragg and Uhler) 0.800291
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-4 -19.156 38.311 9.6649e-08
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model.zi,
pairwise ~ Garden,
adjust="tukey") ### Tukey adjustment
leastsquare
cld(leastsquare,
alpha=0.05,
Letters=letters, ### Use lower-case letters for .group
adjust="tukey") ### Tukey adjustment for multiple comparisons
This example uses the glmRob function in the robust package. The anova function can be used to
conduct an analysis of deviance. The p-value for the model can be found by comparing the model to a
null model. However, at the time of writing, I don’t know of any way to determine AIC or pseudo R-
squared for the model.
At the time of writing, the glmRob function can only use the Poisson and binomial families of models.
619
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
anova(model.rob, test="Chisq")
model.rob.null = glmRob(Monarchs ~ 1,
data = Data,
family = "poisson")
Quasi-Poisson regression
Quasi-Poisson regression is useful since it has a variable dispersion parameter, so that it can model
over-dispersed data. It may be better than negative binomial regression in some circumstances
(Verhoef and Boveng. 2007).
At the time of writing, Quasi-Poisson regression doesn’t have complete set of support functions in R.
Using the quasipoisson family option in the glm function, the results will have the same parameter
coefficients as with the poisson option, but the inference statistics are adjusted in the summary
function. The Anova function in the car package can be used for an analysis of deviance table, and the
lsmeans package can be used for post-hoc comparisons. Since the model doesn’t produce a log-
likelihood value, I don’t know a way to produce a p-value for the mode, for a pseudo R-squared value
for the model.
.
model.qp = glm(Monarchs ~ Garden,
data=Data,
family="quasipoisson")
library(car)
Anova(model.qp,
type="II",
test="LR")
620
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Response: Monarchs
LR Chisq Df Pr(>Chisq)
Garden 52.286 2 4.429e-12 ***
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model.qp,
pairwise ~ Garden,
adjust="tukey")
leastsquare
cld(leastsquare,
alpha=0.05,
Letters=letters,
adjust="tukey")
An alternative approach to handling count data is to sum up the counts for treatments, and use a chi-
square test or related test. Here, a chi-square goodness-of-fit test is used to see if counts differ from
“expected” equal proportions.
Omnibus test
Tabla = xtabs(Monarchs ~ Garden,
data = Data)
Tabla
Garden
A B C
14 52 94
chisq.test(Tabla)
621
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
chisq.test(x = observed,
p = expected)
chisq.test(x = observed,
p = expected)
chisq.test(x = observed,
p = expected)
Optional analysis: Vuong test to compare Poisson, negative binomial, and zero-
inflated models
The Vuong test, implemented by the pscl package, can test two non-nested models. It works with
negbin, zeroinfl, and some glm model objects which are fitted to the same data.
The null hypothesis is that there is no difference in models. The function produces three tests, a “Raw”
test, an AIC-corrected, and a BIC-corrected, any of which could be used.
622
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
It has been suggested that the Vuong test not be used to test for zero-inflation (Wilson, 2015).
Define models
model.p = glm(Monarchs ~ Garden,
data=Data,
family="poisson")
library(MASS)
library(pscl)
Vuong test
library(pscl)
vuong(model.p,
model.nb,
digits = 4)
vuong(model.p,
model.zi,
digits = 4)
623
HERMITE AND POISSON REGRESSION FOR COUNT DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
vuong(model.nb,
model.zi,
digits = 4)
References
Moriña, D., M. Higueras, P. Puig, and M. Oliveira. 2015. Generalized Hermite Distribution
Modelling with the R Package hermite. The R Journal 7(2):263–274. journal.r-
project.org/archive/2015-2/morina-higueras-puig-etal.pdf.
help(package="hermite")
library(hermite); ?glm.hermite
library(MASS); ?glm.nb
library(pscl); ?zeroinfl
library(pscl); ?vuong
“Simple Logistic Regression” in Mangiafico, S.S. 2015. An R Companion for the Handbook of
Biological Statistics, version 1.09. rcompanion.org/rcompanion/e_06.html.
"Generalized linear model: Link function". No date. Wikipedia. Retrieved 31 Jan. 2016.
en.wikipedia.org/wiki/Generalized_linear_model#Link_function.
Verhoef, J.M. and P.L. Boveng. 2007. Quasi-Poisson vs. negative binomial regression: How
should we model overdispersed count data? Ecology 88(11) 2766–2772.
http://fisher.utstat.toronto.edu/reid/sta2201s/QUASI-POISSON.pdf.
624
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Wilson, P. 2015. The Misuse of the Vuong Test for Non-Nested Models to Test for Zero-Inflation.
Economic Letters 127: 51–53. cybermetrics.wlv.ac.uk/paperdata/misusevuong.pdf
Grace-Martin, K. No date. "Regression Models for Count Data". The Analysis Factor.
www.theanalysisfactor.com/regression-models-for-count-data/.
Grace-Martin, K. No date. " Zero-Inflated Poisson Models for Count Outcomes". The Analysis Factor.
www.theanalysisfactor.com/zero-inflated-poisson-models-for-count-outcomes/.
[IDRE] Institute for Digital Research and Education. 2015. “R Data Analysis Examples: Poisson
Regression”. UCLA. www.ats.ucla.edu/stat/r/dae/poissonreg.htm.
[IDRE] Institute for Digital Research and Education. 2015. “R Data Analysis Examples: Negative
Binomial Regression”. UCLA. www.ats.ucla.edu/stat/r/dae/nbreg.htm.
[IDRE] Institute for Digital Research and Education. 2015. “R Data Analysis Examples: Zero-Inflated
Poisson Regression”. UCLA. www.ats.ucla.edu/stat/r/dae/zipoisson.htm.
[IDRE] Institute for Digital Research and Education. 2015. “R Data Analysis Examples: Zero-Truncated
Poisson Regression”. UCLA. www.ats.ucla.edu/stat/r/dae/ztp.htm.
Proportion data
In general, common parametric tests like t-test and anova shouldn’t be used when the dependent
variable is proportion data, since proportion data is by its nature bound at 0 and 1, and is often not
normally distributed or homoscedastic.
Data of proportions, percentages, and rates can be thought of as falling into a few different categories.
625
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
If the numerator can be considered a count variable, Poisson regression or other methods for count
data are usually suggested. As a complication, often the denominator varies in value. For example, if
the count is disease occurrence in a city and the denominator is the population in a city. (Each city has
a different population.) Or if the numerator is the count of televisions in the home and the
denominator is the number of students in the home. In these cases, Poisson regression or related
methods are often recommended with an offset for the value in the denominator. These examples are
not explored further here, but an example model would be
glm(Count_of_televisions ~ Independent_variable +
offset(log(Number_of_students_in_home)),
family="poisson",
data=Data)
Beta regression
Beta regression can be conducted with the betareg function in the betareg package (Cribari-Neto and
Zeileis, 2010). With this function, the dependent variable varies between 0 and 1, but no observation
can equal exactly zero or exactly one. The model assumes that the data follow a beta distribution.
The nagelkerke function in the rcompanion package also works with beta regression objects. The
likelihood ratio test there appears to work fine, but the results for pseudo R-squared may be squirrelly,
and probably should not be relied upon.
626
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Final note
Note that model assumptions and pitfalls of this approach are not discussed here. The reader is urged
to understand the assumptions of this kind of modeling before proceeding.
The following commands will install these packages if they are not already installed:
if(!require(psych)){install.packages("psych")}
if(!require(betareg)){install.packages("betareg")}
if(!require(car)){install.packages("car")}
if(!require(lmtest)){install.packages("lmtest")}
if(!require(rcompanion)){install.packages("rcompanion")}
if(!require(multcompView)){install.packages("multcompView")}
if(!require(lsmeans)){install.packages("lsmeans")}
if(!require(ggplot2)){install.packages("ggplot2")}
The following example uses counts of students passing or failing an exam, and asks if there is a
relationship between the proportion of passing students and Grade. Note that Grade is treated as a
numeric variable in this analysis.
Input = ("
Class Grade Pass Fail
'Bully Hill' 12 14 6
'Keuka Lake' 12 15 5
'Heron Hill' 11 18 2
'Castel Grisch' 11 10 10
'Red Newt' 10 17 3
'Finger Lakes' 10 9 11
'Bellview' 9 12 8
'Auburn Road' 9 8 12
'Balic' 8 10 10
'Cape May' 8 8 12
'Hawk Haven' 7 12 8
'Natali' 7 4 16
")
Data = read.table(textConnection(Input),header=TRUE)
627
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
library(car)
Anova(model.beta,
type="II")
Response: Proportion
Df Chisq Pr(>Chisq)
Grade 1 7.3314 0.006776 **
628
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(lmtest)
lrtest(model.beta)
summary(model.beta)
library(rcompanion)
plotPredy(data = Data,
y = Proportion,
x = Grade,
model = model.beta,
xlab = "Grade",
ylab = "Proportion passed")
plot(fitted(model.beta),
residuals(model.beta))
629
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(car)
Anova(model.log,
type="II",
test="Wald")
Df Chisq Pr(>Chisq)
Grade 1 14.305 0.0001554 ***
library(rcompanion)
nagelkerke(model.log)
$Pseudo.R.squared.for.model.vs.null
Pseudo.R.squared
McFadden 0.190924
Cox and Snell (ML) 0.717212
Nagelkerke (Cragg and Uhler) 0.718174
630
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
$Likelihood.ratio.test
Df.diff LogLik.diff Chisq p.value
-1 -7.5784 15.157 9.8946e-05
library(rcompanion)
plotPredy(data = Data,
y = Percent,
x = Grade,
model = model.log,
type = "response", # Needed for logistic regression
xlab = "Grade",
ylab = "Proportion passing")
plot(fitted(model.beta),
residuals(model.beta))
This example revisits the data set from the chapter on two-way analysis of variance. Here, the
dependent variable Proportion is created by dividing daily student sodium intake by the US FDA
“upper safe limit” of 2300 mg. The rest of the analysis is analogous to that of a two-way ANOVA.
Input = ("
Instructor Supplement Sodium
'Brendon Small' A 1200
'Brendon Small' A 1400
'Brendon Small' A 1350
'Brendon Small' A 950
'Brendon Small' A 1400
'Brendon Small' B 1150
'Brendon Small' B 1300
'Brendon Small' B 1325
'Brendon Small' B 1425
'Brendon Small' B 1500
'Brendon Small' C 1250
'Brendon Small' C 1150
'Brendon Small' C 950
'Brendon Small' C 1150
'Brendon Small' C 1600
'Brendon Small' D 1300
'Brendon Small' D 1050
'Brendon Small' D 1300
'Brendon Small' D 1700
'Brendon Small' D 1300
'Coach McGuirk' A 1100
'Coach McGuirk' A 1200
'Coach McGuirk' A 1250
'Coach McGuirk' A 1050
631
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Data = read.table(textConnection(Input),header=TRUE)
Data$Instructor = factor(Data$Instructor,
levels=unique(Data$Instructor))
Data$Supplement = factor(Data$Supplement,
levels=unique(Data$Supplement))
632
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(psych)
headTail(Data)
str(Data)
summary(Data)
rm(Input)
Beta regression
library(betareg)
library(car)
Anova(model,
type="II")
Df Chisq Pr(>Chisq)
Instructor 2 14.9873 0.0005566 ***
Supplement 3 15.3766 0.0015216 **
Instructor:Supplement 6 1.1985 0.9769603
library(lmtest)
lrtest(model)
633
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
summary(model)
plot(fitted(model),
residuals(model))
library(multcompView)
library(lsmeans)
leastsquare = lsmeans(model,
pairwise ~ Instructor,
adjust = "tukey")
leastsquare
Sum = cld(leastsquare,
alpha = 0.05,
Letters = letters, ### Use lowercase letters for .group
adjust = "tukey") ### Tukey-adjusted comparisons
Sum
634
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Sum = Sum[order(factor(Sum$Instructor,
levels=c("Brendon Small",
"Coach McGuirk",
"Melissa Robins"))),]
Sum
library(ggplot2)
annotate("text",
x = 1:3,
y = Sum$asymp.UCL + 0.008,
label = gsub(" ", "", Sum$.group))
635
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
leastsquare = lsmeans(model,
pairwise ~ Supplement,
adjust = "tukey")
leastsquare
Sum = cld(leastsquare,
alpha = 0.05,
Letters = letters, ### Use lowercase letters for .group
adjust = "tukey") ### Tukey-adjusted comparisons
Sum
Sum = Sum[order(factor(Sum$Supplement,
levels=c("A", "B", "C", "D"))),]
Sum
636
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
library(ggplot2)
annotate("text",
x = 1:4,
y = Sum$asymp.UCL + 0.008,
label = gsub(" ", "", Sum$.group))
References
637
BETA REGRESSION FOR PERCENT AND PROPORTION DATA SUMMARY AND ANALYSIS OF EXTENSION EDUCATION PROGRAM
EVALUATION IN R
Cribari-Neto, F. and Zeileis, A. 2010. Beta Regression in R. Journal of Statistical Software 34(2).
www.jstatsoft.org/article/view/v034i02/v34i02.pdf.
638