R: Ошибка при построении нормали для сравнения с моим распределением данных - PullRequest
0 голосов
/ 22 октября 2019

У меня есть следующий вектор с именем Datadiff:

  [1]   0.00000  -0.01415   2.68350  -4.26980  -2.28975  -1.27000   6.63400  -3.13965  -1.67050  -0.28920  -0.42435
 [12]  -1.36785  -1.97000   6.62200  -0.82470  -1.99530   1.84275  -4.70575   3.67150  -1.00900  -1.00205  -0.77350
 [23]  -0.86150  -2.12430   2.20255   2.03850  -0.19295   1.83205   2.04300  -3.51635  -3.32000   9.07800  -0.60280
 [34]  11.15045  -8.44820  -9.49590   0.13885  -1.06000   3.85800  -1.27255   0.35010   3.38085  -5.70870   5.08605
 [45]  -6.67000   6.57600  -4.48095   0.05825   3.35215   5.94525  -8.91095  -1.21000   7.99800  -2.21730  13.66610
 [56] -20.52355   0.68220   3.64315  -0.46000   4.08800   5.76705   3.88775 -12.32920  -0.08525  -4.10745  -6.70000
 [67]   8.73800   5.17880  -8.20225  -1.61770  23.66225 -16.23860  -2.25000  -5.24400   8.21195  19.02100  -1.55330
 [78] -10.74680 -13.19870   4.32000  -3.45600   0.29670   2.22515  -1.77935  -0.26525   0.32630   0.37000  10.26200
 [89]   5.38240 -15.50855  -0.65105   6.84655  -9.38970   1.70000   5.43200  10.17880   2.98075 -10.85725  -6.22135
