R中预测与预测函数的区别 [英] Difference between forecast and predict function in R
本文介绍了R中预测与预测函数的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
R中的 predict()
和 forecast()
函数是否有区别?
如果是,应在哪些特定情况下使用?
解决方案
简介
-
预测
- 用于多种R对象(模型)。基础库的一部分。 -
预测
- 适用于时间序列。预测软件包的一部分。 (请参阅示例)。
示例
#load训练数据
trnData = read.csv(http://www.bodowinter.com/tutorial/politeness_data.csv)
模型< - lm(频率〜态度+场景,trnData)
#创建测试数据
tstData <-t(cbind(c(H1,H,2,pol,185),
c(M1,M,1,pol,115),
c(M1,M,1,inf,118),
c F1,F,3,inf,210)))
tstData< - data.frame(tstData,stringsAsFactors = F)
colnames(tstData)< colnames(trnData)
tstData [,3] = as.numeric(tstData [,3])
tstData [,5] = as.numeric(tstData [,5])
cbind(Obs = tstData $ frequency,pred = predict(model,newdata = tstData))
#forecast
x< - read.table(text ='day sum
2015-03-04 44
2015-03-05 46
2015-03-06 48
2015-03-07 48
2015-03-08 58
2015-03-09 58
2015-03-10 66
2015-03-11 68
2015-03-12 85
2015-03-13 94
2015-03-14 98
2015-03-15 102
2015-03-16 102
2015-03-17 104
2015-03-18 114',header = TRUE,stringsAsFactors = FALSE)
library(xts)
dates = as.Date(x $ day,% Y-%m-%d)
xs = xts(x $ sum,日期)
库(预测)
fit< - ets(xs)
plot(预测(fit))
预测(fit,h = 4)
Is there any difference between the
predict()
andforecast()
functions in R?If yes, in which specific cases should they be used?
解决方案Intro
predict
-- for many kinds of R objects (models). Part of the base library.forecast
-- for time series. Part of the forecast package. (See example).
Example
#load training data trnData = read.csv("http://www.bodowinter.com/tutorial/politeness_data.csv") model <- lm(frequency ~ attitude + scenario, trnData) #create test data tstData <- t(cbind(c("H1", "H", 2, "pol", 185), c("M1", "M", 1, "pol", 115), c("M1", "M", 1, "inf", 118), c("F1", "F", 3, "inf", 210))) tstData <- data.frame(tstData,stringsAsFactors = F) colnames(tstData) <- colnames(trnData) tstData[,3]=as.numeric(tstData[,3]) tstData[,5]=as.numeric(tstData[,5]) cbind(Obs=tstData$frequency,pred=predict(model,newdata=tstData)) #forecast x <- read.table(text='day sum 2015-03-04 44 2015-03-05 46 2015-03-06 48 2015-03-07 48 2015-03-08 58 2015-03-09 58 2015-03-10 66 2015-03-11 68 2015-03-12 85 2015-03-13 94 2015-03-14 98 2015-03-15 102 2015-03-16 102 2015-03-17 104 2015-03-18 114', header=TRUE, stringsAsFactors=FALSE) library(xts) dates=as.Date(x$day,"%Y-%m-%d") xs=xts(x$sum,dates) library("forecast") fit <- ets(xs) plot(forecast(fit)) forecast(fit, h=4)
这篇关于R中预测与预测函数的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文