Skip to content

Posts from the ‘R’ Category

Measuring Emergency Relief Performance of Thailand Floods in 2011


Background

Last year Thailand faced the worst floods in their history. The World Bank (2011) reported that the 2011 Thailand floods were “The biggest damages and losses were in the manufacturing sector, with a total of THB 1,007 Bn (USD 32 Bn approximately)”. The tourism and agricultural sector were also affected and losses approximately THB 95 Bn (USD 3 Bn) and THB 40 Bn (USD 1.3 Bn) respectively (The World bank, 2011).

Therefore, this study aims to evaluate the performance of organisations that took actions in emergency relief for this disaster. We measure performance based on their preparedness for such disaster as well as how they responded to the events. Data collected via GoogleDoc from 382 respondents (victims) were (A) explored and the (B) used to fit with the conceptual model of performance measurement for emergency relief logistics.

A. Preliminary Findings

  • Descriptive_GoogleDoc (in Thai)
    A chart below shows the proportion of organisations, which samples have got helps.
    Highest percentage is unknown organisation, followed by military agencies.
  • Interesting Info-graphic obtained using R

What we can get from R

Moving away from the basic barplot instantly provided by Google Doc, we can do a better job to visualise what  people rate the performance of each organisation in terms of preparedness and response to the floods. An we can do so via R. Followings are what I produced in R.

1. Level Preparedness 

# Boxplot preparedness performance

k <- ggplot(flood, aes(factor(org), aPRE))

k + geom_jitter(aes(colour = org), size = 4) + opts(legend.title = theme_text(size = 20, face = "bold"), legend.text = theme_text(size = 10)) + opts(title = "Level of Perceived Preparedness of each Organisation") + opts(plot.title = theme_text(size = 15, face="bold", colour = "blue"))

2. Level of Responses

# Boxplot response perforance

c <- ggplot(flood, aes(factor(org), aRES))

c + geom_jitter(aes(colour = org), size = 4) + opts(legend.title = theme_text(size = 20, face = "bold"), legend.text = theme_text(size = 10)) + opts(title = "Level of Perceived Response of each Organisation") + opts(plot.title = theme_text(size = 15, face="bold", colour = "blue"))
<pre>

B. Theoretical Output: BAM2012 Paper

Title: Developing Measurement of Emergency Relief Logistics and Operations Performance: An Empirical Study of Thailand Floods in 2011

Summary

Albeit emergency relief logistics is an emerging field in operations, logistics and supply chain management, the development of performance measurement is still limited. Although one of the objectives of emergency relief logistics is to satisfy customers (victims in the disaster) the development of performance measurement based on victim’s perspective is limited. Then this study propose a measurement of emergency relief logistics performance and tested with an empirical data from the Thailand floods in 2011. We fit the propose measurement model with the data of 382 respondents using Confirmatory Factor Analysis with Mplus version 6 and R version 2.14.1. The result shows that the model is fit with the data. It was found that response had the highest contribution to the total performance, followed by preparedness and recovery respectively. The result also shows that information, operations and evacuation have different contribution to the performance of each stage.

Keywords: Humanitarian logistics, Emergency relief, disaster, floods, Thailand

Download: pdf

R code used in the paper

</pre>
library(lavaan)
 flood1.cfa <-'

PRE =~ x411 + x412 + x413 + x414 + x415 + x416
 + x421 + x422 + x423 + x424 + x425
 + x431 + x432 + x433 + x434

RES =~ x511 + x512 + x513 + x514 + x515 + x516
 + x521 + x522 + x523 + x524 + x525 + x526 + x527
 + x531 + x532 + x533 + x534 + x536

REC =~ x611 + x612 + x613 + x614 + x615 + x616 + x617
 + x621 + x622 + x623 + x624 + x625 + x626 + x627
 + x631 + x632 + x633 + x634 + x636

PEF =~ PRE + RES + REC

'
 fitFlood1 <- cfa(flood1.cfa, data=flooddata)
 summary(fitFlood1, standardized=TRUE, fit.measures=TRUE)
<pre>

How to develop R packages [resources]


