绘图函数错误
Plot function error
我运行在不同的数据集上多次使用 leaps
包。这会产生许多不同的数字。因为我多次尝试 运行 它,所以我想将其转换为绘图函数,以便更轻松地应用它。但是我还不擅长函数,而且出现了一些相当奇怪的错误 - 我怀疑这是因为我在某处错误地编写了函数!
FUNCTIONNAME <- function(A, B, C){
plot.new()
vp0 <- viewport(x=0, y = 0.9, just = c("center"))
vp1 <- viewport(x=0,y=0,width=0.5, height=1, just = c("left", "bottom"))
vp2 <- viewport(x=0.5,y=0,width=0.5, height=1, just = c("left", "bottom"))
pushViewport(vp1)
par(new=TRUE, fig=gridFIG())
plot.regsubsetsMOD((A), scale = "bic", main = NULL, cex.main = 0.5, cex.axis = 0.65)
grid.text("BIC", 0.55, 0.825)
upViewport()
pushViewport(vp2)
par(new=TRUE, fig=gridFIG())
plot.regsubsetsMOD((A), scale = "r2", main = NULL, cex.main = 0.5, cex.axis = 0.65)
grid.text("R2", 0.55, 0.825)
upViewport()
pushViewport(vp0)
grid.text("B")
dev.copy(png,"C", height = 400, width = 1000)
dev.off()
}
FUNCTIONNAME(A = "Aut_Q_PCA_Models", B = "'textstring'", C = "'textstring.png'" )
我得到的错误是:
"Error in lsum$rsq : $ operator is invalid for atomic vectors"
这让我感到困惑,因为 lsum$rsq
在 plot.regsubsetsMOD
函数中,而不是我的绘图函数。 plot.regsubsetsMOD
函数正常工作,所以我相信错误不在那里 - 它在我绘制 viewport
的新函数中。任何帮助将不胜感激!
#
这是页面的完整代码:
library(leaps)
library(grid)
library(gridBase)
#####Apply leaps to first data set
setwd("J:/Academic papers/Dissertation journal paper/Version 3/New analysis/R code/Full data/AutumnLIFE-ChalkRiver")
AutumnLIFEChalkRiver <- read.csv("AutumnLIFE-ChalkRiver.csv",header=T)
attach(AutumnLIFEChalkRiver)
Aut_Q_PCA <- AutumnLIFEChalkRiver[,c(3, 6, 7, 10, 12, 16)]
Aut_Q_PCA_Models <-regsubsets(AutumnChalkLife~.,
data=Aut_Q_PCA,
nbest=5)
#####Generate figure
setwd("J:/R/Leaps")
FUNCTIONNAME(A ="Aut_Q_PCA_Models", B = "'textstring'", C = "'textstring.png'" )
#
代码 plot.regsubsetsMOD
是对 leaps
代码的修改,但它非常小,我只是在几行中添加了 ...
以便我可以将绘图更改为更清晰:
plot.regsubsetsMOD <- function (x, labels = obj$xnames, main = NULL, scale = c("bic",
"Cp", "adjr2", "r2"), col = gray(seq(0, 0.9, length = 10)),
...)
{
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,
1e-04)), -log(pmax(yscale, 1e-04)))
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 = "",
col = col)
laspar <- par("las")
on.exit(par(las = laspar))
par(las = 2)
axis(1, at = 1:np, labels = labels, ...) #Modified
axis(2, at = 1:nmodels, labels = signif(yscale[index], 2), ...) #Modified
if (!is.null(main))
title(main = main, ...) #Modified
box()
invisible(NULL)
}
主要要理解的是"A"
是一个字符串,包含字母A。即使变量名为 A
是包含字母 Aut_Q_PCA_Models 的字符串,它仍然如此。换句话说 "A"
和 A
是不同的东西(只要一个定义了 A <- "A"
)。 "B"
与 B
和 "C"
与 C
的情况相同。
您命名为 FUNCTIONNAME
的绘图函数调用函数 plot.regsubsetsMOD
。如您所写,它以
开头
plot.regsubsetsMOD <- function (x, labels = obj$xnames, main = NULL, scale = c("bic", Cp", "adjr2", "r2"), col = gray(seq(0, 0.9, length = 10)),
...)
{
obj <- x
lsum <- summary(obj)
par(mar = c(7, 5, 6, 3) + 0.1)
nmodels <- length(lsum$rsq)
您的绘图函数将 A = "Aut_Q_PCA_Models"
解析为它。因此,当基本上 R 尝试 运行 summary("Aut_Q_PCA_Models")$rsq
时,就会出现问题。对于 运行 单个字符串的摘要显然毫无意义。当您从 regsubsets
-class Aut_Q_PCA_Models,
命名您的模型时
FUNCTIONNAME(A = Aut_Q_PCA_Models, B = "textstring", C = "textstring.png" )
如果将 dev.copy(png,"C", height = 400, width = 1000)
行更改为 dev.copy(png, C, height = 400, width = 1000)
并将 grid.text("B")
行更改为 grid.text(B)
, 应该可以工作。
我运行在不同的数据集上多次使用 leaps
包。这会产生许多不同的数字。因为我多次尝试 运行 它,所以我想将其转换为绘图函数,以便更轻松地应用它。但是我还不擅长函数,而且出现了一些相当奇怪的错误 - 我怀疑这是因为我在某处错误地编写了函数!
FUNCTIONNAME <- function(A, B, C){
plot.new()
vp0 <- viewport(x=0, y = 0.9, just = c("center"))
vp1 <- viewport(x=0,y=0,width=0.5, height=1, just = c("left", "bottom"))
vp2 <- viewport(x=0.5,y=0,width=0.5, height=1, just = c("left", "bottom"))
pushViewport(vp1)
par(new=TRUE, fig=gridFIG())
plot.regsubsetsMOD((A), scale = "bic", main = NULL, cex.main = 0.5, cex.axis = 0.65)
grid.text("BIC", 0.55, 0.825)
upViewport()
pushViewport(vp2)
par(new=TRUE, fig=gridFIG())
plot.regsubsetsMOD((A), scale = "r2", main = NULL, cex.main = 0.5, cex.axis = 0.65)
grid.text("R2", 0.55, 0.825)
upViewport()
pushViewport(vp0)
grid.text("B")
dev.copy(png,"C", height = 400, width = 1000)
dev.off()
}
FUNCTIONNAME(A = "Aut_Q_PCA_Models", B = "'textstring'", C = "'textstring.png'" )
我得到的错误是:
"Error in lsum$rsq : $ operator is invalid for atomic vectors"
这让我感到困惑,因为 lsum$rsq
在 plot.regsubsetsMOD
函数中,而不是我的绘图函数。 plot.regsubsetsMOD
函数正常工作,所以我相信错误不在那里 - 它在我绘制 viewport
的新函数中。任何帮助将不胜感激!
这是页面的完整代码:
library(leaps)
library(grid)
library(gridBase)
#####Apply leaps to first data set
setwd("J:/Academic papers/Dissertation journal paper/Version 3/New analysis/R code/Full data/AutumnLIFE-ChalkRiver")
AutumnLIFEChalkRiver <- read.csv("AutumnLIFE-ChalkRiver.csv",header=T)
attach(AutumnLIFEChalkRiver)
Aut_Q_PCA <- AutumnLIFEChalkRiver[,c(3, 6, 7, 10, 12, 16)]
Aut_Q_PCA_Models <-regsubsets(AutumnChalkLife~.,
data=Aut_Q_PCA,
nbest=5)
#####Generate figure
setwd("J:/R/Leaps")
FUNCTIONNAME(A ="Aut_Q_PCA_Models", B = "'textstring'", C = "'textstring.png'" )
#
代码 plot.regsubsetsMOD
是对 leaps
代码的修改,但它非常小,我只是在几行中添加了 ...
以便我可以将绘图更改为更清晰:
plot.regsubsetsMOD <- function (x, labels = obj$xnames, main = NULL, scale = c("bic",
"Cp", "adjr2", "r2"), col = gray(seq(0, 0.9, length = 10)),
...)
{
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,
1e-04)), -log(pmax(yscale, 1e-04)))
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 = "",
col = col)
laspar <- par("las")
on.exit(par(las = laspar))
par(las = 2)
axis(1, at = 1:np, labels = labels, ...) #Modified
axis(2, at = 1:nmodels, labels = signif(yscale[index], 2), ...) #Modified
if (!is.null(main))
title(main = main, ...) #Modified
box()
invisible(NULL)
}
主要要理解的是"A"
是一个字符串,包含字母A。即使变量名为 A
是包含字母 Aut_Q_PCA_Models 的字符串,它仍然如此。换句话说 "A"
和 A
是不同的东西(只要一个定义了 A <- "A"
)。 "B"
与 B
和 "C"
与 C
的情况相同。
您命名为 FUNCTIONNAME
的绘图函数调用函数 plot.regsubsetsMOD
。如您所写,它以
plot.regsubsetsMOD <- function (x, labels = obj$xnames, main = NULL, scale = c("bic", Cp", "adjr2", "r2"), col = gray(seq(0, 0.9, length = 10)),
...)
{
obj <- x
lsum <- summary(obj)
par(mar = c(7, 5, 6, 3) + 0.1)
nmodels <- length(lsum$rsq)
您的绘图函数将 A = "Aut_Q_PCA_Models"
解析为它。因此,当基本上 R 尝试 运行 summary("Aut_Q_PCA_Models")$rsq
时,就会出现问题。对于 运行 单个字符串的摘要显然毫无意义。当您从 regsubsets
-class Aut_Q_PCA_Models,
FUNCTIONNAME(A = Aut_Q_PCA_Models, B = "textstring", C = "textstring.png" )
如果将 dev.copy(png,"C", height = 400, width = 1000)
行更改为 dev.copy(png, C, height = 400, width = 1000)
并将 grid.text("B")
行更改为 grid.text(B)
,应该可以工作。