[100]  -1.89010  -3.46000  -2.29000   4.69180   1.20210  -0.95415  -3.40435  -7.49000  -0.15000   7.49445  -0.14045
[111]   3.35195  -2.63695   0.83655 -12.01000  11.16400   9.18425  -3.91240   6.02935  -2.27235  -4.32110  -3.16000
[122]   1.17000   0.74640  -0.12670   2.26105   0.04115  -2.31855  -2.30000   1.81400   0.68420  -0.94390   0.67445
[133]   7.34260  -8.99235  -3.63000   4.55800   5.04180  -5.30290   0.96565   2.37130   0.33100  -2.53000   6.50400
[144]   2.81535  -0.90240  -2.62860  -3.69285   0.61605  -5.81000   5.32800  -0.39985   0.52635   0.39335   0.56145
[155]  -0.92660  -1.53000  10.51400  -3.33635  -4.52020  -1.85500   0.36355 -11.26815   1.62000  10.89000  -0.51330
[166]  -9.56000  -1.79115 -14.12515  14.49105  -6.90000   2.76400  19.79725  -1.36595  -5.14430   1.67195  -9.18290
[177]  -3.72000  18.14200  -2.22105  -5.47200  -1.74350   7.38740  -2.16620 -12.96000  12.10000   1.05790   4.37990
[188]   1.46360  -1.55485  -5.30650  -3.08000   9.59000  -0.70365  -0.29480   0.08570  -3.14220  -4.90170  -4.05690
[199]   0.82470  -0.61950   0.58260  -8.70000  16.49600  -2.05375  -1.15520   0.74145   0.49035  -9.79180  -1.06000
[210]   9.70800  -2.02660  -0.83785   1.14500  -1.46245  -4.42830 -16.34000  18.55000   1.93415  -3.23050  -3.63245
[221]   7.33200  -5.96870  -0.93000   2.03600   5.73760  -2.35745   5.82470  -2.38335  -5.71340  -7.60000  15.42000
[232]  -1.36575  -1.38560  -5.07790   5.94480 -10.58400  -4.41000  11.21400   0.33840   1.42130  -3.75035   7.43265
[243]   2.78635 -15.19000  -1.36800  16.65735  -3.12405  -2.51290   3.51920  -9.63090  -0.95000  21.88000  -6.28715
[254]   6.66370   0.89995  -3.76125 -10.85840  -3.51000  13.74800  -3.02445   0.29100   0.33760   0.13705  -7.97660
[265]  -1.15000   6.79400  -3.98745  -1.46300   2.38375   1.73950 -10.64685  -7.78000  15.32400   2.30850  -2.49665
[276]  -2.86120  -1.30305  -2.09495  -2.23000  11.44800  -0.74900   0.83135  -3.44695   0.31755  -4.00500  -8.25000
[287]  16.45800   3.01845   1.72375  -3.42175  -2.29725  -4.55095  -8.92000  11.56400  -0.85435  -0.98450   1.37875
[298]  -0.05225  -1.79065  -3.24000   6.89800  -2.88615   1.89680  -1.71995   1.23670   4.76065   1.91705   4.91885
[309]  -3.09340  -9.62280  -6.96000  10.17000   1.00150  -0.27075   3.60460   3.91925 -12.29470  -0.15000  11.58200
[320]   1.95930  -9.38075  -1.46460  -5.29375  -1.29280  -2.95000  10.18800  -1.85795  -2.52690   3.46760   0.71515
[331]  -3.87005  -4.41000  14.17200   5.00880  -1.75035  -7.26945  -4.97815   0.33920  -4.99000  -0.38000  13.42315
[342]   0.44745 -12.82670   5.65645  -1.60595  -4.19000  10.42800  -0.91455   2.01905  -1.22470  -6.72005  -2.37540
[353]  -6.62000  11.03000  -0.16420   4.67650   2.10160  -5.40605  -5.63525  -7.84000  13.38400  -4.76505  -2.09520
[364]  -0.21815  -5.53800   0.89805   0.84000  -3.79800  15.20915  -5.04190   2.85030  -3.54720   5.66655  -2.09000
[375]  -3.51800  -2.05115   4.75600   1.64015  -2.93645  -2.78400  -4.60000   7.83000   7.58955  -4.06370  -3.14205
[386]  -1.00330  -0.78590  -3.09000  16.99000  -1.75450  -0.17840  -6.17105   3.67985  -7.73185  -1.45000  -0.48000
[397]  -0.03625   0.64765  -0.88770   4.37420  -3.41025  -4.16000   2.30600   3.64675   0.65160  -1.86050   4.44755
[408]  -4.64435   0.59000   7.70800   0.13515  -3.63075   4.07175  -3.44855   1.61700  -2.69000   6.99000  -6.32835
[419]   0.06035   0.66905   2.09225  -8.90405   2.90000  -5.14800   1.27180  11.69970  18.22670  -5.02240 -11.80655
[430] -11.48000  22.61800  -2.89710 -21.47555   4.60950   0.11945  -1.44270  -1.49000   2.91800   5.56680  -4.15875
[441]  -0.84530  -4.80830   2.38945   0.68000   6.40800   1.46045   6.47015  -3.83785   3.01765 -13.76620   3.80000
[452]   7.04600  -9.70915   5.06240  -4.42255  -4.30895  -0.48565   1.48000   1.01400   0.41325   4.26235  -0.52185
[463]  -2.46555   0.05000  -1.16000  -0.26780   2.31460   7.02870   0.74975  -6.08455  -2.63000   2.65200  -5.02335
[474]   2.71820  14.30270 -14.36495  -2.38640  -4.52000   8.25600  -3.06855  -0.76365   4.48095   5.21815  -4.52365
[485]  -1.52000  12.16000  -1.96610   8.04025  10.50445 -17.61300 -13.53880  -6.99000   7.25800   9.58530   3.87375
[496]  -2.82975   2.68740 -11.12640  -3.71000  -2.09800  11.13375  -4.50495  -4.49020   3.06840  -3.24390   2.42000
[507]  10.54600  -6.76650  -1.39590   2.19665  -2.40210  -2.70515  -4.53000   6.81400  -2.62700   1.78745  -2.80275
[518]   5.00345  -5.79180  -0.41000   3.71200   1.10155  -0.99680  -1.49750   2.46750  -3.66735  -1.33000   1.89400
[529]   2.14050  -0.45820  -2.64025   1.64115  -2.63910   2.86000  -0.43000  -0.02565  -0.69105   0.18560  -0.08540
[540]  -2.21505  -2.10000   7.17400   4.28040  -3.66650  -1.47560   1.35750  -2.93310  -2.36000  -0.04400   3.63380
[551]   1.57210   0.75930  -1.84085  -1.37640  -3.31000   4.11800   0.87930   3.56600  -0.25750  -2.47415  -2.73090
[562]  -3.30000   5.78600  -3.72375  -0.40445   2.40250   1.75210  -3.30980  -2.22000   4.11600  -1.04385   1.09985
[573]   0.12310  -0.10000   0.85585  -3.19015   2.50105   2.59145  -9.87755   4.65000   4.64000  -0.38080  -2.83590
[584]   2.52765  -0.46655  -1.21260  -1.33000   7.19000   5.16895 -11.32840   5.50915  -0.29850  -4.34405  -1.16000
[595]   1.99200   0.94850  -0.11665  -1.32835   3.87565  -4.49720  -1.83000  10.71400  -3.08910  -2.57270   0.14165
[606]   0.67460  -3.13245 -10.58000  11.04000   4.67700  -0.76965  -8.59540   5.31355  -0.48550  -9.83000  13.65000
[617]   1.98135  -1.52210  -2.02930  -1.18050  -6.26230   4.41000  -1.84800  12.20420  -0.73490  -5.45060   2.30475
[628]  -5.93990  -0.98000   8.14800   0.68375  -1.57665  -1.89040   1.30625  -6.66715  -1.09000   5.37200   6.03350
[639]  -2.11330  -3.12840   7.26250  -9.10530  -7.88000   6.99800   1.24900  -1.71020  -0.42185   1.02915  -2.83645
[650]  -2.63000   5.59000   0.04545   2.03685   5.21010  -2.90810  -7.58070  -0.74000   4.79800  -0.78570   6.57080
[661]  -1.00055   0.71075  -8.20515  -2.34000   8.04000   5.03540   3.79530   1.00215  -4.29350  -8.57975  -3.60000
[672]  10.74600   0.03990  -1.48135   1.15855  -2.66635  -6.04255  -1.27000   6.89800   1.07795  -1.42805  -4.52400
[683]   3.48210 -10.79030   1.01000  10.82400  -2.22530  -0.37040  -3.96645   3.98060  -8.26920   4.43000   2.02000
[694]   5.57785   1.46695  -5.79030   1.21475  -4.58515  -0.81000  13.70000   1.10770   4.14880  -6.56570  -3.85860
[705]  -5.86435  -2.84000   8.22800   0.06675  -0.60955  -2.53285   1.06340   2.27175   1.30000   7.91000 -11.03340
[716]  -4.43140   3.40160   0.42545  -4.02465  -4.39000  14.80000 -11.49595   3.76795   9.23025  -3.32815  -7.07725
[727]  -2.19000  11.81600   0.91175  -4.06940  -3.73460  -1.05750  -6.47590  -0.52000   7.66400   6.21025  -2.67520
[738]   1.26305  -1.17585  -6.75250  -4.32000  11.27200  -5.91505   1.08715  -0.00055  -3.66290   3.58325  -6.39000
[749]  13.22600   0.88190

