R Markdown

讀取資料

library(haven)  
pisa.dat <- read_sav("PISA_tawian2022_trimmed_lab.sav")  
View(pisa.dat)

library for creating graphs

使用圖表套件

library(ggplot2)
library(ggExtra)
library(ggpubr)

【ESCS和PV1MATH】

圖表(散布圖)

ESCS和PV1MATH的散布圖

ggplot(pisa.dat, aes(x=ESCS, y=PV1MATH)) + 
  geom_point()

圖表(增加直方圖)

ESCS和PV1MATH的散布圖+直方圖

ggMarginal(
  ggplot(pisa.dat, aes(x = ESCS, y = PV1MATH)) + geom_point(),
  type = "histogram"
)

With fit line and correlation coefficient

ESCS和PV1MATH

ggscatter(pisa.dat, x= "ESCS", y="PV1MATH", 
          add = "reg.line", conf.int = TRUE, 
          cor.coef = TRUE, cor.method = "pearson",
          xlab = "Index of economic,social and cultural status", ylab = "Plausible Value 1 in Mathematics")

計算相關係數

皮爾森相關係數以及p值(ESCS和PV1MATH)

cor.test(pisa.dat$ESCS, pisa.dat$PV1MATH, method = "pearson")
## 
##  Pearson's product-moment correlation
## 
## data:  pisa.dat$ESCS and pisa.dat$PV1MATH
## t = 31.952, df = 5597, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.3703870 0.4146987
## sample estimates:
##       cor 
## 0.3927708

皮爾森相關係數(ESCS和PV1MATH)

cor(pisa.dat$ESCS, pisa.dat$PV1MATH, method = "pearson")
## [1] 0.3927708

【AGE和ESCS】

圖表(散布圖)

AGE和ESCS的散布圖

ggplot(pisa.dat, aes(x=AGE, y=ESCS)) + 
  geom_point()

圖表(增加直方圖)

AGE和ESCS的散布圖+直方圖

ggMarginal(
  ggplot(pisa.dat, aes(x = AGE, y = ESCS)) + geom_point(),
  type = "histogram"
)

With fit line and correlation coefficient

AGE和ESCS

ggscatter(pisa.dat, x= "AGE", y="ESCS", 
          add = "reg.line", conf.int = TRUE, 
          cor.coef = TRUE, cor.method = "pearson",
          xlab = "Student's age", ylab = "Index of economic,social and cultural status")

計算相關係數

皮爾森相關係數以及p值(AGE和ESCS)

cor.test(pisa.dat$AGE, pisa.dat$ESCS, method = "pearson")
## 
##  Pearson's product-moment correlation
## 
## data:  pisa.dat$AGE and pisa.dat$ESCS
## t = -0.33172, df = 5597, p-value = 0.7401
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  -0.03062489  0.02176306
## sample estimates:
##          cor 
## -0.004433958

皮爾森相關係數(AGE和ESCS)

cor(pisa.dat$AGE, pisa.dat$ESCS, method = "pearson")
## [1] -0.004433958