R中的BayesTree包绘图提取

时间:2022-05-18 14:58:24

I am using the BayesTree package in R. Using the author's example:

我在R中使用BayesTree包。使用作者的例子:

##simulate data (example from Friedman MARS paper)
f = function(x){
10*sin(pi*x[,1]*x[,2]) + 20*(x[,3]-.5)^2+10*x[,4]+5*x[,5]
}
sigma = 1.0 #y = f(x) + sigma*z , z~N(0,1)
n = 100 #number of observations
set.seed(99)
x=matrix(runif(n*10),n,10) #10 variables, only first 5 matter
Ey = f(x)
y=Ey+sigma*rnorm(n)

##run BART
set.seed(99)
bartFit = bart(x,y)
plot(bartFit) # plot bart fit

When the plot(bartFit) is executed it produces two plots "The plot method sets mfrow to c(1,2) and makes two plots." I am looking to only use the second plot produced from this command. Is there an easy way to extract only the second plot?

当执行绘图(bartFit)时,它会生成两个图“绘图方法将mfrow设置为c(1,2)并绘制两个图。”我期待只使用从这个命令产生的第二个图。有没有简单的方法只提取第二个情节?

This is provided as well:

这也是提供的:

## S3 method for class 'bart':
plot((
   x,
   plquants=c(.05,.95), cols =c('blue','black'),
   ...))

I am relatively new to R, in general if a command gives more then 1 plot how can you choose a specific plot you would like? For example in the linear model (lm) plot command you also get multiple plots.

我对R比较新,一般来说如果一个命令给出超过1个情节你怎么能选择你想要的特定情节?例如,在线性模型(lm)plot命令中,您还可以获得多个图。

1 个解决方案

#1


1  

Ok, I don't have a solution, but maybe I'm onto something. First, I tried to figure which list elements from bartFit are plotted on the second plot. You can look at the structure of the bart using str(bartFit). It seems that second plot is created from $y and $yhat.train.mean, which seems right when you look at it:

好吧,我没有解决方案,但也许我正在做点什么。首先,我试图在第二个图上计算出来自bartFit的列表元素。您可以使用str(bartFit)查看bart的结构。似乎第二个图是从$ y和$ yhat.train.mean创建的,当你看它时看起来是正确的:

plot(bartFit$yhat.train.mean ~ bartFit$y)

but it's obviously missing the bars. So I decided to change the strategy and traceback() to see how those plots are created. No luck, until I changed the name of x-scale to force an error.

但它显然错过了酒吧。所以我决定改变策略和traceback()来看看这些图是如何创建的。没有运气,直到我更改x-scale的名称以强制出错。

plot(bartFit, xlab="something")

Which gave me only the first plot, error message, but also ability to traceback(). There, I could see that the second plot is created using:

这给了我第一个情节,错误信息,还有追溯能力()。在那里,我可以看到第二个图是使用以下方式创建的:

4: plot.default(x$y, qm, ylim = range(ql, qu), xlab = "y", ylab = "posterior interval for E(Y|x)", ...)
3: plot(x$y, qm, ylim = range(ql, qu), xlab = "y", ylab = "posterior interval for E(Y|x)", ...)

but that's not good. I have no idea what is qm, or where ylim ranges come from. So I started playing with par. There I discovered very, very crude solution to use par argument mfg to "force" plotting only the second plot. This will still leave empty space for one plot, but won't plot it. But it behaves strangely as well. Tricky one.

但那并不好。我不知道什么是qm,或ylim范围来自哪里。所以我开始玩标准杆。在那里,我发现非常非常粗糙的解决方案,使用par参数mfg“强制”仅绘制第二个绘图。这仍将为一个地块留下空白空间,但不会绘制它。但它的表现也很奇怪。棘手的一个。

plot(bartFit)
par(mfg=c(1,2))
plot(bartFit)

#1


1  

Ok, I don't have a solution, but maybe I'm onto something. First, I tried to figure which list elements from bartFit are plotted on the second plot. You can look at the structure of the bart using str(bartFit). It seems that second plot is created from $y and $yhat.train.mean, which seems right when you look at it:

好吧,我没有解决方案,但也许我正在做点什么。首先,我试图在第二个图上计算出来自bartFit的列表元素。您可以使用str(bartFit)查看bart的结构。似乎第二个图是从$ y和$ yhat.train.mean创建的,当你看它时看起来是正确的:

plot(bartFit$yhat.train.mean ~ bartFit$y)

but it's obviously missing the bars. So I decided to change the strategy and traceback() to see how those plots are created. No luck, until I changed the name of x-scale to force an error.

但它显然错过了酒吧。所以我决定改变策略和traceback()来看看这些图是如何创建的。没有运气,直到我更改x-scale的名称以强制出错。

plot(bartFit, xlab="something")

Which gave me only the first plot, error message, but also ability to traceback(). There, I could see that the second plot is created using:

这给了我第一个情节,错误信息,还有追溯能力()。在那里,我可以看到第二个图是使用以下方式创建的:

4: plot.default(x$y, qm, ylim = range(ql, qu), xlab = "y", ylab = "posterior interval for E(Y|x)", ...)
3: plot(x$y, qm, ylim = range(ql, qu), xlab = "y", ylab = "posterior interval for E(Y|x)", ...)

but that's not good. I have no idea what is qm, or where ylim ranges come from. So I started playing with par. There I discovered very, very crude solution to use par argument mfg to "force" plotting only the second plot. This will still leave empty space for one plot, but won't plot it. But it behaves strangely as well. Tricky one.

但那并不好。我不知道什么是qm,或ylim范围来自哪里。所以我开始玩标准杆。在那里,我发现非常非常粗糙的解决方案,使用par参数mfg“强制”仅绘制第二个绘图。这仍将为一个地块留下空白空间,但不会绘制它。但它的表现也很奇怪。棘手的一个。

plot(bartFit)
par(mfg=c(1,2))
plot(bartFit)