(ISLR)2019-nCov新增人數預測模型

Title : TODO

Objective : TODO

Created by: chenjunyi

Created on: 2020/2/11

#Feb_11
#截止至昨日24:00 的感染人數
par(family=“STXihei”)
Today_confirmedNum <- c( #截至於Feb_11
44742,42744, 40224, 37162,
34594, 31197, 28060, 24363, 20471,
17238, 14411, 11821, 9720, 7736,
5997, 4535, 2761, 1985, 1297
)
#獲得數據長度
date_len <- length(Today_confirmedNum) #數據長度
x <- 1:date_len
lastnum <- date_len

#截至前日24:00 的感染人數
Yesterday_confirmedNum <- c(Today_confirmedNum[2:date_len],830)

#截止至昨日24:00 的新增確診人數序列
ConfirmNumIcr <- rev(Today_confirmedNum - Yesterday_confirmedNum)

days <- 30

#畫出數據離散點 注:plot()必須加上type=""參數才能畫出線
plot(x, ConfirmNumIcr,type = ‘b’,xlim = c(1,days+1),ylim =c(0,5000),xlab=‘1月23日起’, ylab=‘新增確診人數’,cex = 1.5 )
points(x[lastnum], ConfirmNumIcr[lastnum],col=“black”,pch=19,cex=2)

#建立模型;已證明該模型p-value最小
model1 <-lm.fit3_2 <- lm(ConfirmNumIcr~I(x2)+I(x3))

#求出擬合模型
#爲什麼要用data.frame?
model_date <- data.frame(x=seq(1,days,1))
#predict()函數的意義:給定一個模型model1,給出橫座標向量,生成縱座標向量,se=TRUE即指給出Standard ERROR
#Q:model_fitted_y也是有下個值的向量,如何lines()出來?

model_fitted_y <- predict(model1, newdata = model_date,se = TRUE)
#畫出包括昨日數據的模型
lines(model_datex,modelfittedyx, model_fitted_yfit,col=‘red’,cex = 2,lwd = 2)
#points(model_datex[daysofmonth],modelfittedyx[days_of_month], model_fitted_yfit[days_of_month],col=“red”,cex=2,pch=19)
points(model_datex[datelen],modelfittedyx[date_len], model_fitted_yfit[date_len],col=“red”,cex=2,pch=19)
points(model_datex[datelen+1],modelfittedyx[date_len+1], model_fitted_yfit[date_len+1],col=“red”,cex=2,pch=19)

#畫出去掉昨日數據的模型
ConfirmNumIcr.old <- ConfirmNumIcr[1:date_len-1]
x.old <- x[1:date_len-1]
model.old <- lm(ConfirmNumIcr.old~I(x.old2)+I(x.old3)) #得到的是擬合的函數
model_date.old <- data.frame(x.old=seq(1,days,1))
#給出預測變量x,得到在所擬合的函數對應的響應值
model_fitted_y.old <- predict(model.old, newdata = model_date.old,se = TRUE)
lines(model_date.oldx,modelfittedy.oldx, model_fitted_y.oldfit,col=‘green’,cex = 2,lwd = 2)
points(model_date.oldx[datelen1],modelfittedy.oldx[date_len-1], model_fitted_y.oldfit[date_len-1],col=“green”,cex=2,pch=19)
points(model_date.oldx[datelen],modelfittedy.oldx[date_len], model_fitted_y.oldfit[date_len],col=“green”,cex=2,pch=19)

#備註信息
legend(“top”, legend = “空心點爲實際數據,綠線爲昨日模型,紅線爲今日模型”,text.col = “black”,text.width = 26)
text(date_len+7,model_fitted_yfit[datelen+1],"(2.13)")text(datelen+7,modelfittedy.oldfit[date_len+1],"預測明日(2.13)新增點") text(date_len+7,model_fitted_y.oldfit[date_len],“昨日模型預測今日新增點”)

diff <- ConfirmNumIcr[date_len] - model_fitted_y.oldfit[datelen]diffper<diff/ConfirmNumIcr[datelen]print(sprintf(:print(sprintf(:fit[date_len] diff_per <- diff/ConfirmNumIcr[date_len] print(sprintf('昨日模型偏差: %4.2f%%', 100*diff_per)) print(sprintf('今日模型預測明日新增數據爲: %4.2f', model_fitted_yfit[date_len+1]))
print(sprintf(‘算上偏差,今日模型預測明日(2.13)新增數據爲: %4.2f’,(diff_per+1)*model_fitted_y$fit[date_len+1]))

points(model_date$x[date_len+1], 1275.72,col=“blue”,cex=2,pch=19)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章