在R

2014-03-29 77 views
0

中运行width.SJ()时出现错误今天,我使用下面的代码来检查非参数密度估计的数据正态性。然而,我得到错误说:在R

width.SJ(bc.mdat[, 2]) : 
    no solution in the specified range of bandwidths 

所以,我不知道为什么会发生这个错误。因为我用另一个数据做得很好。感谢您的帮助

library(car) 
library(MASS) 
library(graphics) 
Q=read.table(text=' 
12.5 13.7 
14.5 16.5 
8.0 17.4 
9.0 11.0 
19.5 23.6 
8.0 13.2 
9.0 32.1 
7.0 12.3 
7.0 11.8 
9.0 24.4 
6.5 18.2 
10.5 22.0 
10.0 32.5 
4.5 18.7 
7.0 15.8 
8.5 15.6 
6.5 12.0 
8.0 12.8 
3.5 26.1 
8.0 14.5 
17.5 42.3 
10.5 17.5 
12.0 21.8 
6.0 10.4 
13.0 25.6 
') 
bc=powerTransform(as.matrix(Q)~1) 
summary(bc) 
bc.mdat<-bcPower(Q,bc$lambda) 
f2=kde2d(bc.mdat[,1],bc.mdat[,2],h=c(width.SJ(bc.mdat[,1]),width.SJ(bc.mdat[,2]))) 
persp(f2,phi=30,theta=20,d=5) 

回答

1

运用一些简单的调试说明什么问题是你的第二个列,即,

> width.SJ(bc.mdat[,2]) 
Error in width.SJ(bc.mdat[, 2]) : 
    no solution in the specified range of bandwidths 

的问题是,对于mc.mdat[,2],它看起来像这样:

[1] 1.267853 1.300541 1.309183 1.223900 1.353462 1.260837 1.390182 1.247041 1.238636 1.357801 1.316270 1.344016 1.391514 1.320446 1.293266 1.291090 1.242067 1.254902 1.366288 1.278246 1.417488 1.310098 
[23] 1.342756 1.211639 1.363888 

width.SJ函数中得到的值(只需输入width.SJ并按回车即可查看函数体),即

> fSD(lower, cnt, alph2, c1, n, d) 
[1] 0.003946731 
> fSD(upper, cnt, alph2, c1, n, d) 
[1] 0.002883121 

乘法将超过0,想必这是某种奇异的条件。

if (fSD(lower, cnt, alph2, c1, n, d) * fSD(upper, cnt, alph2, 
    c1, n, d) > 0) 
    stop("no solution in the specified range of bandwidths") 

这里,fSD是由function (h, x, alph2, c1, n, d) (c1/SDh(x, alph2 * h^(5/7), n, d))^(1/5) - h给出的功能。

我不确定这可能意味着什么,而不深入数学,但希望这粗略的调试会话足以让你开始。这个解决方案无疑是修复你的数据集或者理解它为什么是单数的。我已经尝试使用combn删除多达四个数据点,但遇到同样的问题,所以这是一个基本的完整性问题。