0

在 pls 包中加载一个数据集,该数据集包括 60 个汽油样品在 401 个波长下的光谱强度,以及它们的辛烷值。在我的例子中:

library(pls)

#Data set

data(gasoline)

'data.frame':   60 obs. of  2 variables:
 $ octane: num  85.3 85.2 88.5 83.4 87.9 ...
 $ NIR   : 'AsIs' num [1:60, 1:401] -0.0502 -0.0442 -0.0469 -0.0467 -0.0509 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : chr  "1" "2" "3" "4" ...
  .. ..$ : chr  "900 nm" "902 nm" "904 nm" "906 nm"

我喜欢用辛烷值绘制 NIR 的 3D 表示,我的圣杯图是:

https://it.mathworks.com/help/stats/examples/partial-least-squares-regression-and-principal-components-regression.html

但是这个输出图是在 matlab 中创建的,而不是在 R 中创建的。原始代码是(https://it.mathworks.com/help/stats/examples/partial-least-squares-regression-and-principal-components-regression。 html ):

load spectra
whos NIR octane

[dummy,h] = sort(octane);
oldorder = get(gcf,'DefaultAxesColorOrder');
set(gcf,'DefaultAxesColorOrder',jet(60));
plot3(repmat(1:401,60,1)',repmat(octane(h),1,401)',NIR(h,:)');
set(gcf,'DefaultAxesColorOrder',oldorder);
xlabel('Wavelength Index'); ylabel('Octane'); axis('tight');
grid on

请问R中有任何想法或类似的功能/包吗?提前致谢!

4

1 回答 1

2

这给出了类似于 Matlab 图的内容:

library(rgl) 
library(pls) 
data(gasoline) 
str(gasoline) 
gasoline$ID <- seq(1:length(gasoline[,1])) 
open3d() 
x <- gasoline$octane 
y <- gasoline$NIR 
z <- gasoline$ID 
plot3d(x, 1:ncol(y), y, type = "n", xlab = "", ylab = "", zlab = "")
cols <- rainbow(1000)[(x - min(x))/(max(x) - min(x))*999 + 1]
for (i in seq_along(x))
  lines3d(x[i], 1:ncol(y), y[i,], col = cols[i])

截屏

有几个区别:

  • 坐标系的手性。(辛烷值在 rgl 图中向观察者增加。)如果你真的想要 Matlab 风格,你可以在rglusing中改变它。par3d("userMatrix" = par3d("userMatrix") %*% diag(c(-1, 1,1,1)))
  • 我没有画轴标签。默认的rgl有点丑,我懒得用mtext3d画更好的了。
  • 我没有打扰背景网格。我不喜欢它们,但如果你真的想要它们,你可以用grid3d.
  • Matlab 不显示透视图。如果这是您想要的,请使用par3d(FOV = 0).
  • 纵横比不同。我认为 Matlab 正在使用类似aspect3d(1, 1, 0.75).
  • 颜色。 rgl使用 R 颜色系统,因此,如果您能找到比 更喜欢的调色板rainbow(1000),请使用它。
  • 价格。
于 2019-04-20T19:36:52.213 回答