使用 glm probit 模型生成预测
generate forecast using glm probit model
我正在尝试使用 forecast
命令为 glm(概率回归)模型生成一个简单的单期预测。但是当 运行 以下代码时,我收到以下消息: Error in as.data.frame(newdata) : argument "newdata" is missing, with no default
#generate dataset with unknown value for last period dependent variable
data.set <- data.frame(date= seq(as.Date('2015-01-01'), by = 'days' , length = 100),
replicate(100, sample(0:1,1)), runif(1e2),runif(1e2),runif(1e2))
colnames(data.set) <- c("date", "dv", "iv1", "iv2", "iv3")
data.set.ts <- xts(data.set[-1], order.by=data.set$date)
rm(data.set)
data.set.ts$dv[100] <- NA
#run glm probit model
PRmodel <- glm(dv ~ iv1 + iv2 + iv3, data = data.set.ts[-nrow(data.set.ts),],
family = binomial(link = "probit"))
#generate forecast for last period dv
iv_input <- xts(data.set.ts[, which(colnames(data.set.ts) %in% c("iv1" , "iv2" , "iv3"))],
order.by = index(data.set.ts))
start.date <- index(data.set.ts)[length(index(data.set.ts))]
fcastFn_output <- forecast(PRmodel, xreg=window(iv_input, start = start.date))
我也尝试过使用 predict
命令,但如果缺少实际值,R 似乎不会为观察生成预测值。
生成预测的代码应该遵循这些原则
data.set <- data.frame(date= seq(as.Date('2015-01-01'), by = 'days' , length = 100),
replicate(100, sample(0:1,1)), runif(1e2),runif(1e2),runif(1e2))
colnames(data.set) <- c("date", "dv", "iv1", "iv2", "iv3")
# Load the required package.
require(xts)
data.set.ts <- xts(data.set[-1], order.by=data.set$date)
rm(data.set)
data.set.ts$dv[100] <- NA
#run glm probit model
PRmodel <- glm(dv ~ iv1 + iv2 + iv3, data = data.set.ts[-nrow(data.set.ts),],
family = binomial(link = "probit"))
#generate forecast for last period dv
iv_input <- xts(data.set.ts[, which(colnames(data.set.ts) %in% c("iv1" , "iv2" , "iv3"))],
order.by = index(data.set.ts))
start.date <- index(data.set.ts)[length(index(data.set.ts))]
# Generate the forecast for 2015-04-10.
fcastFn_output <- predict(PRmodel, window(iv_input, start = start.date))
# Create a time series with all the forecasts.
fcastFn_all <- c(predict(PRmodel), fcastFn_output)
希望对您有所帮助
我正在尝试使用 forecast
命令为 glm(概率回归)模型生成一个简单的单期预测。但是当 运行 以下代码时,我收到以下消息: Error in as.data.frame(newdata) : argument "newdata" is missing, with no default
#generate dataset with unknown value for last period dependent variable
data.set <- data.frame(date= seq(as.Date('2015-01-01'), by = 'days' , length = 100),
replicate(100, sample(0:1,1)), runif(1e2),runif(1e2),runif(1e2))
colnames(data.set) <- c("date", "dv", "iv1", "iv2", "iv3")
data.set.ts <- xts(data.set[-1], order.by=data.set$date)
rm(data.set)
data.set.ts$dv[100] <- NA
#run glm probit model
PRmodel <- glm(dv ~ iv1 + iv2 + iv3, data = data.set.ts[-nrow(data.set.ts),],
family = binomial(link = "probit"))
#generate forecast for last period dv
iv_input <- xts(data.set.ts[, which(colnames(data.set.ts) %in% c("iv1" , "iv2" , "iv3"))],
order.by = index(data.set.ts))
start.date <- index(data.set.ts)[length(index(data.set.ts))]
fcastFn_output <- forecast(PRmodel, xreg=window(iv_input, start = start.date))
我也尝试过使用 predict
命令,但如果缺少实际值,R 似乎不会为观察生成预测值。
生成预测的代码应该遵循这些原则
data.set <- data.frame(date= seq(as.Date('2015-01-01'), by = 'days' , length = 100),
replicate(100, sample(0:1,1)), runif(1e2),runif(1e2),runif(1e2))
colnames(data.set) <- c("date", "dv", "iv1", "iv2", "iv3")
# Load the required package.
require(xts)
data.set.ts <- xts(data.set[-1], order.by=data.set$date)
rm(data.set)
data.set.ts$dv[100] <- NA
#run glm probit model
PRmodel <- glm(dv ~ iv1 + iv2 + iv3, data = data.set.ts[-nrow(data.set.ts),],
family = binomial(link = "probit"))
#generate forecast for last period dv
iv_input <- xts(data.set.ts[, which(colnames(data.set.ts) %in% c("iv1" , "iv2" , "iv3"))],
order.by = index(data.set.ts))
start.date <- index(data.set.ts)[length(index(data.set.ts))]
# Generate the forecast for 2015-04-10.
fcastFn_output <- predict(PRmodel, window(iv_input, start = start.date))
# Create a time series with all the forecasts.
fcastFn_all <- c(predict(PRmodel), fcastFn_output)
希望对您有所帮助