从lars包改变剧情标题

时间:2015-04-02 15:50:54

标签: r plot lars

我一直在使用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") 

1 个答案:

答案 0 :(得分: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)