You are on page 1of 3

Using knitR (R + L A T E X) in R Studio: A Demo

Pairach Piboonrungroj 1,2,

1. Logistics Systems Dynamics Group, Cardiff Business School, Cardiff University, United Kingdom

2. Chiang Mai School of Economics, Chiang Mai University, Thailand

1. Show only R source code

1 +

1

2. Show only output

##

[1]

2

3. Show both source code and output

1

+

1

##

[1]

2

4. Show source code in grey shade but the output

1 +

1

[1] 2

5.

Now, testing a linear model

 

#

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()

my_pdf

=

function(file,

width,

height)

{

 

pdf(file,

width

=

5,

height

=

5,

pointsize

=

10)

}

6.

See the scatter plot

plot(x,

y)

30000

10000

y 0

−10000

−30000

30000 10000 y 0 −10000 −30000 0 20 40 60 80 100 x 7. Let’s build
30000 10000 y 0 −10000 −30000 0 20 40 60 80 100 x 7. Let’s build
30000 10000 y 0 −10000 −30000 0 20 40 60 80 100 x 7. Let’s build
30000 10000 y 0 −10000 −30000 0 20 40 60 80 100 x 7. Let’s build

0

20

40

60

80

100

x

7. Let’s build a linear model by regressing y on x

# creating a linear model by regressing y on x as 'lm1' object lm1 <-
# 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)
##
##
Call:
##
lm(formula
=
y
˜
x)
##
##
Residuals:
##
Min
1Q
Median
3Q
Max
##
-27498
-5830
-323
6265
24228
##
##
Coefficients:
##
Estimate
Std.
Error
t
value
Pr(>|t|)
##
(Intercept)
-3457.7
2027.2
-1.71
0.09124
.
##
x
132.8
34.9
3.81
0.00024
***
##
---
##
Signif.
codes:
0
' *** '
0.001
' ** '
0.01
'
* '
0.05
'.'
0.1
'
'
1
##
##
Residual
standard
error:
10100
on
98
degrees
of
freedom
##
Multiple
R-squared:
0.129,Adjusted
R-squared:
0.12
##
F-statistic:
14.5
on
1
and
98
DF,
p-value:
0.000242
##

Standardized residuals

3

2

1

−1

−3

8. Now we can create a post-hoc plots to check assumptions of regression

#

Creating

post-hoc

plot

for

lm1

par(mfrow

=

c(2,

2))

plot(lm1)

Residuals vs Fitted

98 46 2 −2000 4000 8000 Residuals −30000 0 20000
98
46
2
−2000
4000
8000
Residuals
−30000
0
20000

Fitted values

Normal Q−Q

98 46 2 −2 −1 0 1 2
98
46
2
−2
−1
0
1
2

Theoretical Quantiles

Scale−Location Residuals vs Leverage 2 98 98 7 46 Cook's distance 2 −2000 4000 8000
Scale−Location
Residuals vs Leverage
2
98
98 7
46
Cook's distance
2
−2000
4000
8000
0.00
0.02
0.04
Fitted values
Leverage
Standardized residuals
0.0
0.5
1.0
1.5
Standardized residuals
−3
−1
1
3