One of the advantages of R is its add-on packages which are now 3,759 packages freely available in CRAN (May 1, 2012). I have been using R for my research since 2010. These packages have brought me to R. It is also the main reason why I am mainly use R (90%) for my research and teaching. Great packages like ggplot2 or iplot (for graphics) as well as sem or lavaan for (Structural Equation Model) are the good examples why many people are migrating to R.

Hence, I also would like to contribute to R more by writing some packages that would be useful to my subject areas (Economics, Supply Chain Management and Tourism). I wish I can have three packages (hopefully called econ, scm, and tour) that contains data sets, functions that help researchers, teachers and students in my fields benefit the uses of R in the future.

As I have no experience in writing any software or computer package before, I have search around the Internet and as usual there are lots of stuffs on how to create a package in R. Followings are the list of resources I found. Hope that one who also want to write an package will find the list useful.

==================================================================================================

*UPDATE (23 Oct 2012): I just found a new feature of R Studio for package development (with RStudio v0.97 or higher). I believe that this is gonna be a big hit soon.

Useful resources for creating R packages / extensions

General guides / tutorials

  1. Writing R Extensions [url]
    by R Core Team
  2. Creating R Packages: A Tutorial [pdf, 19 pages]
    by Friedrich Leisch
  3. R development master class [San Francisco, Canberra]
    by Hadley Wickham 
  4. Creating R Packages, Using CRAN, R-Forge, And Local R Archive Networks And Subversion (SVN) Repositories (pdf, ppt, 2009-05-04, 45 slides)
    by Spencer Graves and Sundar Dorai-Raj
  5. How To Make an R Package Based on C++ And Manage It With R-Forge: A Tutorial [pdf]
    by Jose M. Maisog
  6. Creating an R package, using developer/productivity tools [url, Youtube (1:28:53)]
    by Szilard Pafka and Jeroen Ooms
  7. R Package Writing Workshop
    [Youtube (1:02:28), Talk+Slide (slideshare),Download slide (Vcasmo) ]
    by Rory Winston
  8. Creating R packages (url)
    by Nunes
  9. Building R packages for PC, Mac, and Linux or Unix [GitHub wiki]
    by  Christopher Adolph
  10. Building R packages [45 presentation slides, pdf]
    by Derek Young
  11. Create R package from command line prompts [Git Hub]
    by  milktrader
  12. Tips for R Package Creation
    by Tyler Rinker

For Mac useRs

  1. R for Mac OS X Developer’s Page – Building R [url]
    by AT&T
  2. Making R packages for the Mac: A simplified guide [url]
    by William Revelle

For Windows PC useRs

  1. Building R packages for Windows [url]
    by Rob J Hyndman
  2. Building R packages in Windows [url]
    by Karl W Broman

For Linux useRs

  1. Building Microsoft Windows Versions of R and R packages under Intel Linux [pdfMakefile]
    by Jun Yan and A. J. Rossini

Related posts

OpenMx package for Structural Equation Model in R


OpenMx (Boker et al, 2011) is claimed to be a “ free and open source software for use with R that allows estimation of a wide variety of advanced multivariate statistical models.” contributed by experts in R and SEM.

The following is my OpenMx script I used in my presentation at R useR! 2011.

##-------------------------------------------------##
##    Measuring Transaction Cost in Supply Chains  ##
##               Pairach Piboonrungroj             ##
##         R useR conference August 2011           ##
##-------------------------------------------------##

#install OpenMx
source('http://openmx.psyc.virginia.edu/getOpenMx.R')
require(OpenMx)

# load the OpenMx package into R

library(OpenMx)

# read the data into an R dataframe

hoteldata <- read.csv("http://dl.dropbox.com/u/46344142/useR2011/cleandata.csv")

# define which indicators load on each factor

indicatorsTC <- c("TC1", "TC2", "TC3", "TC6", "TC7" , "TC11" , "TC13")

indicatorsAS <- c("AS1", "AS2")

indicatorsUN <- c("UN1", "UN2")

# create a vector of all of the manifest variables

manifests <- c(indicatorsTC, indicatorsAS, indicatorsUN)

# define which indicator is to be used to scale each factor

scaleTC <- c("TC1")

scaleAS <- c("AS1")

scaleUN <- c("UN1")

# define the names of the factors

