0

我一直在使用 lars 包来做一些套索回归的工作,并且已经弄清楚了很多。它输出的图表对我来说仍然是个谜。我尝试编辑 plot.lars 函数以更改绘图标题,尝试在绘图调用中插入 main="TITLE" ,这只是将标题放在现有标题上。任何人都知道如何更改情节调用上方的“LASSO”文本?

library(lars)
data(diabetes) # load the data set
LL = lars(diabetes$x,diabetes$y,type="lasso")
plot(LL,xvar="step") 
4

1 回答 1

1

我想你必须像这样破解绘图方法(我评论了title命令):

plot.lars <- function (x, xvar = c("norm", "df", "arc.length", "step"), breaks = TRUE, 
                       plottype = c("coefficients", "Cp"), omit.zeros = TRUE, eps = 1e-10, 
                       ...) 
{
  object <- x
  plottype <- match.arg(plottype)
  xvar <- match.arg(xvar)
  coef1 <- object$beta
  if (x$type != "LASSO" && xvar == "norm") 
    coef1 = betabreaker(x)
  stepid = trunc(as.numeric(dimnames(coef1)[[1]]))
  coef1 <- scale(coef1, FALSE, 1/object$normx)
  if (omit.zeros) {
    c1 <- drop(rep(1, nrow(coef1)) %*% abs(coef1))
    nonzeros <- c1 > eps
    cnums <- seq(nonzeros)[nonzeros]
    coef1 <- coef1[, nonzeros, drop = FALSE]
  }
  else cnums <- seq(ncol(coef1))
  s1 <- switch(xvar, norm = {
    s1 <- apply(abs(coef1), 1, sum)
    s1/max(s1)
  }, df = object$df, arc.length = cumsum(c(0, object$arc.length)), 
  step = seq(nrow(coef1)) - 1)
  xname <- switch(xvar, norm = "|beta|/max|beta|", df = "Df", 
                  arc.length = "Arc Length", step = "Step")
  if (plottype == "Cp") {
    Cp <- object$Cp
    plot(s1, Cp, type = "b", xlab = xname, main = object$type, 
         ...)
    plot(s1, Cp, type = "b", xlab = xname, #main = object$type, 
         ...)
  }
  else {
    matplot(s1, coef1, xlab = xname, ..., type = "b", pch = "*", 
            ylab = "Standardized Coefficients")
    #title(object$type, line = 2.5)
    abline(h = 0, lty = 3)
    axis(4, at = coef1[nrow(coef1), ], labels = paste(cnums), 
         cex = 0.8, adj = 0)
    if (breaks) {
      axis(3, at = s1, labels = paste(stepid), cex = 0.8)
      abline(v = s1)
    }
  }
  invisible()
}

接着:

plot(LL,xvar="step", main = "my title")

或者

plot(LL,xvar="step")
title("my title", line = 3)
于 2015-04-02T16:03:17.823 回答