【问题标题】:How to pass the amount of decimals as an argument in an R plot.regsubsets?如何在 R plot.regsubsets 中将小数位数作为参数传递?
【发布时间】:2019-09-29 11:27:57
【问题描述】:

在 R 中,plot.regsubsets 的 y 轴总是四舍五入到两位数。我想定义自己使用多少位数。

从函数的来源来看,两位小数的舍入是硬编码的(line 33)。

调整这些图的 y 轴位数的最简单方法是什么?

【问题讨论】:

    标签: r plot visualization


    【解决方案1】:

    可以在每次启动 R 时重新定义整个函数,或者使用额外的参数 digits 扩展函数:

    plot.regsubsets<-function(x,labels=obj$xnames,main=NULL,
                              scale=c("bic","Cp","adjr2","r2"),
                              col=gray(seq(0,0.9,length=10)),digits=2,...){
        obj<-x
        lsum<-summary(obj)
        par(mar=c(7,5,6,3)+0.1)
        nmodels<-length(lsum$rsq)
        np<-obj$np
        propscale<-FALSE
        sscale<-pmatch(scale[1],c("bic","Cp","adjr2","r2"),nomatch=0)
        if (sscale==0)
            stop(paste("Unrecognised scale=",scale))
        if (propscale)
            stop(paste("Proportional scaling only for probabilities"))
    
        yscale<-switch(sscale,lsum$bic,lsum$cp,lsum$adjr2,lsum$rsq)
        up<-switch(sscale,-1,-1,1,1)
    
        index<-order(yscale*up)
    
        colorscale<- switch(sscale,
                            yscale,yscale,
                            -log(pmax(yscale,0.0001)),-log(pmax(yscale,0.0001)))
    
        image(z=t(ifelse(lsum$which[index,],
              colorscale[index],NA+max(colorscale)*1.5)),
              xaxt="n",yaxt="n",x=(1:np),y=1:nmodels,xlab="",ylab=scale[1],col=col)
    
        laspar<-par("las")
        on.exit(par(las=laspar))
        par(las=2)
        axis(1,at=1:np,labels=labels)
        axis(2,at=1:nmodels,labels=signif(yscale[index],digits))
    
        if (!is.null(main))
            title(main=main)
        box()
        invisible(NULL)
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-03
      • 2021-07-03
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多