R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(lme4)
## 載入需要的套件:Matrix
library(lmerTest)
## 
## 載入套件:'lmerTest'
## 下列物件被遮斷自 'package:lme4':
## 
##     lmer
## 下列物件被遮斷自 'package:stats':
## 
##     step
library(performance)
library(ggplot2)

## Import the data
library(haven)
hsb1 <- read_sav("HSB/hsb1.sav")
View(hsb1)

## Select a school
hsb_c<-hsb1[hsb1$schoolid=="3881",]
View(hsb_c)

## Regression of uncentered predictor
fit.ses<-lm(mathach ~ ses, data=hsb_c)
summary(fit.ses)
## 
## Call:
## lm(formula = mathach ~ ses, data = hsb_c)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.9596  -5.9037  -0.8132   5.1900  12.6608 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   11.644      1.055   11.03 1.47e-13 ***
## ses            2.391      1.943    1.23    0.226    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.568 on 39 degrees of freedom
## Multiple R-squared:  0.03737,    Adjusted R-squared:  0.01268 
## F-statistic: 1.514 on 1 and 39 DF,  p-value: 0.2259
library(lm.beta)
std.fit.ses<-lm.beta(fit.ses)
summary(std.fit.ses)
## 
## Call:
## lm(formula = mathach ~ ses, data = hsb_c)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.9596  -5.9037  -0.8132   5.1900  12.6608 
## 
## Coefficients:
##             Estimate Standardized Std. Error t value Pr(>|t|)    
## (Intercept)  11.6441           NA     1.0553   11.03 1.47e-13 ***
## ses           2.3907       0.1933     1.9430    1.23    0.226    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.568 on 39 degrees of freedom
## Multiple R-squared:  0.03737,    Adjusted R-squared:  0.01268 
## F-statistic: 1.514 on 1 and 39 DF,  p-value: 0.2259
library(psych)
## 
## 載入套件:'psych'
## 下列物件被遮斷自 'package:ggplot2':
## 
##     %+%, alpha
describe(fit.ses$residuals)
##    vars  n mean   sd median trimmed  mad    min   max range skew kurtosis   se
## X1    1 41    0 6.49  -0.81   -0.18 7.77 -10.96 12.66 23.62 0.22    -1.19 1.01
library(car)
## 載入需要的套件:carData
## 
## 載入套件:'car'
## 下列物件被遮斷自 'package:psych':
## 
##     logit
durbinWatsonTest(fit.ses)
##  lag Autocorrelation D-W Statistic p-value
##    1       0.1596718      1.608378   0.162
##  Alternative hypothesis: rho != 0
ncvTest(fit.ses)
## Non-constant Variance Score Test 
## Variance formula: ~ fitted.values 
## Chisquare = 0.04004184, Df = 1, p = 0.8414
ggplot(fit.ses, aes(x = ses, y=mathach)) + geom_point(size=3) + stat_smooth(method="lm")
## `geom_smooth()` using formula = 'y ~ x'

##regression of centered ses
sesdev<-hsb_c$ses-mean(hsb_c$ses)
fit.ses_c<-lm(mathach~sesdev, data=hsb_c)
summary(fit.ses_c)
## 
## Call:
## lm(formula = mathach ~ sesdev, data = hsb_c)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.9596  -5.9037  -0.8132   5.1900  12.6608 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   11.949      1.026   11.65 2.86e-14 ***
## sesdev         2.391      1.943    1.23    0.226    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.568 on 39 degrees of freedom
## Multiple R-squared:  0.03737,    Adjusted R-squared:  0.01268 
## F-statistic: 1.514 on 1 and 39 DF,  p-value: 0.2259
library(lm.beta)
std.fit.ses_c<-lm.beta(fit.ses_c)
summary(std.fit.ses_c)
## 
## Call:
## lm(formula = mathach ~ sesdev, data = hsb_c)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -10.9596  -5.9037  -0.8132   5.1900  12.6608 
## 
## Coefficients:
##             Estimate Standardized Std. Error t value Pr(>|t|)    
## (Intercept)  11.9492           NA     1.0257   11.65 2.86e-14 ***
## sesdev        2.3907       0.1933     1.9430    1.23    0.226    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.568 on 39 degrees of freedom
## Multiple R-squared:  0.03737,    Adjusted R-squared:  0.01268 
## F-statistic: 1.514 on 1 and 39 DF,  p-value: 0.2259
library(psych)
describe(fit.ses_c$residuals)
##    vars  n mean   sd median trimmed  mad    min   max range skew kurtosis   se
## X1    1 41    0 6.49  -0.81   -0.18 7.77 -10.96 12.66 23.62 0.22    -1.19 1.01
library(car)
durbinWatsonTest(fit.ses_c)
##  lag Autocorrelation D-W Statistic p-value
##    1       0.1596718      1.608378   0.162
##  Alternative hypothesis: rho != 0
ncvTest(fit.ses_c)
## Non-constant Variance Score Test 
## Variance formula: ~ fitted.values 
## Chisquare = 0.04004184, Df = 1, p = 0.8414
ggplot(fit.ses_c, aes(x = sesdev, y=mathach)) + geom_point(size=3) + stat_smooth(method="lm")
## `geom_smooth()` using formula = 'y ~ x'

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.