R/plot.arfimaMLM.R

Defines functions plot.arfimaMLM

Documented in plot.arfimaMLM

#' ArfimaMLM plotting function
#' 
#' Function for plotting time-varying coefficients generated by Arfima-MLM package
#' 
#' @param x Object of class ArfimaMLM
#' @param CoefName Name of time varying coefficient you wish to plot.
#' @param TimeVar Variable specifying unit of time.
#' @param loess TRUE sets loess smoother on.  FALSE disables features.  Defaults to TRUE.
#' @param title Character string to pass on to ggtitle.
#' @param xaxis Character string to pass on to ggplot2.  Defaults to "Date" if left blank.  
#' @param yaxis Character string to pass on to ggplot2.  Defaults to coefficient name if left blank.
#' @param ... Other arguments passed down, currently not implemented. 
#' 
#' @author David L Stack, \email{stackd85@gmail.com}
#' 
#' @export
#' @method plot arfimaMLM
#' 
#' @examples 
#' require(fracdiff)
#' t = 100 # number of time points
#' n = 500 # number of observations within time point
#' N = t*n # total number of observations
#' 
#' ### generate fractional ARIMA Time Series for y_t, x1_t, z1_t, z2_t
#' set.seed(123)
#' y_t <- fracdiff.sim(t, d=0.4, mu=10)$series
#' x1_t <- fracdiff.sim(t, d=0.3, mu=5)$series
#' z1_t <- fracdiff.sim(t, d=0.1, mu=2)$series
#' z2_t <- fracdiff.sim(t, d=0.25, mu=3)$series
#' 
#' ### simulate data
#' data <- NULL; data$time <- rep(seq(1:t),each=n)
#' data <- data.frame(data)
#' data$x1 <- rnorm(N,rep(x1_t,each=n),2)
#' data$x2 <- rnorm(N,0,40)
#' data$z1 <- rnorm(N,rep(z1_t,each=n),3)
#' data$z2 <- rep(z2_t,each=n)
#' b1 <- 0.2+rep(rnorm(t,0,0.1),each=n)
#' data$y <- (b1*data$x1-0.05*data$x2+0.3*rep(z1_t,each=n)
#'            +0*data$z2+rnorm(N,rep(y_t,each=n),1))
#' m1 <- arfimaMLM(y.ydif ~ x1.xdif + x2 + z1.fd + z2.fd + (1 + x1.xdif|time)
#'                 , data = data, timevar = "time")
#' plot(x = m1, 
#'      CoefName = "x1.xdif", 
#'      TimeVar = "time")
plot.arfimaMLM <- function(x, ..., CoefName, TimeVar, 
                          loess = NULL, title = NULL,
                          xaxis = NULL, yaxis = NULL) {
  # Extract coefficients from ArfimaMLM object
  Model <- x
  Model1<- Model$result
  ran.coef<- coef(Model1)
  ran.coef2<- data.frame(ran.coef[[1]])
  rm(ran.coef)
  # Create dataframe of coefficients and dates to use in plot.
  slope<-  ran.coef2[,CoefName]
  Date<- as.character(row.names(ran.coef2))
  Date<- as.numeric(Date)
  plot.frame<- data.frame(Date, slope)
  # plot.frame<- ran.coef2 %>%
  #   select_(Date, Coefname)
  #plot coefficients
  ArfimaPlot<- ggplot(plot.frame, 
                      aes(x = Date, y = slope)) + 
    geom_line(aes())
  if (missing(title)) {
    ArfimaPlot<- ArfimaPlot
  } else ArfimaPlot<- ArfimaPlot + 
    ggtitle(title) +
    theme(plot.title = element_text(hjust = 0.5))
  if (missing(loess)){
    ArfimaPlot<- ArfimaPlot
  } else if (loess == TRUE) {
#    ArfimaPlot<- ArfimaPlot
    ArfimaPlot<- ArfimaPlot + geom_smooth(aes(),
                method = "loess")
  } else if (loess == FALSE) {
    ArfimaPlot<- ArfimaPlot
  }
  if (missing(xaxis)) {
    ArfimaPlot<- ArfimaPlot
  } else ArfimaPlot<- ArfimaPlot + 
      xlab(xaxis)
  if (missing(yaxis)) {
    ArfimaPlot <- ArfimaPlot
  } else ArfimaPlot <- ArfimaPlot +
    ylab(yaxis)
  
  return(ArfimaPlot)
}
pwkraft/ArfimaMLM documentation built on March 29, 2022, 3:20 p.m.