Skip to content

Posts from the ‘Software’ Category

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

Useful resources for writing C++ code in Mac using Xcode


During 18-20 April 2012 I am attending the course on “C++ for beginner”. The course mainly based on Window OS. To remind myself I here posted some useful link for C++ in Mac using Xcode.

Tips

How to run an executable file

  • In Xcode, an executable file is in the product folder below the main folder (the one that contains main.cpp file) in the left-hand side section.
  • The file name is the project name (without any extension).
  • Here are the step to run the file
  1. Right click on the file -> select “Open in finder”
  2. Launch the Terminal
  3. Type “cd” in the Terminal and Drag the folder that contains the file to the Terminal
  4. Type “./” and the project name  followed by the requirement for the program

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

How to licence your thesis using Creative Commons


One of the main objectives of PhD thesis (dissertation) is to advance the subject by making a contribution. Traditionally theses have been licensed under the copyright where all rights are reserved. And I also did put “All rights reserved” on the cover page of my thesis when I first drafted it. But just today when I realised that I should replace it by a “Creative Commons” one which could enhance the contributions and the impacts of my thesis.

Therefore I adopted the CC BY-NC-ND licence.  Then others are free to copy, distribute and transmit the work with some conditions.

To do so, I inserted the artwork of the By-NC-ND licence and put the sentence ” This thesis is licensed under a Creative Commons Attribution-Non Commercial-No Derivs 3.0 Unported License.” with the link to Creative Commons site that explain the licence.

1. Choose a licence you will use for your thesis here

2. Download the button of the licence you here and save the image in the LaTeX script folder of your thesis. I named my image as cc-by-nc-nd.png

3. Put the following LaTeX code on the title page of your thesis

line 06: Place your licence file name. Mine is “cc-by-nc-nd.png”.
line 10: Place the URL link to your licence and its description.

LaTeX code:

%Insert Creative Commons Artwork
\DeclareGraphicsExtensions{.pdf,.png,.jpg}
\begin{center}
\leavevmode
%Insert image file name below "cc-by-nc-nd.png"
\includegraphics[width=1in]{cc-by-nc-nd.png}
\end{center}
\label{fig:cc}
%insert a link to the licence and its description below
\scriptsize{This thesis is licensed under a \href{http://creativecommons.org/licenses/by-nc-nd/3.0/}{Creative Commons Attribution-Non Commercial-No Derivs 3.0 Unported License.}}

4. Finally you will get the following on the title page of your thesis.

Back to LaTeX tips