File: plot.R

package info (click to toggle)
r-cran-regsem 1.6.2+dfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 496 kB
  • sloc: cpp: 263; ansic: 15; makefile: 2
file content (122 lines) | stat: -rw-r--r-- 3,815 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#' Plot function for cv_regsem
#'
#' @param x An x from cv_regsem.
#' @param ... Other arguments.
#' @param pars Which parameters to plot
#' @param show.minimum What fit index to use
#' @param col A specification for the default plotting color.
#' @param type what type of plot should be drawn. Possible types are "p" for points, "l" for lines, or "b" for both
#' @param lwd line width
#' @param h_line Where to draw horizontal line
#' @param lty line type
#' @param xlab X axis label
#' @param ylab Y axis label
#' @param legend.x x-coordinate of legend. See ?legend
#' @param legend.y y-coordinate of legend. See ?legend
#' @param legend.cex cex of legend. See ?legend
#' @param legend.bg legend background color. See ?legend
#' @param grey.out Add grey to background
#' @method plot cvregsem
#' @export



plot.cvregsem <- function (x, ..., pars = NULL, show.minimum="BIC",
                              col = NULL, type = "l", lwd = 3,h_line=0,
                              lty = 1, xlab = NULL, ylab = NULL,
                              legend.x = NULL, legend.y = NULL,
                              legend.cex = 1, legend.bg=par("bg"), grey.out=FALSE)
{
  if (is.null(pars))
    pars <- x$pars_pen
  if (!(type %in% c("p", "b", "l")))
    stop("Unknown plot type given.")
  if (!class(x) == "cvregsem")
    stop("Specified x is not a x from cv_regsem(.")
  if (is.null(xlab))
    xlab <- "Penalty"
  if (is.null(ylab))
    ylab <- "Estimate"
  coef.mat <- x$parameters[, pars]
  if (is.null(col)) {
    colls <- colorspace::rainbow_hcl(length(pars))
  }
  else {
    if (length(col) < length(pars))
      col <- rep(col, ceiling(length(pars)/length(col)))
    colls <- col
  }

  # filter NA values in fit function
 if(is.null(dim(coef.mat))){
   ydat <- coef.mat
 }else{
   ydat <- coef.mat[, 1]
 }
  xdat <- x$fits[, "lambda"]
  rm.ids <- which(x$fits[,"conv"] != 0)
  if (length(rm.ids)>0) {
    xdat <- xdat[-rm.ids]
    ydat <- ydat[-rm.ids]
    coef.mat <- coef.mat[-rm.ids, ]
  }

  # grey-out colors based on threshold
  if (grey.out!=FALSE) {
    if (isTRUE(grey.out)) grey.out<-0.001
    min.id <- which.min(abs(x$fits[ x$fits[,"conv"]!=0 ,show.minimum]))
    min.pars <- coef.mat[min.id, ]
    min.pars.filt <- abs(min.pars)<grey.out
    colls[min.pars.filt] <- "grey"
  }

  # adjust plot limits relative to scale not by absolute increment
  plot(xdat, ydat, ylim = c(min(coef.mat) * 0.95, max(coef.mat) * 1.05),
       ylab = ylab, xlab = xlab,
       type = "n")

  if(is.null(dim(coef.mat))){

      if (type == "l" || type == "b")
        lines(xdat, coef.mat, lty = lty,
              col = colls, lwd = lwd)
      if (type == "p" || type == "b")
        points(xdat, coef.mat)

  }else{
    for (i in 1:(ncol(coef.mat))) {
      if (type == "l" || type == "b")
        lines(xdat, coef.mat[, i], lty = lty,
              col = colls[i], lwd = lwd)
      if (type == "p" || type == "b")
        points(xdat, coef.mat[, i])
    }
  }

  # draw horizontal line
  abline(a=h_line,b=0)

  # add minimum
  if (!is.null(show.minimum)) {
    min.id <- which.min(abs(x$fits[,show.minimum]))
    lambda <- x$fits[min.id,1]

    abline(v=lambda,lty=2)

    pnts <- x$parameters[min.id,pars]
    points(rep(lambda,length(pnts)),pnts, col=colls,cex=2, pch=19)
    points(rep(lambda,length(pnts)),pnts, col="black",cex=1)
  }
  # --

  # add legend
  if (!is.null(legend.x)) {
    graphics::legend(x=legend.x, y=legend.y,colnames(x$parameters[,pars]),
                     #lty=1,lwd=legend.lwd,
                     fill=colls,
                     cex=legend.cex,
                     y.intersp=0.75,x.intersp=0.5,
                     bg=legend.bg
                     )
  }
}