把之前亂七八糟的程式碼整理一下並且提供之前自己整理出來的資料集,方便我自己之後如果要在不同電腦調閱資料用,如果要copy走也比較方便不用一個一個點,其實我之前一直卡在,如果資料不是常態怎麼處理,如果資料有交叉項要怎麼辦,也許沒有這次鐵人賽,我絕不會親自去找尋答案吧!
檔案下載:https://drive.google.com/drive/folders/1uiXSjAUZz2uRai9gSx8kCxiabzFtYoIz?usp=sharing
library(tidyverse)
library(magrittr)
#----------------讀取-------------#
x = read.csv(paste0("C:\\Users\\",paste(Sys.getenv("USERNAME")),"\\Desktop\\Day 7 資料關連.csv"))
y = read.csv(paste0("C:\\Users\\",paste(Sys.getenv("USERNAME")),"\\Desktop\\Day 7 _變數名稱關連.csv"))
# x[is.na(x)] = 0
xx = x
#---------------處理非遺失值----#
xx[c(2:5,12)][is.na(xx[c(2:5,12)])] = 0
#--------------目標不得遺失-----#
xx = xx[-which(xx$一天營業總收入 == 0),]
#-------------遺失值處理方法1_全部刪掉-----#
xx = xx[complete.cases(xx), ] #刪掉不完整的行
table(is.na(xx)) #看資料內是否還有NA值
# #-------------遺失值處理方法2_用平均值補-----#
# for(i in 1:length(xx))
# xx[is.na(xx[,i]), i] <- mean(xx[,i], na.rm=T)#用平均值代替
# table(is.na(xx)) #看資料內是否還有NA值
# #-------------遺失值處理方法2_用平均值補-----#
# library(Hmisc)
# impute(xx$每天空車時數, mean) # replace with mean
# impute(BostonHousing$ptratio, median) # median
# impute(BostonHousing$ptratio, 20) # replace specific number
# # 或是手動填補
# BostonHousing$ptratio[is.na(BostonHousing$ptratio)] <- mean(BostonHousing$ptratio, na.rm = T) # not run
for(i in c(1:11,13:length(x))){
for_vlookup = y[,(i+1)]
if(length(which(for_vlookup == "" )) !=0)
for_vlookup = for_vlookup[-which(for_vlookup == "" )] %>% as.character()#擷取一整行然後扣掉空值
a = 1:length(for_vlookup) #告訴我總共有多少變數
#把每一行都轉換成一個VLOOKUP用的表格
df1 = data.frame(
x1 = c(as.numeric(as.character(for_vlookup[a[a%%2==1]])),0),
x2 = c(as.character(for_vlookup[a[a%%2==0]]),0)
)
#每一行的資料
df2 = data.frame(
x1 =as.numeric(as.character(xx[,i]))
)
#VLOOKUP
w = df2 %>% inner_join(df1 , by = "x1")
#如果遇到開放題,則放棄vlookup
#取代掉原本的數字,變成方便閱讀的中文
if(identical(for_vlookup,character(0))==F){
xx[,i] = w[,2]
}else{
xx[,i] = ifelse(xx[,i]==-1,0,xx[,i])
}
}
View(xx)
#-----------改成金額-------#
change_to_num = y$一天營業總收入
change_to_num = change_to_num[-which(change_to_num == "" )] %>% as.character()#擷取一整行然後扣掉空值
a = 1:length(change_to_num)
df1 = data.frame(
x1 = c(as.numeric(as.character(change_to_num[a[a%%2==1]])),0),
x2 = c(200,500,700,900,1100,1300,1500,1700,1900,2100,2300,2500,2800,3200,3600,4500,0)
)
df2 = data.frame(
x1 =as.numeric(as.character(xx$一天營業總收入))
)
w = df2 %>% inner_join(df1 , by = "x1")
xx$一天營業總收入 = w[,2]
#-----------------------#
# View(xx)
# q = read.csv(paste0("C:\\Users\\",paste(Sys.getenv("USERNAME")),"\\Desktop\\Day 5 資料.csv"))[,45:49]
# q[is.na(q)] = 0
# q = q[-which(q$一天營業總收入 == 0),]
# qq = apply(q,1,sum)
#
# xx = xx %>%
# mutate("保險數" = qq)
xx[,"每天空車時數"] = as.numeric(as.character(xx[,"每天空車時數"]))
xx[,"年齡"] = as.numeric(as.character(xx[,"年齡"]))
xx[,"每月放假"] = as.numeric(as.character(xx[,"每月放假"]))
xx[,"已行駛公里數"] = as.numeric(as.character(xx[,"已行駛公里數"]))
xx[,"營業時數"] = as.numeric(as.character(xx[,"營業時數"]))
xx[,"工作總年資"] = as.numeric(as.character(xx[,"工作總年資"]))
fit<-lm(一天營業總收入~.,data=xx)
summary(fit)
可以用以下程式碼填入資料(1,2,3)後便可進行預測
ew.xx <- data.frame("經營型態" = 1,"巡迴攬客"=1,"招呼站等候"=1,"定點.不含招呼站.排班"=1,
"車行等候" = 1,"已行駛公里數" =3, "每月放假" =5 ,"營業時數"=3,
"每天空車時數" =2,"X105考慮停開計程車" =1,"加入無線電或衛星派遣車隊"=1,
"年齡" =3 , "教育程度"=2,"工作總年資"=2,"選擇開計程車之最主要原因"=1,
"最主要營業縣市"=1,"設置廣告物"=1,"性別"=1,"輪流駕駛" =1)
new.xx
predict(fit, new.xx)