test knitr

Upload: pairach-piboonrungroj

Post on 05-Apr-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Test KnitR

    1/3

    Using knitR (R + LATEX) in R Studio: A Demo

    Pairach Piboonrungroj1,2,

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

    2. Chiang Mai School of Economics, Chiang Mai University, ThailandEmail: [email protected]

    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

  • 7/31/2019 Test KnitR

    2/3

    0 20 40 60 80 100

    30000

    10000

    0

    10000

    30000

    x

    y

    7. Lets build a linear model by regressing y on x# creating a linear model by regressing y on x as 'lm1' object

    lm1 |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

    ##

  • 7/31/2019 Test KnitR

    3/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)

    2000 4000 8000

    30000

    0

    20000

    Fitted values

    Residuals

    Residuals vs Fitted

    2

    98

    46

    2 1 0 1 2

    3

    1

    1

    2

    3

    Theoretical Quantiles

    Standardizedresiduals

    Normal QQ

    2

    98

    46

    2000 4000 80000

    .0

    0.5

    1.0

    1.5

    Fitted values

    S

    tandardizedresiduals ScaleLocation

    29846

    0.00 0.02 0.04

    3

    1

    1

    3

    Leverage

    S

    tandardizedresiduals

    Cook's distance

    Residuals vs Leverage

    2

    987