library(lattice)
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggplot2)
library(car)
## 載入需要的套件:carData
## 
## 載入套件:'car'
## 下列物件被遮斷自 'package:dplyr':
## 
##     recode
## 下列物件被遮斷自 'package:purrr':
## 
##     some
DATE <- c("324", "325", "326", "327", "328", "329", "330", "331", "401",
          "402", "403", "404", "405", "406", "407", "408", "409", "410", "411","412")
Domestic <- c(15, 14, 21, 83, 34, 33, 56, 87, 104, 160, 183, 133, 216, 281, 382, 384, 442, 431, 439,551)
Oversea <- c(124, 122, 82, 120, 93, 63, 107, 152, 132, 244, 97, 142, 65, 78, 149, 123, 136, 144, 191,112)
Male <-c(6,12,13,69,15,15,36,46,45,83,98,64,100,148,188,187,230,209,216,291)
Female <-c(9,2,8,14,19,18,20,41,59,77,85,69,116,133,194,197,212,219,223,260)
#把上面的資料變成daraframe
cvdta <- data.frame(DATE, Domestic, Oversea, Male, Female)
cvdta$DATE <- as.factor(cvdta$DATE)

境外案例與本土案例的分析

scatterplot(Domestic ~ Oversea, 
            data = cvdta, 
            smooth = F)

重上述這張圖中,我們可以大致看出境外移入與本土案例大致呈現正相關。

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

cvmod <- lm(Domestic ~ Oversea, data = cvdta)
summary(cvmod)
## 
## Call:
## lm(formula = Domestic ~ Oversea, data = cvdta)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -187.66 -136.95  -97.39  156.82  360.77 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)   74.210    122.743   0.605    0.553
## Oversea        1.036      0.940   1.102    0.285
## 
## Residual standard error: 174.5 on 18 degrees of freedom
## Multiple R-squared:  0.0632, Adjusted R-squared:  0.01115 
## F-statistic: 1.214 on 1 and 18 DF,  p-value: 0.285

在上述的分析中,我們可以發現其實本土案例與境外案例之間的關係並不大。因為其R square值僅有0.0493,表示境外案例對於國內案例的解釋程度只有大約5%左右。而因為t值通常當資料數大於5或是6時,其t值通常為2,而此次分析中T值為1.39,我們可以發現其與2之間的距離並不大,再加上其p值小於0.05,因此我們並沒有足夠的信心拒絕虛無假設,因其結論為境外移入案例並無法很好的預測本土案例。

本土男性案例與本土女性案例的分析

cvdta$DATE <- as.factor(cvdta$DATE)
boxplot(cvdta$Male,cvdta$Female,col = "alice blue", border = "brown")

首先我們先對男性與女性的本土案例進行圖形的觀察,由此圖中,我們可以看見此兩個變項的平均數接近,但男性的全距略大於女性的全距。

t.test(cvdta$Male,cvdta$Female)
## 
##  Welch Two Sample t-test
## 
## data:  cvdta$Male and cvdta$Female
## t = 0.1724, df = 38, p-value = 0.864
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -51.56332  61.16332
## sample estimates:
## mean of x mean of y 
##    103.55     98.75

透過T-test,我們可以對此兩筆數據進行檢定,觀看其兩筆數據是否能拒絕虛無假設。而透過此統計檢定結果,我們可以發現。其95%信賴區間中包含0,且其p值為0.864,其值大於0.05。透過上述兩點,我們可以說此統計結果無法拒絕虛無假設,因此此兩組數據之平均數差異為0。

scatterplot(Male ~ Female, 
            data = cvdta, 
            smooth = F)

對此兩組數據進行作圖,我們可以發現此兩筆數據大致呈現正相關,且其點接近直線,因此其看起來互相能解釋對方的程度很大,但我們還是不能下定論,因為其實際上能多少程度的解釋對方仍需進行後續的判定。

cvmod <- lm(Male ~ Female, data = cvdta)
summary(cvmod)
## 
## Call:
## lm(formula = Male ~ Female, data = cvdta)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -20.477 -10.308  -5.258   8.389  48.614 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  6.64763    5.92047   1.123    0.276    
## Female       0.98129    0.04525  21.684 2.37e-14 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 17.37 on 18 degrees of freedom
## Multiple R-squared:  0.9631, Adjusted R-squared:  0.9611 
## F-statistic: 470.2 on 1 and 18 DF,  p-value: 2.374e-14

由上述的結果,我們可以看出女性本土案例能解釋男性本土案例的程度約為96%,可說是有相當高的解釋能力。且在其分析中,我們也可以看見其t值為21、離2很遠,且其p值小於0.05,因此我們可以有足夠的信心去拒絕虛無假設,並說本土女性案例可以良好的預測本土男性案例。

分析境外移入案例、本土案例、本土男性案例、本土女性案例

ccdta <- pivot_longer(cvdta, cols = c(, 2:5), names_to = "category", values_to = "case")
ccdta_ttest <- filter(ccdta, category== "Domestic" | category== "Oversea"  | category== "Male" | category== "Female")
boxplot(case ~ category, data = ccdta)

從整體案例在3/24~4/12的區間,我們可以看見在這個期間中,本土案例的全距很大,且境外移入有一outlier的存在。

vmod_A <- lm(case ~ category, data = ccdta)
summary(vmod_A)
## 
## Call:
## lm(formula = case ~ category, data = ccdta)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -188.45  -79.00  -18.13   70.04  348.55 
## 
## Coefficients:
##                 Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       202.45      24.52   8.256 3.56e-12 ***
## categoryFemale   -103.70      34.68  -2.990  0.00376 ** 
## categoryMale      -98.90      34.68  -2.852  0.00560 ** 
## categoryOversea   -78.65      34.68  -2.268  0.02618 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 109.7 on 76 degrees of freedom
## Multiple R-squared:  0.1319, Adjusted R-squared:  0.09766 
## F-statistic:  3.85 on 3 and 76 DF,  p-value: 0.01273

雖然我依舊不知道怎麼將多個變數一起進行線性分析,所以得出了這個結果其實並不能被同時進行分析。因為在我所設定的類別中,本土女性確診案例以及本土男性確診案例在R中分別與本土案例進行分析了,但其實並不能這樣分析。因為本土案例中就包含本土女性確診案例以及本土男性確診案例,其範圍與類別有所重疊,因此不能進行分析。