Я хочу сравнить распределение этих данных с распределением данных нормалей, поэтому у меня есть следующий код, чтобы попытаться это сделать, онсначала создает гистограмму вектора, затем вычисляет соотношение между частотой и плотностью, и, наконец, я перекрываю нормаль к гистограмме, используя функцию линий и множитель, который я только что вычислил:

histogram <- hist(Datadiff, breaks = 40)
multiplier2 <- unique(histogram$counts/histogram$density)[1]
lines(x = Datadiff, dnorm(Datadiff, sd = sd(Datadiff))*multiplier2, lwd = 3, lty = 2)

Когдасравнение строится, я получаю грязные линии вместо нормального распределения:

enter image description here

Может кто-нибудь указать, в чем проблема?

Ответы [ 2 ]

1 голос
/ 22 октября 2019

Если вы хотите сохранить частоту на оси Y вместо плотности, просто используйте последовательность в наблюдаемом диапазоне в dnorm.

Ваш старый код

histogram <- hist(Datadiff, breaks = 40)
multiplier2 <- unique(histogram$counts/histogram$density)[1]

Новыймодификация: создать последовательность, охватывающую диапазон значений Datadiff

seq1 <- seq(min(Datadiff),max(Datadiff),.1)

Построить эту последовательность на гистограмме

