dta <- read.csv("data.csv")

資料來源為疾管署資料以及觀測資料查詢 (CODiS)

head(dta)
##   Date Domestic Oversea Rainy_Taipei
## 1  324       15     124            N
## 2  325       14     122            Y
## 3  326       21      82            Y
## 4  327       83     120            Y
## 5  328       34      93            Y
## 6  329       33      63            Y

檢視前六筆資料

str(dta)
## 'data.frame':    20 obs. of  4 variables:
##  $ Date        : int  324 325 326 327 328 329 330 331 401 402 ...
##  $ Domestic    : int  15 14 21 83 34 33 56 87 104 160 ...
##  $ Oversea     : int  124 122 82 120 93 63 107 152 132 244 ...
##  $ Rainy_Taipei: chr  "N" "Y" "Y" "Y" ...

看資料的結構

dta$Rainy_Taipei <- as.factor(dta$Rainy_Taipei)

把類別變項轉成factor

ggplot(aes(y = Oversea, x = Domestic, color = Rainy_Taipei), data = dta) +
  geom_point() +
  geom_smooth(method = lm, se = F) + 
  theme_bw()
## `geom_smooth()` using formula 'y ~ x'

看資料之間的作圖與線性關係,我們可以看見其圖形相當的分散且點皆與所呈現出的線相差甚遠。所以我們可以猜測台北下雨與否這個變項也許與確診案例並沒有太大的關係,但這只是初步猜測,我們仍需要進行後續分析才得以下結論。

cvmod <- lm(Oversea ~ Domestic + Rainy_Taipei, data = dta)
summary(cvmod)
## 
## Call:
## lm(formula = Oversea ~ Domestic + Rainy_Taipei, data = dta)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -58.736 -28.403  -0.116  14.730 121.207 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   109.67132   22.00852   4.983 0.000114 ***
## Domestic        0.06512    0.06792   0.959 0.351154    
## Rainy_TaipeiY   2.70280   24.35507   0.111 0.912936    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 43.56 on 17 degrees of freedom
## Multiple R-squared:  0.06387,    Adjusted R-squared:  -0.04626 
## F-statistic:  0.58 on 2 and 17 DF,  p-value: 0.5706

透過多元迴歸看資料之間的關係,我們可以看見在控制了其他的變項後。 海外確診人數與本土確診人數之間並沒有顯著的關係,因為其p值為0.351154,大於0.05,且同時其t值為0.959,比1.96還要來的小,所以我們可以做出其未達顯著的結論,也就是說自變項(本土案例)並沒有辦法很好的對依變項(境外移入)進行預測。 同樣的在台北天氣的部分,其T值為0.111,比1.96還要來的小,且其p值為0.912936,比0.05還要來的大,所以我們也可以做出其並沒有顯著差異的結論,也就是說自變項(台北下雨與否)並沒有辦法很好的對依變項(境外移入)進行預測。。 而從R平方值的部份我們也可以做出這個model僅能對結果做出6.387%的變異解釋,我們可以發現其解釋力可以說是相當的小。

整體迴歸公式為: 109.67132 + 0.06512Domestic +2.70280Rainy_TaipeiY + 43.56 Regression coefficient of β1為 0.06512,其意義為當本土案例提高1時,境外移入將會提高0.06512。

Diagnostic

接下來將以下方的圖形來進行資料的常態性觀察

hist( x = residuals(cvmod),
      xlab = "Value of residual",
      main = "",
      breaks = 20)

透過這張圖我們可以看見其殘差基本上都在接近0的地方,所以我們可以說在這筆數據中,基本上接近常態分布。但是同時我們卻也可以看見有一筆資料離0以及大部份資料的分布的還要來的遠,所以也許我們可以猜測資料中或許有outlier的存在。

plot(cvmod, which  = 1)

plot(cvmod, which = 2)

plot(cvmod, which = 3)

library(car)
residualPlots(cvmod)

##              Test stat Pr(>|Test stat|)
## Domestic       -0.6212           0.5432
## Rainy_Taipei                           
## Tukey test     -0.4267           0.6696

在以上各圖中我們可以再次驗證我們的資料基本上符合常態分佈,但是同時也有資料很明顯的與其他資料相差甚遠。也許在未來有機會要繼續對資料進行分析的話,會需要進一步觀察資料有哪些不妥,並進行資料的排除或是偵錯。