library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(caret)
## Warning: package 'caret' was built under R version 3.4.3
## Loading required package: lattice
## Loading required package: ggplot2
library(e1071)
## Warning: package 'e1071' was built under R version 3.4.3
data = read.csv('part2.csv',header = TRUE)%>%as.data.frame()
d= data[,-1]
#移除遺失值
for (i in 1:ncol(d)){
  d = d [ d[,i]!= '...',] 
}
for(i in c(6:8)){
  d[,i] =as.numeric(as.character(d[,i]))  
}
d =na.omit(d)
#SVM預測
#預測2016年的各縣市資料
testpart = d[(c(nrow(d)-21):nrow(d)),]
trainpart = d[c(1:(nrow(d)-22)),]
svm.model = svm(X.1 ~., data = trainpart)
svm.pred <- predict(svm.model, testpart[,-1])
## 顯示結果
print(svm.pred)
##     416     417     418     419     420     421     422     423     424 
##  新北市  澎湖縣  高雄市  高雄市  臺南市  高雄市  澎湖縣  苗栗縣  苗栗縣 
##     425     426     427     428     429     430     431     432     433 
##  苗栗縣  嘉義縣  嘉義縣  嘉義縣  屏東縣  屏東縣  澎湖縣  澎湖縣  基隆市 
##     434     435     436     437 
##  新竹市  澎湖縣  基隆市  基隆市 
## 23 Levels:   宜蘭縣  花蓮縣  金門縣  南投縣  屏東縣  苗栗縣 ...  澎湖縣
#結果明顯不正確

把組別簡化成兩類再試試看

t=d
u = c(' 臺北市',' 新北市',' 臺中市',' 高雄市',' 臺南市',' 新竹市',' 桃園市')
t= mutate(t,city =' ')
for(i in c(1:length(u))){
  t$city[which(t$X.1 ==u[i])]='都市'
}
t$city[which(t$city==' ')]='非都市'
t$city = as.factor(t$city)
t=t[,c(-1)]
testpart2 = t[(c(nrow(t)-21):nrow(t)),]
trainpart2 = t[c(1:(nrow(t)-22)),]
svm.model2 = svm(city~., data = trainpart2)
svm.pred2 <- predict(svm.model2, testpart2[,-8])
## 顯示結果
print(svm.pred2)
##    391    392    393    394    395    396    397    398    399    400 
## 非都市 非都市   都市   都市   都市   都市 非都市 非都市 非都市 非都市 
##    401    402    403    404    405    406    407    408    409    410 
## 非都市 非都市 非都市 非都市 非都市 非都市 非都市 非都市 非都市 非都市 
##    411    412 
## 非都市 非都市 
## Levels: 非都市 都市
confusionMatrix(svm.pred2,testpart2[,8])
## Confusion Matrix and Statistics
## 
##           Reference
## Prediction 非都市 都市
##     非都市     15    3
##     都市        0    4
##                                           
##                Accuracy : 0.8636          
##                  95% CI : (0.6509, 0.9709)
##     No Information Rate : 0.6818          
##     P-Value [Acc > NIR] : 0.04779         
##                                           
##                   Kappa : 0.6452          
##  Mcnemar's Test P-Value : 0.24821         
##                                           
##             Sensitivity : 1.0000          
##             Specificity : 0.5714          
##          Pos Pred Value : 0.8333          
##          Neg Pred Value : 1.0000          
##              Prevalence : 0.6818          
##          Detection Rate : 0.6818          
##    Detection Prevalence : 0.8182          
##       Balanced Accuracy : 0.7857          
##                                           
##        'Positive' Class : 非都市          
## 
## Accuracy為0.86,明顯有進步,很可能是因為資料量(1998-2015年)不夠,因此SVM沒辦法分類到那麼仔細