## An example to show that the prediction error of the magnitude based on
## 1-nearest neighbor predictor can be reduced by taking marks into account.
## It will take about 5 minutes if you run this example
## Not run:
# library(mmpp)
# data(Miyagi20030626)
# ## split the original MPP by using 3-hour time window
# sMiyagi <- splitMPP(Miyagi20030626,h=60*60*3,scaleMarks=TRUE)$S
#
# ## target of the prediction is the maximum magnitude in the window
# y <- NULL
# for(i in 1:length(sMiyagi)){
# y <- c(y, max(sMiyagi[[i]]$magnitude))
# }
#
# y <- y[-1]
# sMiyagi[[length(sMiyagi)]] <- NULL
#
# ## number of whole partial MPPs splitted by a 3-hour time window
# N <- length(sMiyagi)
# ## training samples are past one week data
# Ntr <- 24*7/3
# ## number of different prediction methods
# Nd <- 10
#
# err <- matrix(0, N-Ntr, Nd)
# colnames(err) <- c("f SPP","iip SPP","cooc smooth SPP","cooc count SPP","iei SPP",
# "f MPP","iip MPP","cooc smooth MPP","cooc count MPP","iei MPP")
#
# ## predict the max magnitude in the next 3-hour based on the similarity
# ## between the current partial point process and the 7-days past partial point process
# cat("running prediction experiment")
# for(t in 1:(N-Ntr)){
# cat(".")
# qid <- Ntr+t
# q <- sMiyagi[[qid]]
#
# ## simple PP
# ## fmetric with tau=1
# sim2query <- NULL
# for(i in 1:Ntr){
# sim2query <- c(sim2query,fmetric(q$time,sMiyagi[[qid-i]]$time))
# }
# err[t,1] <- abs(y[qid]-y[t:(Ntr+t-1)][which.max(sim2query)])
#
# ## iipmetric with tau=1
# sim2query <- NULL
# for(i in 1:Ntr){
# sim2query <- c(sim2query,iipmetric(q$time,sMiyagi[[qid-i]]$time))
# }
# err[t,2] <- abs(y[qid]-y[t:(Ntr+t-1)][which.max(sim2query)])
#
# ## coocmetric (smooth) with tau=1
# sim2query <- NULL
# for(i in 1:Ntr){
# sim2query <- c(sim2query,coocmetric(q$time,sMiyagi[[qid-i]]$time,type="smooth"))
# }
# err[t,3] <- abs(y[qid]-y[t:(Ntr+t-1)][which.max(sim2query)])
#
# ## coocmetric (count)
# sim2query <- NULL
# for(i in 1:Ntr){
# sim2query <- c(sim2query,coocmetric(q$time,sMiyagi[[qid-i]]$time,type="count"))
# }
# err[t,4] <- abs(y[qid]-y[t:(Ntr+t-1)][which.max(sim2query)])
#
# ## iei metric
# sim2query <- NULL
# for(i in 1:Ntr){
# sim2query <- c(sim2query,ieimetric(q$time,sMiyagi[[qid-i]]$time))
# }
# err[t,5] <- abs(y[qid]-y[t:(Ntr+t-1)][which.max(sim2query)])
#
# ## marked PP with latitude, longitude, depth, and magnitude
# ## fmetric with tau=1
# sim2query <- NULL
# for(i in 1:Ntr){
# sim2query <- c(sim2query,fmetric(q,sMiyagi[[qid-i]]))
# }
# err[t,6] <- abs(y[qid]-y[t:(Ntr+t-1)][which.max(sim2query)])
#
# ## iipmetric with tau=1
# sim2query <- NULL
# for(i in 1:Ntr){
# sim2query <- c(sim2query,iipmetric(q,sMiyagi[[qid-i]]))
# }
# err[t,7] <- abs(y[qid]-y[t:(Ntr+t-1)][which.max(sim2query)])
#
# ## coocmetric (smooth) with tau=1
# sim2query <- NULL
# for(i in 1:Ntr){
# sim2query <- c(sim2query,coocmetric(q,sMiyagi[[qid-i]],type="smooth"))
# }
# err[t,8] <- abs(y[qid]-y[t:(Ntr+t-1)][which.max(sim2query)])
#
# ## coocmetric (count)
# sim2query <- NULL
# for(i in 1:Ntr){
# sim2query <- c(sim2query,coocmetric(q,sMiyagi[[qid-i]],type="count"))
# }
# err[t,9] <- abs(y[qid]-y[t:(Ntr+t-1)][which.max(sim2query)])
#
# ## iei metric
# sim2query <- NULL
# for(i in 1:Ntr){
# sim2query <- c(sim2query,ieimetric(q,sMiyagi[[qid-i]]))
# }
# err[t,10] <- abs(y[qid]-y[t:(Ntr+t-1)][which.max(sim2query)])
#
# }
# cat("done\n")
# print(colMeans(err))
# ##f SPP iip SPP cooc smooth SPP cooc count SPP iei SPP
# ##0.7002634 0.6839529 0.7263602 0.6632930 0.7905148
# ##f MPP iip MPP cooc smooth MPP cooc count MPP iei MPP
# ##0.6839529 0.6317594 0.6643804 0.6622056 0.7698548
# ## End(Not run)
Run the code above in your browser using DataLab