latents <- c("TC", "AS", "UN")

# define the MxModel and store it into "factorModel"

factorModel <- mxModel("TC Model",

type="RAM",

manifestVars = manifests,

latentVars = latents,

# specify the free factor loadings

mxPath(from="TC", to=indicatorsTC, free=TRUE, values=.2),

mxPath(from="AS", to=indicatorsAS, free=TRUE, values=.2),

mxPath(from="UN", to=indicatorsUN, free=TRUE, values=.2),

# scale the two latent variables

mxPath(from="TC", to=scaleTC, free=FALSE, values=1),

mxPath(from="AS", to=scaleAS, free=FALSE, values=1),

mxPath(from="UN", to=scaleUN, free=FALSE, values=1),

# specify the unique variances

mxPath(from=manifests, arrows=2, free=TRUE, values=.8),

# specify the factor variances

mxPath(from=latents, arrows=2, free=TRUE, values=.8),

# specify the path

mxPath(from="AS", to="TC", arrows=2, free=TRUE, values=.3),

mxPath(from="UN", to="TC", arrows=2, free=TRUE, values=.3),

mxPath(from="AS", to="UN", arrows=2, free=TRUE, values=.3),

# specify the mean structure

mxPath(from="one", to=c(manifests, latents), arrows=1, free=FALSE, values=0),

# attach the data to the model

mxData(hoteldata, type="raw")

)

# run the factor model

factorModelOut <- mxRun(factorModel)

# print a summary of the results

summary(factorModelOut)
<pre>

See more details about the model and the comparison with other R packages and software

lavaan package for Structural Equation Model in R


lavaan is a package for running a “Structural Equation Models” or SEM in R. The script of lavaan is similar to Mplus, which is very comprehensive and compact. The lavaan package is developed by Yves Rosseel.

The following is my lavaan script I used in my presentation at R useR! 2011.

