Skip to content

Posts tagged ‘Rstats’

How to post R code on Wordpress blogs


Most WordPress BloggeRs are using this text highlight syntax, some are not.
I hope that this post would be a reference source for new WordPress BloggeRs for posting their R code on their blog posts.

According to an official guide by WordPress.com on “Posting Source Code“, To post R code in the WordPress.com, just wrap R code as follows (without “#” in both wrappers):

######################################################

[#sourcecode language="r"]
Your R code and comments
x <- rnorm(100)
y <- x + 10
[#/sourcecode]

######################################################

  • From above, before your R code put the command in line 1, or [#sourcecode language=”r”]but without #
  • Then, place your R code (line 2-4).
  • End the code box by put the command line as in line  5, but without # or “[/sourcecode]

Then the code will appear as following.

Your R code and comments
x <- rnorm(100)
y <- x + 10

Moreover, more options can be configured to better describe the code efficiently.

  • autolinks (true/false)
    TRUE: Makes all URLs in your posted code clickable.
    Defaults: TRUE
  • collapse (true/false) 
    TRUE: The code box will be collapsed when the page loads, requiring the visitor to click to expand it.
    Comment: Good for large code posts.
    Defaults: False.
  • firstline (number)
    Comments: Use this to change what number the line numbering starts at
    Defaults =  1
  • gutter (true/false)
    TRUE:  Show the line numbering on the left hand side.
    FALSE: The line numbering on the left side will be hidden.
    Defaults = TRUE
  • highlight (comma-seperated list of numbers)
    You can list the line numbers you want to be highlighted.
    Example = “4,7,19″.
  • htmlscript (true/false)
    TRUE: Any HTML/XML in your code will be highlighted.
    Comment: This is useful when you are mixing code into HTML, such as PHP inside of HTML.
    Defaults = FALSE (only work with certain code languages)
  • light (true/false)
    TRUE: The gutter (line numbering) and toolbar (see below) will be hidden.
    Comment: This is helpful when posting only one or two lines of code.
    Defaults = FALSE
  • padlinenumbers (true/false/integer)
    TRUE: Automatic padding
    FALSE: No padding, and entering a number will force a specific amount of padding.
    Comment: Allows you to control the line number padding.
  • toolbar (true/false)
    FALSE: The toolbar containing the helpful buttons that appears when you hover over the code will not be shown.
    Defaults = TRUE
  • wraplines (true/false)
    FALSE: Line wrapping will be disabled. This will cause a horizontal scrollbar to appear for long lines of code.

If you are using WordPress.org, here is a plugin.

Update: I just found a nice post by William K. Morris on How to update your WordPress.com blog from R

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

R packages for Structural Equation Model: SEM with R


Structural Equation Model (SEM) was first examined by a software called LISREL. Then, SEM has been mainly run by several proprietary software i.e., Mplus, AMOS, EQS, SAS and a new version of Stata (v.12).

However, you may also run SEM with a great but free software like R.

To the best of my knowledge, there are now four active packages that you can use to fit SEM. Here they are:

Main Packages (for fitting SEM models) 

  1. sem (John Fox, 2006):The first R package for SEM ” fit by maximum likelihood assuming multinormality, and single-equation estimation for observed-variable models by two-stage least.squares.” It was also the first package I tried to run SEM in R. Thanks to a very quick response from Prof.Fox on my question I emailed him.
    See Example of ‘sem’ package here.
  2. OpenMx (Boker et al, 2011)
    A very active package that “is 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.
    See Example of ‘OpenMx’ package here.
  3. lavaan (Yves Rosseel, 2012)
    A promising package for SEM. Its command language is similar to those of Mplus. Hence it is perhaps the most user-friendly package for SEM to date.
    See Example of ‘lavaan’ package here.
    Link to JSS paper
  4. semPLS (Armin Monecke, 2012)
    Fitting Structural Equation Model Using Partial Least Squares
    See: CRAN link, JSS paper
  5. plspm (Gaston Sanchez, 2012)
    R package dedicated to Partial Least Squares (PLS) methods (CRAN, plsmodeling.com)
    by Gaston Sanchez and Laura Trinchera
    A corresponding book titled “PLS Path Modeling with R” can be downloaded here.
My paper in useR! 2011 has evaluated R packages vs. Proprietary software i.e., AMOS & Lisrel.

Today (30 May 2012), I gladly found that there are also complementary packages for SEM in R as follows.

Complementary packages

  • SEMplusR: Functions, examples and datasets to learn, use and teach Structural Equation Modeling (SEM)  [GitHub]
    by Pairach Piboonrungroj 
  • SEMModComp: Model Comparisons for SEM [CRAN link, Additional Documents]
    by  Roy Levy
  • semGOF: an add-on package which provides fourteen goodness-of-fit indeces for structural equation models using ‘sem’ package.[CRAN]
    by Elena Bertossi 
  • stremo: Functions to help the process of learning structural equation modelling [CRAN link]
    by  Gustavo Carvalho, Marco Batalha, and Owen Petchey
  • FIAR: Functional Integration Analysis in R [CRAN link]
    by  Bjorn Roelstraete
  • semTools: Useful tools for structural equation modeling [CRAN link]
    by  Sunthud Pornprasertmanit, Patrick Miller, Alex Schoemann, Yves Rosseel
  • simsem: SIMulated Structural Equation Modeling [CRAN link]
    by  Sunthud Pornprasertmanit, Patrick Miller, Alexander Schoemann
  • pathmox R package dedicated to segmentation trees in PLS Path Modeling [CRAN, plsmodeling.com]

Packages for SEM plotting and graphics

  • qgraph: Network representations of relationships in data [CRAN link]
    by  Sacha Epskamp, Angelique O. J. Cramer, Lourens J. Waldorp, Verena D. Schmittmann and Denny Borsboom
  • psych: Procedures for Psychological, Psychometric, and Personality Research [CRAN link]
    by William Revelle

Packages that link R with other software to fit SEM

  • Mplus
    Automating Mplus Model Estimation and Interpretation [CRAN link]
    by  Michael Hallquist
  • EQS
    R/EQS Interface [CRAN link]
    by  Patrick Mair and Eric Wu

More external resources on SEM in R

  • CRAN Task view on ‘Structural Equation Models, Factor Analysis, PCA’ in Psychometrics [url]
    by Patrick Mair
  • A tutorial on the use of sem package  [url]
    by William Revelle
  • A post on ‘Structural Equation Modeling in R‘  [url]
    by Jeromy Anglim
%d bloggers like this: