คู่มือการวิเคราะห์ทางสถิติ และ เศรษฐมิติประยุกต์ ด้วยโปรแกรม R
- คู่มือวิเคราะห์สถิติและเศรษฐมิติประยุกต์ด้วยโปรแกรม R (ฉบับภาษาไทย)
- ASER: Applied Statistics and Econometrics with R (English Version)
Jun 20
Jun 20
Arguably, knitr (CRAN link) is the most outstanding R package of this year and its creator, Yihui Xie is the star of the useR! conference 2012. This is because the ease of use comparing to Sweave for making reproducible report. Integration of knitR and R Studio has made reproducible research much more convenience, intuitive and easier to use.
This post is an example, based on the demo by Yihui Xie himself, I will show how to create a reproducible report consisting the R code in a LaTeX style in the Cardiff R User Group session at the Cardiff Business School (CARBS) Research Fair tomorrow (19 June 2012).
A chuck of R code is wrapped in the following code:
<<chunk1, echo=TRUE, results='hide'>>= Put your R code here @
chunk1 is the name of the chuck
echo = TRUE to show your R code in the chuck, = FALSE if you do not want to show the code.
result = ‘markup’ to show the result unless = ‘hide’
\documentclass[11pt, a4paper]{article}
\usepackage{amsfonts, amsmath, hanging, hyperref, natbib, parskip, times}
\hypersetup{
colorlinks,
linkcolor=blue,
urlcolor=blue
}
\setlength{\topmargin}{-15mm}
\setlength{\oddsidemargin}{-2mm}
\setlength{\textwidth}{165mm}
\setlength{\textheight}{250mm}
\let\section=\subsubsection
\newcommand{\pkg}[1]{{\normalfont\fontseries{b}\selectfont #1}}
\let\proglang=\textit
\let\code=\texttt
\renewcommand{\title}[1]{\begin{center}{\bf \LARGE #1}\end{center}}
\newcommand{\affiliations}{\footnotesize}
\newcommand{\keywords}{\paragraph{Keywords:}}
\begin{document}
\pagestyle{empty}
\title{Using knitR (R + \LaTeX) in R Studio: A Demo}
\begin{center}
{\bf Pairach Piboonrungroj$^{1,2,^\star}$}
\end{center}
\begin{affiliations}
1. Logistics Systems Dynamics Group, Cardiff Business School, Cardiff University, United Kingdom \\[-2pt]
2. Chiang Mai School of Economics, Chiang Mai University, Thailand \\[-2pt]
%3. Second affiliation of author B \\[-2pt]
$^\star$Email: \href{mailto:me@pairach.com}{me@pairach.com}
\end{affiliations}
\vskip -0.5cm
%%%%%%%%%%%%%%%%%%
%Add Breaking Line
\begin{center}
\linethickness{1mm}
\line(1,0){480}
\end{center}
%%%%%%%%%%%%%%%%%%
1. Show only R source code
<<chunk1, echo=TRUE, results='hide'>>=
1 + 1
@
2. Show only output
<<chunk2, ref.label='chunk1', echo=FALSE, results='markup'>>=
@
3. Show both source code and output
<<chunk3, echo=TRUE, results='markup'>>=
1 + 1
@
4. Show source code in grey shade but the output
<<chunk4, echo=TRUE, results='asis'>>=
1 + 1
@
5. Now, testing a linear model
<<chunk5, echo=TRUE, results='markup'>>=
# generating value for x variable from 1 to 100
x <- c(1:100)
# creat error term
e <- rnorm(100, mean = 5, sd = 10000)
# computing y equal to 3 plus five times x plus random number
y = 10 + 100*x + e
@
Set the format of all object called pdf()
<<custom-dev2>>=
my_pdf = function(file, width, height) {pdf(file, width = 5, height = 5, pointsize = 10)}
@
6. See the scatter plot
<<chunk6, echo=TRUE, results='markup', dev='my_pdf', fig.ext='pdf'>>=
plot(x, y)
@
7. Let's build a linear model by regressing y on x
<<chunk7, echo=TRUE, results='markup'>>=
# creating a linear model by regressing y on x as 'lm1' object
lm1 <- lm(y ~ x)
# calling a summary of linear model result
summary(lm1)
@
8. Now we can create a post-hoc plots to check assumptions of regression
<<chunk8, echo=TRUE, results='markup', dev='my_pdf', fig.ext='pdf'>>=
# Creating post-hoc plot for lm1
par(mfrow=c(2,2))
plot(lm1)
@
\end{document}
According to the post on FREE online R tutorials from universities, I have received many email suggesting more and more tutorials. However some tutorials are not hosted in an academic institutes, so I decided to create this post to list such tutorials. If you know other tutorials, please kindly suggest me by email to me@pairach.com or post the link in the comment section.
The tutorials are listed in no particular order but categorised by subjects and/or topics.
Jun 15
Recently British government (by Office of National Statistics: ONS) just published their version of R manual for analysis of the government survey. The links to PDF and MS word versions of the manual including the R syntax are as below.
Note: The R syntax link is not working now. I am contacting the ONS, hope they will fix it soon.
![]() |
The R Guide to ESDS Large-Scale Government Surveys PDF, Word |
|||
For the US governemnt, there is an emerging awareness and recognition of the power of R in their Big Data Initiative. David Smith (Revolution Analytics) has summarised the application of R in the US governemnt in his post here.