[R] Problem on Histogtam
jim holtman
jholtman at gmail.com
Sat Aug 26 14:13:05 CEST 2006
try thie:
r <- rnorm(100,4,1)
hist(r, freq=FALSE)
r.f <- function(x)dnorm(x, mean(r), sd(r))
curve(r.f, from=min(r), to=max(r), add=TRUE, col='red')
On 8/26/06, stat stat <stat700004 at yahoo.co.in> wrote:
> Dear all,
>
> May be question seems trivial for most of the R
> users, but really at least for me, this comes out to
> be very problematic.
>
> Suppose I have the following data:
>
> > r
> [1] -0.0008179960 -0.0277968529 -0.0105731583
> -0.0254050262 0.0321847131 0.0328170674
> [7] 0.0431894392 -0.0217614918 -0.0218366946
> 0.0048939739 -0.0012212499 0.0032533579
> [13] -0.0081533269 -0.0098725606 -0.0099710008
> -0.0130281313 0.0121927094 0.0029209284
> [19] -0.0206279678 -0.0210624593 -0.0100415600
> -0.0266801063 -0.0241744954 0.0178453603
> [25] 0.0452204703 0.0133478839 0.0274220937
> 0.0091135676 0.0380304730 -0.0087719861
> [31] -0.0412893310 -0.0092243841 -0.0016863410
> -0.0080491856 -0.0293504020 0.0411898638
> [37] -0.0037902762 -0.0076239251 0.0193690266
> -0.0012518257 0.0259647803 0.0241169245
> [43] 0.0446589449 0.0536073638 -0.0028839241
> 0.0168251687 0.0095390230 -0.0213227695
> [49] -0.0519741069 -0.0287872498 -0.0035108287
> 0.0197336642 -0.0065347148 -0.0011576308
> [55] -0.0085304899 0.0162228355 0.0087736592
> 0.0007593015 0.0113208756 0.0229980462
> [61] 0.0370746078 0.0679558204 0.0401218485
> -0.0022225759 0.0188922496 -0.0230330422
> [67] -0.0261938985 -0.0503969590 -0.0069156569
> -0.0031277176 -0.0235967983 -0.0100287373
> [73] -0.0014409224 -0.0307491283 -0.0240794177
> -0.0091813958 0.0076570053 0.0621042105
> [79] 0.0156475454 0.0353589695 0.0078032627
> 0.0070719271 0.0026809668 -0.0111056164
> [85] -0.0088375830 0.0128902735 0.0279180644
> 0.0268407382 -0.0038363218 -0.0194055249
> [91] -0.0284881803 0.0016786977 -0.0145297369
> 0.0081356381 -0.0187430943 0.0123078477
> [97] 0.0148400700 -0.0070552955 0.0103975446
> 0.0109508312 0.0147422817 0.0218728566
> [103] -0.0041447533 -0.0096308931 0.0289322358
> 0.0216995495 0.0499431039 0.0717506367
> [109] 0.0032529169 -0.0040678022 -0.0331522073
> -0.0164263110 -0.0322093707 0.0542379373
> [115] 0.0259181864 0.0387076215 0.0044415488
> -0.0078513881 0.0411792612 0.0062837960
> [121] -0.0243481467 0.0328312315 -0.0520127424
> -0.0137024542 -0.0367509610 -0.0149753215
> [127] 0.0256530410 0.0376798081 0.0106952891
> 0.0525638932 0.0281572000 0.0324963843
> [133] -0.0170584812 -0.0063837550 0.0294482602
> -0.0118152654 -0.0169216729 0.0061436866
> [139] -0.0245630568 0.0093716894 -0.0122730454
> 0.0234516610 -0.0090283341 0.0059488575
> [145] -0.0104937762 -0.0123026519 0.0263488297
> -0.0087855249 0.0021441342 0.0204930031
> [151] -0.0079588435 -0.0021173987 0.0135674596
> 0.0053296384 -0.0004623209 0.0199159610
> [157] -0.0034056112 -0.0052445679 -0.0142760886
> -0.0158956129 -0.0137605967 -0.0007169316
> [163] 0.0236250062 0.0002334540 0.0007000350
> -0.0023353584 -0.0210285362 -0.0178275018
> [169] -0.0663226429 0.0098217383 -0.0111212836
> -0.0597472435 0.0371266051 -0.0247790755
> [175] 0.0337833736 0.0265364166 -0.0160458929
> 0.0085725944 0.0161645598 0.0700273748
> [181] -0.0308458190 0.0362889752 -0.0195430433
> 0.0284716741 0.0097766142 -0.0037131628
> [187] 0.0275183610 0.0276618128 0.0109410282
> -0.0302685208 0.0191074465 -0.0086197904
> [193] 0.0013309674 0.0169253408 -0.0010903937
> 0.0073913380 0.0330203607 0.0116667990
> [199] -0.0099917567 0.0035501760 -0.0231980036
> 0.0023441673 -0.0150120769 -0.0165581125
> [205] -0.0101568548 -0.0031118051 -0.0051333669
> 0.0312800833 -0.0050005540 -0.0109578129
> [211] -0.0222825618 -0.0388242167 -0.0037549915
> -0.0185102580 -0.0120483385 0.0336106003
> [217] 0.0146599484 0.0146757965 0.0117675040
> 0.0196041030 -0.0030932415 -0.0024371343
> [223] -0.0158751238 -0.0145292000 0.0050182587
> 0.0061245513 0.0139234228 -0.0116645558
> [229] -0.0475950303 -0.0346688548 0.0041559771
> -0.0246987117 0.0195623423 0.0167785091
> [235] -0.0133513332 -0.0113051093 0.0215167225
> -0.0009680543 -0.0141431277 -0.0150936925
> [241] -0.0183676066 -0.0117498159 0.0165629981
> -0.0248189789 -0.0133004960 -0.0132137229
> [247] -0.0429413799 -0.0055694937 0.0005589715
> 0.0168908903 0.0321518982 0.0007973422
> [253] 0.0573060001 -0.0078076206 -0.0253502245
> 0.0210426035 0.0025361414 -0.0091603694
> [259] -0.0064111003 -0.0126862543 -0.0067974118
> 0.0023581827 -0.0359703092 0.0185464260
> [265] -0.0007992541 -0.0193762790 0.0225694695
> 0.0052994295 -0.0039719369 0.0144872809
> [271] 0.0002614721 -0.0076105865 -0.0153971717
> 0.0056022555 -0.0104292963 -0.0189965007
> [277] 0.0079138085 0.0254944708 -0.0165646320
> -0.0162958522 -0.0107365106 0.0049696403
> [283] 0.0041225838 0.0000000000 -0.0035719230
> -0.0200173489 -0.0022490873 -0.0039481157
> [289] -0.0182497223 0.0157033919 -0.0154156635
> -0.0023041485 -0.0028876717 0.0000000000
> [295] -0.0195655241 0.0210104023 -0.0020234145
> -0.0026075634 0.0149729259 0.0045623119
> [301] 0.0431521368 0.0228966231 -0.0077530130
> 0.0285733724 -0.0091707704 -0.0137823544
> [307] 0.0092974504 -0.0076974501 0.0106017426
> 0.0481245566 0.0164469194 0.0356877294
> [313] 0.0002384643 0.0151446340 -0.0184883199
> 0.0000000000
>
> Now I want to plot a histogram:
>
> > histo = hist(r,freq=F,breaks=50)
> > histo$density
> [1] 1.582278 0.000000 0.000000 0.000000 1.582278
> 0.000000 0.000000 1.582278 3.164557
> [10] 0.000000 1.582278 0.000000 1.582278 1.582278
> 1.582278 1.582278 3.164557 3.164557
> [19] 4.746835 4.746835 4.746835 14.240506 6.329114
> 11.075949 15.822785 12.658228 17.405063
> [28] 17.405063 22.151899 25.316456 20.569620 9.493671
> 34.810127 22.151899 14.240506 14.240506
> [37] 17.405063 12.658228 14.240506 12.658228 9.493671
> 14.240506 14.240506 12.658228 9.493671
> [46] 7.911392 7.911392 11.075949 7.911392 1.582278
> 12.658228 3.164557 6.329114 3.164557
> [55] 4.746835 3.164557 3.164557 0.000000 3.164557
> 0.000000 3.164557 1.582278 1.582278
> [64] 0.000000 0.000000 1.582278 0.000000 1.582278
> 0.000000 3.164557
>
>
> Now I want to plot a NORMAL reference plot
>
> > x = rnorm(length(r),mean(r),sd(r))
> > curve(dnorm(x), add=T, col="red")
>
> But I am not getting what it should be. I get an
> straight line. Can anyone give me any suggestion?
>
> Also please let me know how R calculate "density" in
> "Hist" function
>
> Sincerely yours,
> Stat
>
>
> thanks in advance
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem you are trying to solve?
More information about the R-help
mailing list