library(RMySQL)
connect <- dbConnect(drv = MySQL(),
host = 'localhost',
user = 'root',
password = '123456',
dbname = 'ninashuju')
# 只需给数据库传递一条命令,就可解决乱码。
dbSendQuery(connect, 'SET NAMES GBK')
nian_16 <- dbReadTable(connect, 'nian_16')
dbDisconnect(connect) # 取消连接
library(sqldf)
detach("package:RMySQL", unload=TRUE) #为使sqldf可以正常使用
shijian <- sqldf('select sum(总价),签约日期 from nian_16
group by 签约日期')
names(shijian) <- c('money','time_16')
shijian$time_16 <- as.Date(shijian$time_16)
library(ggplot2)
p1 = ggplot(data=shijian,aes(x=time_16,y=money))+ labs(x='date',y='money',title='原始序列')+
geom_line(size=0.2) + theme_bw() + theme(plot.title = element_text(hjust = 0.5)) #原始序列图
p1
shijian$money <- log(shijian$money)
p2 = ggplot(data=shijian,aes(x=time_16,y=money))+ labs(x='date',y='money',title='原始序列')+
geom_line(size=0.2) + theme_bw() + theme(plot.title = element_text(hjust = 0.5))
p2
ats <- ts(shijian[,2],frequency=7)
new <- decompose(ats)
plot(new)
newdata = array(ats - new$seasonal)
diffshijian = diff(newdata)
ggplot(data=NULL,aes(x=c(1:length(diffshijian)),y=diffshijian)) + xlab('Index')+ ylab('diff(money)') +
geom_line(size=0.2) + theme_bw()
install.packages('tseries')
library(tseries)
install.packages('forecast')
library(forecast)
adf.test(diffshijian)
Box.test(diffshijian)
acf(diffshijian)
pacf(diffshijian)
fit = auto.arima(newdata,trace=T)
Box.test(fit$residuals)
residuals <- array(fit$residuals)
ggplot(data=NULL,aes(x=c(1:length(residuals)),y=residuals)) +
xlab('Index')+ ylab('residuals') + geom_line(size=0.2) + theme_bw()
newfit = fitted(fit) + array(new$seasonal)
expfit <- exp(newfit)
expshijian <- exp(shijian[,1])
ggplot(data=NULL,aes(x=shijian$time_16,y=expshijian )) + xlab('Index')+ ylab('money') +
geom_line(aes(y=expshijian),size=0.5,colour = "black") +
geom_line(aes(y=expfit),colour = "blue",alpha=0.5,size=0.5) + theme_bw()
forecast<-forecast(fit,h=31,level=c(95))
head(forecast)
connect <- dbConnect(drv = MySQL(),
host = 'localhost',
user = 'root',
password = '123456',
dbname = 'ninashuju')
# 只需给数据库传递一条命令,就可解决乱码。
dbSendQuery(connect, 'SET NAMES GBK')
nian_16 <- dbReadTable(connect, 'nian_16')
dbDisconnect(connect) # 取消连接
library(sqldf)
detach("package:RMySQL", unload=TRUE) #为使sqldf可以正常使用
shijian <- sqldf('select sum(总价),签约日期 from nian_16
group by 签约日期')
names(shijian) <- c('money','time_16')
shijian$time_16 <- as.Date(shijian$time_16)
library(ggplot2)
p1 = ggplot(data=shijian,aes(x=time_16,y=money))+ labs(x='date',y='money',title='原始序列')+
geom_line(size=0.2) + theme_bw() + theme(plot.title = element_text(hjust = 0.5)) #原始序列图
p1
shijian$money <- log(shijian$money)
p2 = ggplot(data=shijian,aes(x=time_16,y=money))+ labs(x='date',y='money',title='原始序列')+
geom_line(size=0.2) + theme_bw() + theme(plot.title = element_text(hjust = 0.5))
p2
ats <- ts(shijian[,2],frequency=7)
new <- decompose(ats)
plot(new)
newdata = array(ats - new$seasonal)
diffshijian = diff(newdata)
ggplot(data=NULL,aes(x=c(1:length(diffshijian)),y=diffshijian)) + xlab('Index')+ ylab('diff(money)') +
geom_line(size=0.2) + theme_bw()
install.packages('tseries')
library(tseries)
install.packages('forecast')
library(forecast)
adf.test(diffshijian)
Box.test(diffshijian)
acf(diffshijian)
pacf(diffshijian)
fit = auto.arima(newdata,trace=T)
Box.test(fit$residuals)
residuals <- array(fit$residuals)
ggplot(data=NULL,aes(x=c(1:length(residuals)),y=residuals)) +
xlab('Index')+ ylab('residuals') + geom_line(size=0.2) + theme_bw()
newfit = fitted(fit) + array(new$seasonal)
expfit <- exp(newfit)
expshijian <- exp(shijian[,1])
ggplot(data=NULL,aes(x=shijian$time_16,y=expshijian )) + xlab('Index')+ ylab('money') +
geom_line(aes(y=expshijian),size=0.5,colour = "black") +
geom_line(aes(y=expfit),colour = "blue",alpha=0.5,size=0.5) + theme_bw()
forecast<-forecast(fit,h=31,level=c(95))
head(forecast)