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)
## Warning: 套件 'lme4' 是用 R 版本 4.3.1 來建造的
## 載入需要的套件:Matrix
library(lmerTest)
## Warning: 套件 'lmerTest' 是用 R 版本 4.3.1 來建造的
## 
## 載入套件:'lmerTest'
## 下列物件被遮斷自 'package:lme4':
## 
##     lmer
## 下列物件被遮斷自 'package:stats':
## 
##     step
library(performance)
library(ggplot2)
## Warning: 套件 'ggplot2' 是用 R 版本 4.3.1 來建造的
## Import the data
library(haven)
## Warning: 套件 'haven' 是用 R 版本 4.3.1 來建造的
hsb <- read_sav("C:/Users/Tingyen/Desktop/HLM/Data/HSB/hsb1.sav")
View(hsb)

##select a school
hsb1 <- hsb[hsb$schoolid =="2458",]
View(hsb1)

## Column means
sub.dat <- hsb1[, c("ses", "mathach")]
head(sub.dat)  # just to confirm I have the right variables
## # A tibble: 6 × 2
##      ses mathach
##    <dbl>   <dbl>
## 1 -0.048   21.4 
## 2  1.30    14.0 
## 3  0.402   19.2 
## 4 -1.06     7.81
## 5 -0.178    9.83
## 6  0.232   22.6
colMeans(sub.dat)
##        ses    mathach 
##  0.2277895 13.9856842
##Descriptive stat
summary(sub.dat) 
##       ses             mathach      
##  Min.   :-1.0580   Min.   : 2.454  
##  1st Qu.:-0.0680   1st Qu.: 9.490  
##  Median : 0.2620   Median :13.071  
##  Mean   : 0.2278   Mean   :13.986  
##  3rd Qu.: 0.7220   3rd Qu.:19.161  
##  Max.   : 1.5120   Max.   :24.488
## Correlation matrices
cor(sub.dat)  # correlation matrix
##               ses   mathach
## ses     1.0000000 0.3328597
## mathach 0.3328597 1.0000000
## Creating new variables
sub.dat$sesXmath <- sub.dat$ses * sub.dat$mathach
head(sub.dat)
## # A tibble: 6 × 3
##      ses mathach sesXmath
##    <dbl>   <dbl>    <dbl>
## 1 -0.048   21.4     -1.03
## 2  1.30    14.0     18.2 
## 3  0.402   19.2      7.70
## 4 -1.06     7.81    -8.27
## 5 -0.178    9.83    -1.75
## 6  0.232   22.6      5.25
# Regression with lm using the full dataset without considering nesting structure
# Coefficients, standard errors, and t-test results
fit.ses <- lm(mathach ~ ses, data = hsb1)
summary(fit.ses)
## 
## Call:
## lm(formula = mathach ~ ses, data = hsb1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -11.278  -4.400  -1.284   4.880   9.240 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  13.3122     0.7807  17.052   <2e-16 ***
## ses           2.9567     1.1294   2.618   0.0114 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.565 on 55 degrees of freedom
## Multiple R-squared:  0.1108, Adjusted R-squared:  0.09463 
## F-statistic: 6.853 on 1 and 55 DF,  p-value: 0.01141
coef(summary(fit.ses))
##              Estimate Std. Error   t value     Pr(>|t|)
## (Intercept) 13.312180   0.780695 17.051704 1.287130e-23
## ses          2.956694   1.129444  2.617832 1.140768e-02
sigma(fit.ses)^2 # error variance
## [1] 30.96776
summary(fit.ses)$r.squared # r-squared
## [1] 0.1107956
library(ggplot2)
ggplot(hsb1, aes(x = ses, y = mathach)) + 
  geom_point(size=3) + stat_smooth(method="lm")
## `geom_smooth()` using formula = 'y ~ x'

中心化

###中心化
library(psych)
## 
## 載入套件:'psych'
## 下列物件被遮斷自 'package:ggplot2':
## 
##     %+%, alpha
describe(hsb1$ses)
##    vars  n mean   sd median trimmed  mad   min  max range  skew kurtosis   se
## X1    1 57 0.23 0.66   0.26    0.24 0.65 -1.06 1.51  2.57 -0.17     -0.7 0.09
library(dplyr)
## 
## 載入套件:'dplyr'
## 下列物件被遮斷自 'package:stats':
## 
##     filter, lag
## 下列物件被遮斷自 'package:base':
## 
##     intersect, setdiff, setequal, union
hsb1 <- hsb1 %>%
  mutate(sescentering = hsb1$ses - mean(hsb1$ses))

sub.dat1 <- hsb1[, c("sescentering", "mathach")]
head(sub.dat1)  # just to confirm I have the right variables
## # A tibble: 6 × 2
##   sescentering mathach
##          <dbl>   <dbl>
## 1     -0.276     21.4 
## 2      1.07      14.0 
## 3      0.174     19.2 
## 4     -1.29       7.81
## 5     -0.406      9.83
## 6      0.00421   22.6
colMeans(sub.dat1)
##  sescentering       mathach 
## -4.869399e-19  1.398568e+01
##Descriptive stat
summary(sub.dat1) 
##   sescentering         mathach      
##  Min.   :-1.28579   Min.   : 2.454  
##  1st Qu.:-0.29579   1st Qu.: 9.490  
##  Median : 0.03421   Median :13.071  
##  Mean   : 0.00000   Mean   :13.986  
##  3rd Qu.: 0.49421   3rd Qu.:19.161  
##  Max.   : 1.28421   Max.   :24.488
## Correlation matrices
cor(sub.dat1)  # correlation matrix
##              sescentering   mathach
## sescentering    1.0000000 0.3328597
## mathach         0.3328597 1.0000000
## Creating new variables
sub.dat1$sescenteringXmath <- sub.dat1$sescentering * sub.dat1$mathach
head(sub.dat1)
## # A tibble: 6 × 3
##   sescentering mathach sescenteringXmath
##          <dbl>   <dbl>             <dbl>
## 1     -0.276     21.4            -5.90  
## 2      1.07      14.0            15.0   
## 3      0.174     19.2             3.34  
## 4     -1.29       7.81          -10.0   
## 5     -0.406      9.83           -3.99  
## 6      0.00421   22.6             0.0953
# Regression with lm using the full dataset without considering nesting structure
# Coefficients, standard errors, and t-test results
fit.ses1 <- lm(mathach ~ sescentering, data = hsb1)
summary(fit.ses1)
## 
## Call:
## lm(formula = mathach ~ sescentering, data = hsb1)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -11.278  -4.400  -1.284   4.880   9.240 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   13.9857     0.7371  18.974   <2e-16 ***
## sescentering   2.9567     1.1294   2.618   0.0114 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.565 on 55 degrees of freedom
## Multiple R-squared:  0.1108, Adjusted R-squared:  0.09463 
## F-statistic: 6.853 on 1 and 55 DF,  p-value: 0.01141
coef(summary(fit.ses1))
##               Estimate Std. Error   t value     Pr(>|t|)
## (Intercept)  13.985684  0.7370849 18.974320 8.364414e-26
## sescentering  2.956694  1.1294439  2.617832 1.140768e-02
sigma(fit.ses1)^2 # error variance
## [1] 30.96776
summary(fit.ses1)$r.squared # r-squared
## [1] 0.1107956
library(ggplot2)
ggplot(hsb1, aes(x = sescentering, y = mathach)) + 
  geom_point(size=3) + stat_smooth(method="lm")
## `geom_smooth()` using formula = 'y ~ x'