##-------------------------------------------------##
##    Measuring Transaction Cost in Supply Chains  ##
##               Pairach Piboonrungroj             ##
##         R useR conference August 2011           ##
##-------------------------------------------------##
# 1. Load data
hoteldata &lt;- read.csv(&quot;https://www.dropbox.com/s/fzh040xkhofozjy/cleandata.csv&quot;)
# 2. Install Package
install.packages(&quot;lavaan&quot;)
# 3. Load Package
library(lavaan)
#5. Structural Model
TC.Model &lt;- '
# latent variable definitions
cost =~ TC1 + TC2 + TC3 +TC6 + TC7   +TC11  + TC13
asset =~ AS1 + AS2
uncertainty  =~ UN1 + UN2
# regression
cost ~ uncertainty + asset
# resident (co)variance
TC1 ~~ TC3
TC6 ~~ TC13
TC11 ~~ TC13
TC13 ~~ AS1
'
fitTC &lt;- sem(TC.Model, data = hoteldata)
summary(fitTC, standardized = TRUE, fit.measures=TRUE)
# Coefficients
# coef(fitTC)
#More indices
# Fit indices e.g. CFI
fit.indices &lt;- inspect(fitTC, what = &quot;fit&quot;)
fit.indices[&quot;cfi&quot;]
# Calling Modification Indices
# inspect(fitTC, what = &quot;mi&quot;)

See more details about the model and the comparison with other R packages and software

R-Uni (A List of 100 Free R Tutorials and Resources in University webpages)


Free 100 Online tutorials for R programming, Statistics and Graphics

Here is a list of FREE R tutorials hosted in official website of universities around the world. The tutorials are listed  in no particular order, actually based on when I have discovered it. They will be categorised soon. Please kindly suggest me other university-hosted online R tutorials by email to me@pairach.com.

A list of R tutorials, which are not hosted in the webpages of academic institutes can be found here.

  1. University of Oxford
     Modern Applied Statistics with S, 4edn (On-line material)[url]
    by  W. N. Venables and B. D. Ripley
  2. University of California at Davis
    Getting Started with the R Data Analysis Package [url]
    by Norm Matloff
  3. Clarkson University
    R Tutorial [url]
    by Kelly Black
  4. York University
    Getting started with R [url]
  5. University of Waterloo
    R Tutorial For  Windows and  Unix Environment [url]
  6. University of California at Los Angles, UCLA
    Resources to help you learn and use R [url]
  7. University of California at Riverside
    Programming in R [url]
  8. University of Illinois
    A Brief Introduction to R [url]
  9. University of Texas at Austin
    R Tutorial Videos [url]
    by Brandon K. Vaughn
  10. University of California at Berkeley
    An Introduction to R (pdf)
    by Phil Spector
  11. Chiang Mai University
    Econometrics with R (in Thai) [url]
    by Pairach Piboonrungroj
  12. University of California at Santa Babara
    R Programming Resource Centre [url]
    by National Center for Ecological Analysis and Synthesis
  13. University of Carnegie Mellon
    A Tutorial: Some Fundamentals of R [url]
    by Bruce E. Trumbo
  14. University of Illinois State
    R Tutorial [url]
    by Dong-Yun Kim
  15. University of MacMaster
    Introduction to the R Statistical Computing Environment [url]
    by John Fox.
  16. University of Princeton
    Introducing R [url]
    by Germán Rodríguez
  17. University of Amsterdam
    How to draw graphs with R (Graphics) [url]
    by Angelos-Miltiadis Krypotos
  18. University of North Texas
    Do it yourself – Introduction to R (Intro) [url]
  19. University of Warwick
    R programming page [url]
    (Biosciences: Molecular Organisation and Assembly in Cells)
    by Peter Cock.
  20. University of Illinois at Urbana-Champaign
    R tutorial for Applied Econometrics [url]
    by Prof. Roger Koenker
  21. Coastal Carolina University
    R tutorials (General) [url]
    by  William B. King
  22. University of Colorado Denver
    R Tutorial (General)[url]
    by  Stephanie Santorico and Mark Shin
  23. Stanford School of Medicine (Biomedical Informatics)
    R Tutorial (VDO on Introduction + Translational Bioinformatics (url)
  24. Harding University
    Producing Simple Graphs with R (basic graphic e.g., line, bar, hist, line) (url)
    by Frank McCown
  25. University of Kentucky, Department of Statistics
    Use Software R to do Survival Analysis and Simulation [pdf]
    by Mai Zhou
  26. University of Pittsburgh, Department of Statistics
    Time Series Analysis and Its Applications: With R Examples[url]
    by R.H. Shumway & D.S. Stoffer
  27. University of Toronto
    R Tutorial (for Ecology) [url]
    by the Cadotte Lab
  28. Florida State University
    Use R for Climate research [url]
    by James B. Elsner & Thomas H. Jagger
  29. University of Washington
    Introduction to R [url]
    by  Jinyoung Kim
  30. Lancaster University (UK)
    R tutorial [url]
    by  Joe Whittaker
  31. University of Georgia Athens
    A short R tutorial [pdf]
    by Steven M. Holland
  32. University of Twente
    R tutorials [url]
    (more than 1o tutorials about R in pdf + data & code)
    by D G Rossiter
  33. Vrijie University Amsterdam, Netherlands
    Handleiding R (in Dutch) [pdf]
    by A.W. van der Vaart
  34. University of Wisconsin-Madison
    Introduction to R [url]
    by Karl W Broman
  35. City University of New York
    simpleR Using R for Introductory Statistics [url]
    by  John Verzani
  36. Pomona College
    An R tutorial [pdf]
    by Jo Hardin
  37. University of Alaska Fairbanks 
    Using R [url]
    by the Biology & Wildlife Department computer
  38. University of Bath
    Practical Regression and Anova using R [pdf]
    Julian J. Faraway
  39. University of Goettingen
    Time Series Analysis with R – Part I [pdf]
    by Walter Zucchini and Oleg Nenadíc
    Statistical Analysis with R – a quick start – [pdf]
    by  Oleg Nenadíc and Walter Zucchini
  40. University of Washington, Department of Economics
    Working with Time Series Data in R [pdf]
    by Eric Zivot
  41. University of Liège, Faculty of Engineering
    Using R for Linear regression [pdf, 9 pages]
    by Kristel Van Steen
  42. University of Cambridge
    Local tips for R [url]  – in both statistics & graphics
    by Rudolf Cardinal
  43. Montana State University, Department of Mathematical Sciences
    Introduction to Sweave (R+LaTeX) [url]
    by Jim Robison-Cox
  44. Stanford University
    Social Network Analysis in R [url] including source R files and outputs
    by  McFarland, Daniel A., Solomon Messing, Michael Nowak and Sean J. Westwood.
  45. Ludwig-Maximilians-University Munich (Germany)
    Sweave (R + LaTeX) [url] including manuals and example files
    by Friedrich Leisch
  46. Ecole Polytechnique Fédérale de Lausanne
    Sweave = R • LaTeX^2 (pdf, slides)
    by Nicola Sartori
  47. University of Auckland
    Four course on Statistical Computing and Graphics including document, R codes (pdf)
    by Ross Ihaka
  48. Nova Southeastern University
    Many R tutorials using Tegrity-based video tutorials (url)
    by  Thomas W. MacFarland
  49. Penn State University
    – Introduction to R (get started) [url]
    – Welcome to STAT 497C – Topics in R Statistical Language! [url]
  50. Carnegie Mellon University
    Open & Free Course on Statistics (with applications in R among other software) [url]
  51. Centre for Mathematical Sciences, Lund University
    Exception handling in R [url]
    Object-oriented programming with references using S3/UseMethod [url]
    by Henrik Bengtsson
  52. University of Edinburgh
    Data Mining and Exploration (with examples with R) [url]
    by  Charles Sutton
  53. University of Washington
    Survey analysis in R [url]
    by Thomas Lumley
  54. Department of Psychology, University of Pennsylvania
    Notes on the use of R for psychology experiments and questionnaires [url]
    by Jonathan Baron and Yuelin Li
  55. College of Staten Island, Department of Mathematics
    simpleR: Using R for Introductory Statistics [url]
    By John Verzani
  56. Vanderbilt University
    rApache Manual [url]
  57. University of Aarhus, Department of Computer Science – Daimi, Faculty of Science
    RPy — R from Python [url]
  58. University of Auckland
    Many documents and links about R [url]
    By Paul  Murrell
  59. National Institute of Genetics (Japan)
    R Graphical Manual [url]
    By Osamu Ogasawara and IMS Lab Inc. Japan
  60. Montana State University
    R Labs for Vegetation Ecologists [url]
  61. University of Auckland
    Introduction to Data Technologies: Basic introduction to a number of computer technologies for working with data (HTML, XML, Databases, SQL, regular expressions, and R) [url]
    by Paul  Murrell
  62. Department of Statistics and Actuarial Science, SFU
    Technical Notes on the R programming language [url]
    by Sigal Blay
  63. University of Vienna
    Stattconn Project [url]
    by Thomas Baier and Erich Neuwirth
  64. Gunma University (Japan)
    R による統計処理  [url]
    by Shigenobu AOKI
  65. Tsukuba Office, Agriculture, Forestry and Fisheries Research Council Secretariat
    R Tips (in Japancese) [url]
  66. University of Western Ontario, Department of Statistical & Actuarial Sciences
    Debugging in R [url]
    by Duncan Murdoch
  67. University of Sunderland
    Programming in R [url]
    by Harry Erwin
  68. Smith College, Department of Mathematics and Statistics
    Use of R as a toolbox for mathematical statistics exploration [url]
    by Nicholas J. HortonElizabeth R.Brown and Linjuan Qian
  69. Katholieke Universiteit Leuven 
    R and Statistics [url]
    by Guido Wyseure
  70. Iowa State University
    Introduction to R [url]
    by Di Cook
  71. Cambridge University
    Graph redesign in R [url]
    by Stephen J. Murdoch
  72. Trinity College Dublin
    R Podcasts [url]
    by Andrew Jackson
  73. University of Arizona
    Introduction to R [url]
    by Caroline R.H. Wiley
  74. Cambridge University
    ESS: Emacs Speak Statistics [url]
    by Stephen Eglen
  75. Columbia University
    Running WinBugs and OpenBugs from R [url]
    Andrew Gelman
  76. University of Bremen
    vim R plugin for Linux/Unix [url]
    Johannes Ranke
  77. Montana State University
    R Web: Statistical Analysis on the web. [url]
    by Jeff Banfield
  78. University of Florida
    R for Categorical data analysis [html]
    by  Brett D. Presnell
  79. University of Auckland 
    An S (and R) Programming Workshop in 2003 (including slides, data and codes) [html]
    by The Quartet (affiliations at that moment)
    (1) Bill Venables (CSIRO, Australia)
    (2) Robert Gentleman (Harvard University)
    (3) Ross Ihaka (University of Auckland)
    (4) Paul Murrell (University of Auckland)
  80. Australian National University , Mathematical Science Institute
    Data Analysis and Graphics Using R – An Example-Based Approach [url]
    by John Maindonald and John Braun
  81. The Hebrew University
    Introduction to Statistical Thinking (With R, Without Calculus) [pdf]
    by Benjamin Yakir
  82. Basel Institute for Clinical Epidemiology and Biostatistics
    Tutorial: ggplot2 [pdf, 14 p.]
    by Ramon Saccilotto, Universitätsspital Basel
  83. UC, Davis: Bioinformatics
    Data Analysis and Visualization Course, 2012 (links to many sessions during two days of the course)
    by  Vince Buffalo, Joe Fass, Jie Peng and Dawei Lin
  84. University of Warwick
    R useR! Conference 2011 (links to materials and documents of the tutorials, invited talks and presentations)
    by  Department of Statistics, University of Warwick
  85. Vanderbilt University 
    R useR! Conference 2012 (links to materials and documents of short courses, tutorials, invited talks and presentations)
    by Department of Statistics, Vanderbilt University
  86. Technische Universität Wien, Vienna, Austria 
    useR! 2004: The 5th International R useRs Conference (links to materials of the conference)
    DSC 1999: The 1st conference for the developers of statistical software and researchers in statistical computing
    DSC 2001: The 2nd conference for the developers of statistical software and researchers in statistical computing
    DSC 2003: The 3rd conference for the developers of statistical software and researchers in statistical computing
    by  Department of Statistics and Probability Theory,Technische Universität Wien
  87. The University of Auckland
    DSC 2007: The 5th conference for the developers of statistical software and researchers in statistical computing
    (Workshops, Program, papers and posters)
    by Department of Statistics, The University of Auckland
  88. University of Oxford
    APTS Computer-Intensive Statistics 2012 (html) – Lecture note, datasets and codes
    by Prof. Brain Ripley
  89. Simon Fraser University
    Technical Notes on the R programming language
    by Sigal Blay
  90. University of Florida
    Introducing Monte Carlo Methods with R (pdf)
    by Christian P. Robert George Casella
  91. University of Tennessee
    pbdR — Programming with Big Data in R (html)
    Remote Data Analysis and Visulization Center
  92. National Center for Ecological Analysis and Synthesis
    The Regents of the University of California
    Integrate C Language Functions into R With an R Package
    – R: A self-learn tutorial
  93. Ghent University
    Teaching materials (slides, codes and datasets) for using lavaan package for Structural Equation Modeling (link)
    by Yves Rosseel
  94. Calvin College
    Computational Statistics Using R and R Studio – An Introduction for Scientists (pdf)
    by Randall Pruim
  95. Department of Mathematical Sciences
    Aalborg University, Denmark
    Graphical Models with R (pdf)
    Soren Hojsgaard
  96. Statistics Department
    Carnegie Mellon University
    Writing R Functions (pdf)
    by Cosma Shalizi
  97. Institute for Environmental Sciences
    University Koblenz-Landau
    Lecture “Applied Multivariate Statistics” 2011/2012 (url)
    by Ralf Schäfer
  98. University of Groningen, The Netherlands
    R Tutorial for Applied Statistics (URL)
    by Anne Boomsma
  99. University of Florida
    Tutorial on Making Simple R Packages (URL)
  100. University of Göttingen – Georg-August-Universität Göttingen
    Time Series Analysis with R – Part I (PDF)
    by Walter Zucchini, Oleg Nenadi´c
  101. Harvard University
    Quick Introduction to Graphics in R Introduction to the R language (pdf)
    by Aedin Culhane
  102. University of Pennsylvania
    R Study Group (General guide to R) (URL)
    Josef Fruehwald