使用postResample分别绘制RMSE和RSquared

时间:2017-06-05 18:27:41

标签: r plot

我想在一个图中打印pred x obs,RMSE和Rsquared之间的相关性。当我使用" postResample"计算时,我将两个值组合在一起,但我希望它们分开。因此,当我要求R绘图时,它会将所有内容拼凑在一起。有人可以帮助将它分开绘制吗?

## Ploting residuals
axisRange <- extendrange(c(observed, predicted))
plot(observed, predicted,
 ylim = axisRange,
 xlim = axisRange,
 ylab = 'Predicted',
 xlab = 'Observed', las=1)

 ## correlation predicted x observed
 inform = as.character(round(cor(predicted, observed), digits = 2))
 text(x=10,y=46, paste('Corr =', inform))
 text(x=10,y=43, paste('RMSE =', inform))
 text(x=10,y=40, paste('RSquared =', inform))

 ## Adding reference line
 abline(0, 1, col = "darkgrey", lty = 2)

 ## Calculating Rsquared and RMSE
 postResample(predicted, observed)

1 个答案:

答案 0 :(得分:0)

您可以使用[]访问函数postResample()输出的第一个(RMSE)和第二个(R平方)元素。

以下是一个例子:

postResample(1:10,9:18)[1]

postResample(1:10,9:18)[2]

所以在你的代码中:

## Ploting residues
axisRange <- extendrange(c(observed, predicted))
plot(observed, predicted,
 ylim = axisRange,
 xlim = axisRange,
 ylab = 'Predicted',
 xlab = 'Observed', las=1)

 ## correlation predicted x observed
 inform = as.character(round(cor(predicted, observed), digits = 2))
 text(x=10,y=46, paste('Corr =', inform))
 text(x=10,y=43, paste('RMSE =', postResample(predicted, observed)[1]))
 text(x=10,y=40, paste('RSquared =', postResample(predicted, observed)[2]))

 ## Adding reference line
 abline(0, 1, col = "darkgrey", lty = 2)