lines(x = seq1, dnorm(seq1,sd = sd(Datadiff))*multiplier2, lwd = 3, lty = 2)

Вы также можете заменить среднее значение dnorm на наблюдаемое среднее значение mean = mean(Datadiff) enter image description here

1 голос
/ 22 октября 2019

Вы можете использовать curve. Установите freq=FALSE в гистограмме, чтобы получить согласованные плотности на обоих графиках.

hist(Datadiff, breaks=40, freq=FALSE)
curve(dnorm(x, mean=mean(Datadiff), sd=sd(Datadiff)), 
      add=TRUE, col=2, lty=2)
legend("topright", c("empirical", "theoretical normal"), cex=.8, lty=1:2, col=1:2)

(Если вы хотите сравнить со стандартным нормальным, не используйте mean=mean(Datadiff), sd=sd(Datadiff), позвольте вашему dnorm(.) по умолчанию mean=0, sd=1.)

Производит

enter image description here

Данные

Datadiff <- c(-1.36785, -0.8615, 11.15045, -6.67, -20.52355, 8.738, -10.7468, 
5.3824, -1.8901, 3.35195, 1.17, 7.3426, 2.81535, -0.9266, -9.56, 
-3.72, 1.4636, 0.8247, 9.708, 7.332, -1.36575, 2.78635, 6.6637, 
-1.15, -2.8612, 16.458, -0.05225, -3.0934, 1.9593, -3.87005, 
0.44745, -6.62, -0.21815, -3.518, -1.0033, -0.03625, -4.64435, 
0.06035, -11.48, -0.8453, 7.046, -2.46555, 2.7182, -1.52, -2.82975, 
10.546, 5.00345, 2.1405, -2.21505, 1.5721, -3.3, 0.1231, 2.52765, 
1.992, 0.6746, 1.98135, -5.9399, -2.1133, -2.63, -1.00055, 10.746, 
3.4821, 5.57785, -5.86435, -4.4314, -2.19, 1.26305, 13.226, -0.01415, 
-1.97, -2.1243, -8.4482, 6.576, 0.6822, 5.1788, -13.1987, -15.50855, 
-3.46, -2.63695, 0.7464, -8.99235, -0.9024, -1.53, -1.79115, 
18.142, -1.55485, -0.6195, -2.0266, -5.9687, -1.3856, -15.19, 
0.89995, 6.794, -1.30305, 3.01845, -1.79065, -9.6228, -9.38075, 
-4.41, -12.8267, 11.03, -5.538, -2.05115, -0.7859, 0.64765, 0.59, 
0.66905, 22.618, -4.8083, -9.70915, 0.05, 14.3027, 12.16, 2.6874, 
-6.7665, -5.7918, -0.4582, -2.1, 0.7593, 5.786, -0.1, -0.46655, 
0.9485, -3.13245, -1.5221, -0.98, -3.1284, 5.59, 0.71075, 0.0399, 
-10.7903, 1.46695, -2.84, 3.4016, 11.816, -1.17585, 0.8819, 2.6835, 
6.622, 2.20255, -9.4959, -4.48095, 3.64315, -8.20225, 4.32, -0.65105, 
-2.29, 0.83655, -0.1267, -3.63, -2.6286, 10.514, -14.12515, -2.22105, 
-5.3065, 0.5826, -0.83785, -0.93, -5.0779, -1.368, -3.76125, 
-3.98745, -2.09495, 1.72375, -3.24, -6.96, -1.4646, 14.172, 5.65645, 
-0.1642, 0.89805, 4.756, -3.09, -0.8877, 7.708, 2.09225, -2.8971, 
2.38945, 5.0624, -1.16, -14.36495, -1.9661, -11.1264, -1.3959, 
-0.41, -2.64025, 7.174, -1.84085, -3.72375, 0.85585, -1.2126, 
-0.11665, -10.58, -2.0293, 8.148, 7.2625, 0.04545, -8.20515, 
-1.48135, 1.01, -5.7903, 8.228, 0.42545, 0.91175, -6.7525, -4.2698, 
-0.8247, 2.0385, 0.13885, 0.05825, -0.46, -1.6177, -3.456, 6.84655, 
4.6918, -12.01, 2.26105, 4.558, -3.69285, -3.33635, 14.49105, 
-5.472, -3.08, -8.7, 1.145, 2.036, 5.9448, 16.65735, -10.8584, 
-1.463, -2.23, -3.42175, 6.898, 10.17, -5.29375, 5.0088, -1.60595, 
4.6765, 0.84, 1.64015, 16.99, 4.3742, 0.13515, -8.90405, -21.47555, 
0.68, -4.42255, -0.2678, -2.3864, 8.04025, -3.71, 2.19665, 3.712, 
1.64115, 4.2804, -1.3764, -0.40445, -3.19015, -1.33, -1.32835, 
11.04, -1.1805, 0.68375, -9.1053, 2.03685, -2.34, 1.15855, 10.824, 
1.21475, 0.06675, -4.02465, -4.0694, -4.32, -2.28975, -1.9953, 
-0.19295, -1.06, 3.35215, 4.088, 23.66225, 0.2967, -9.3897, 1.2021, 
11.164, 0.04115, 5.0418, 0.61605, -4.5202, -6.9, -1.7435, 9.59, 
16.496, -1.46245, 5.7376, -10.584, -3.12405, -3.51, 2.38375, 
11.448, -2.29725, -2.88615, 1.0015, -1.2928, -1.75035, -4.19, 
2.1016, -3.798, -2.93645, -1.7545, -3.41025, -3.63075, 2.9, 4.6095, 
6.408, -4.30895, 2.3146, -4.52, 10.50445, -2.098, -2.4021, 1.10155, 
-2.6391, -3.6665, -3.31, 2.4025, 2.50105, 7.19, 3.87565, 4.677, 
-6.2623, -1.57665, -7.88, 5.2101, 8.04, -2.66635, -2.2253, -4.58515, 
-0.60955, -4.39, -3.7346, 11.272, -1.27, 1.84275, 1.83205, 3.858, 
5.94525, 5.76705, -16.2386, 2.22515, 1.7, -0.95415, 9.18425, 
-2.31855, -5.3029, -5.81, -1.855, 2.764, 7.3874, -0.70365, -2.05375, 
-4.4283, -2.35745, -4.41, -2.5129, 13.748, 1.7395, -0.749, -4.55095, 
1.8968, -0.27075, -2.95, -7.26945, 10.428, -5.40605, 15.20915, 
-2.784, -0.1784, -4.16, 4.07175, -5.148, 0.11945, 1.46045, -0.48565, 
7.0287, 8.256, -17.613, 11.13375, -2.70515, -0.9968, 2.86, -1.4756, 
4.118, 1.7521, 2.59145, 5.16895, -4.4972, -0.76965, 4.41, -1.8904, 
6.998, -2.9081, 5.0354, -6.04255, -0.3704, -0.81, -2.53285, 14.8, 
-1.0575, -5.91505, 6.634, -4.70575, 2.043, -1.27255, -8.91095, 
3.88775, -2.25, -1.77935, 5.432, -3.40435, -3.9124, -2.3, 0.96565, 
5.328, 0.36355, 19.79725, -2.1662, -0.2948, -1.1552, -16.34, 
5.8247, 11.214, 3.5192, -3.02445, -10.64685, 0.83135, -8.92, 
-1.71995, 3.6046, 10.188, -4.97815, -0.91455, -5.63525, -5.0419, 
-4.6, -6.17105, 2.306, -3.44855, 1.2718, -1.4427, 6.47015, 1.48, 
0.74975, -3.06855, -13.5388, -4.50495, -4.53, -1.4975, -0.43, 
1.3575, 0.8793, -3.3098, -9.87755, -11.3284, -1.83, -8.5954, 
-1.848, 1.30625, 1.249, -7.5807, 3.7953, -1.27, -3.96645, 13.7, 
1.0634, -11.49595, -6.4759, 1.08715, -3.13965, 3.6715, -3.51635, 
0.3501, -1.21, -12.3292, -5.244, -0.26525, 10.1788, -7.49, 6.02935, 
1.814, 2.3713, -0.39985, -11.26815, -1.36595, -12.96, 0.0857, 
0.74145, 18.55, -2.38335, 0.3384, -9.6309, 0.291, -7.78, -3.44695, 
11.564, 1.2367, 3.91925, -1.85795, 0.3392, 2.01905, -7.84, 2.8503, 
7.83, 3.67985, 3.64675, 1.617, 11.6997, -1.49, -3.83785, 1.014, 
-6.08455, -0.76365, -6.99, -4.4902, 6.814, 2.4675, -0.02565, 
-2.9331, 3.566, -2.22, 4.65, 5.50915, 10.714, 5.31355, 12.2042, 
-6.66715, -1.7102, -0.74, 1.00215, 6.898, 3.9806, 1.1077, 2.27175, 
3.76795, -0.52, -0.00055, -1.6705, -1.009, -3.32, 3.38085, 7.998, 
-0.08525, 8.21195, 0.3263, 2.98075, -0.15, -2.27235, 0.6842, 
0.331, 0.52635, 1.62, -5.1443, 12.1, -3.1422, 0.49035, 1.93415, 
-5.7134, 1.4213, -0.95, 0.3376, 15.324, 0.31755, -0.85435, 4.76065, 
-12.2947, -2.5269, -4.99, -1.2247, 13.384, -3.5472, 7.58955, 
-7.73185, 0.6516, -2.69, 18.2267, 2.918, 3.01765, 0.41325, -2.63, 
4.48095, 7.258, 3.0684, -2.627, -3.66735, -0.69105, -2.36, -0.2575, 
4.116, 4.64, -0.2985, -3.0891, -0.4855, -0.7349, -1.09, -0.42185, 
4.798, -4.2935, 1.07795, -8.2692, 4.1488, 1.3, 9.23025, 7.664, 
-3.6629, -0.2892, -1.00205, 9.078, -5.7087, -2.2173, -4.10745, 
19.021, 0.37, -10.85725, 7.49445, -4.3211, -0.9439, -2.53, 0.39335, 
10.89, 1.67195, 1.0579, -4.9017, -9.7918, -3.2305, -7.6, -3.75035, 
21.88, 0.13705, 2.3085, -4.005, -0.9845, 1.91705, -0.15, 3.4676, 
-0.38, -6.72005, -4.76505, 5.66655, -4.0637, -1.45, -1.8605, 
6.99, -5.0224, 5.5668, -13.7662, 4.26235, 2.652, 5.21815, 9.5853, 
-3.2439, 1.78745, -1.33, 0.1856, -0.044, -2.47415, -1.04385, 
-0.3808, -4.34405, -2.5727, -9.83, -5.4506, 5.372, 1.02915, -0.7857, 
-8.57975, -1.42805, 4.43, -6.5657, 7.91, -3.32815, 6.21025, 3.58325, 
-0.42435, -0.7735, -0.6028, 5.08605, 13.6661, -6.7, -1.5533, 
10.262, -6.22135, -0.14045, -3.16, 0.67445, 6.504, 0.56145, -0.5133, 
-9.1829, 4.3799, -4.0569, -1.06, -3.63245, 15.42, 7.43265, -6.28715, 
-7.9766, -2.49665, -8.25, 1.37875, 4.91885, 11.582, 0.71515, 
13.42315, -2.3754, -2.0952, -2.09, -3.14205, -0.48, 4.44755, 
-6.32835, -11.80655, -4.15875, 3.8, -0.52185, -5.02335, -4.52365, 
3.87375, 2.42, -2.80275, 1.894, -0.0854, 3.6338, -2.7309, 1.09985, 
-2.8359, -1.16, 0.14165, 13.65, 2.30475, 6.0335, -2.83645, 6.5708, 
-3.6, -4.524, 2.02, -3.8586, -11.0334, -7.07725, -2.6752, -6.39
)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...