第二題 #先從網路抓取資料
theURL <-"http://www1.aucegypt.edu/faculty/hadi/RABE5/Data5/P005.txt"
dta<- read.table(file = theURL, header = T, sep = "\t")
head(dta) #看一下前六欄的資料
## City COL PD URate Pop Taxes Income RTWL
## 1 Atlanta 169 414 13.6 1790128 5128 2961 1
## 2 Austin 143 239 11.0 396891 4303 1711 1
## 3 Bakersfield 339 43 23.7 349874 4166 2122 0
## 4 Baltimore 173 951 21.0 2147850 5001 4654 0
## 5 Baton Rouge 99 255 16.0 411725 3965 1620 1
## 6 Boston 363 1257 24.4 3914071 4928 5634 0
#把要比較的變項取出來
dta_compare <- dta[, c('Income', 'Taxes')]
round(cor(dta_compare), 3)#只取到小數點後第三位
## Income Taxes
## Income 1.000 0.056
## Taxes 0.056 1.000
#另一種方法
round(cor(dta$Income, dta$Taxes),3)
## [1] 0.056
#可以進一步檢定相關是否顯著,以及信賴區間
cor.test(~ Income + Taxes, data = dta_compare)
##
## Pearson's product-moment correlation
##
## data: Income and Taxes
## t = 0.33696, df = 36, p-value = 0.7381
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.2684231 0.3691383
## sample estimates:
## cor
## 0.0560718