Nicola Sturaro Sommacal (Quantide srl)
2010-Nov-02 10:04 UTC
[R] density() function: differences with S-PLUS
Hello! Someone know what are the difference between R and S-PLUS in the density() function? For example, I would like to reply this simple S-PLUS code in R, but I don't understand which parameter I should modify to get the same results. S-PLUS CODE: density(1:1000, width = 4) R-CODE: density(1:1000, bw = 4, window = "g", n = 50, cut = 0.75) I obtain the same x values, but different y values. I try also different examples, with different parameter. Can you help me? Thank you in advance. Nicola Sturaro [[alternative HTML version deleted]]
Dear Nicola, There are undoubtedly people here who are familiar with both S+ and R, but they may not always be around or get to every question. In that case there are (at least) two good options for you: 1) Say what you want mathematically (something of a universal language) or statistically 2) Rather than just give us S+ code, show sample data (e.g., 1:1000), and the values you would like obtained (in this case whatever the output from S+ was). This would let us *try* to figure out what happened and duplicate it in R.>From the arcane step of reading R's documentation for density (?density):width: this exists for compatibility with S; if given, and ?bw? is not, will set ?bw? to ?width? if this is a character string, or to a kernel-dependent multiple of ?width? if this is numeric. Which makes me wonder if this works for you (in R)? density(1:1000, width = 4) Cheers, Josh On Tue, Nov 2, 2010 at 3:04 AM, Nicola Sturaro Sommacal (Quantide srl) <mailinglist at sturaro.net> wrote:> Hello! > > Someone know what are the difference between R and S-PLUS in the density() > function? > > For example, I would like to reply this simple S-PLUS code in R, but I don't > understand which parameter I should modify to get the same results. > > S-PLUS CODE: > density(1:1000, width = 4) > > R-CODE: > density(1:1000, bw = 4, window = "g", ?n = 50, cut = 0.75) > > I obtain the same x values, but different y values. I try also different > examples, with different parameter. > > Can you help me? > > Thank you in advance. > > Nicola Sturaro > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Nicola > Sturaro Sommacal (Quantide srl) > Sent: Tuesday, November 02, 2010 3:05 AM > To: r-help at r-project.org > Subject: [R] density() function: differences with S-PLUS > > Hello! > > Someone know what are the difference between R and S-PLUS in > the density() > function? > > For example, I would like to reply this simple S-PLUS code in > R, but I don't > understand which parameter I should modify to get the same results. > > S-PLUS CODE: > density(1:1000, width = 4) > > R-CODE: > density(1:1000, bw = 4, window = "g", n = 50, cut = 0.75) > > I obtain the same x values, but different y values. I try > also different > examples, with different parameter.I needed to use the to= and from= arguments to get the same set of x values in R and S+. E.g., z <- density(x=0, width=3, window="gaussian", n=2001, from=-10, to=10, cut=0.75) gave identical x outputs in R and S+. By using x=0 you can see the difference in the gaussian-based kernel used by R and Splus: plot(z$x, z$y, pch=".", log="y") Splus, as its help("density") states", uses a truncated Gaussian kernel: "The "gaussian" window is truncated at 4 standard deviations (and then scaled appropriately to adjust for the truncated area)." R appears to not truncate the Gaussian kernel. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> Can you help me? > > Thank you in advance. > > Nicola Sturaro > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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. >
Nicola Sturaro Sommacal (Quantide srl)
2010-Nov-03 17:26 UTC
[R] density() function: differences with S-PLUS
Dear William, thank you very much for your reply. I see it only after my reply to Joshua. Unfortunately I cannot try until tomorrow, because I don't have S-PLUS on this machine. Thanks again. Nicola 2010/11/3 William Dunlap <wdunlap@tibco.com>> Did you get my reply (1:31pm PST Tuesday) > to your request? It showed how you needed > to use the from= and to= argument to density > to get identical x components to the output > and that the small differences in the y > component were due to S+ truncating the > gaussian kernel at +- 4 standard deviations > from the center while R does not truncate > the gaussian kernel (it output looks like it > uses a Fourier transform to do the convolution). > > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > -----Original Message----- > > From: r-help-bounces@r-project.org > > [mailto:r-help-bounces@r-project.org] On Behalf Of Nicola > > Sturaro Sommacal (Quantide srl) > > Sent: Wednesday, November 03, 2010 3:34 AM > > To: Joshua Wiley > > Cc: r-help@r-project.org > > Subject: Re: [R] density() function: differences with S-PLUS > > > > Dear Joshua, > > > > first of all, thank you very much for reply. I hoped that > > someone who's > > familiar with both S+ and R can reply to me, because I spent > > some hours to > > looking for a solution. > > > > If someone else would try, this is the SPLUS code and output, > > while below > > there is the R code. I obtain the same x values, while y values are > > differents for both examples. > > > > Thank you very much. > > > > Nicola > > > > > > ### S-PLUS CODE AND OUTPUT ### > > > > > density(1:1000, width = 4) > > $x: > > [1] -2.00000 18.51020 39.02041 59.53061 80.04082 > > 100.55102 121.06122 > > [8] 141.57143 162.08163 182.59184 203.10204 223.61224 > > 244.12245 264.63265 > > [15] 285.14286 305.65306 326.16327 346.67347 367.18367 > > 387.69388 408.20408 > > [22] 428.71429 449.22449 469.73469 490.24490 510.75510 > > 531.26531 551.77551 > > [29] 572.28571 592.79592 613.30612 633.81633 654.32653 > > 674.83673 695.34694 > > [36] 715.85714 736.36735 756.87755 777.38776 797.89796 > > 818.40816 838.91837 > > [43] 859.42857 879.93878 900.44898 920.95918 941.46939 > > 961.97959 982.48980 > > [50] 1003.00000 > > > > $y: > > [1] 4.565970e-006 1.000031e-003 9.999374e-004 1.000031e-003 > > 9.999471e-004 > > 1.000031e-003 > > [7] 9.999560e-004 1.000030e-003 9.999643e-004 1.000029e-003 > > 9.999718e-004 > > 1.000028e-003 > > [13] 9.999788e-004 1.000026e-003 9.999852e-004 1.000024e-003 > > 9.999910e-004 > > 1.000022e-003 > > [19] 9.999963e-004 1.000019e-003 1.000001e-003 1.000016e-003 > > 1.000006e-003 > > 1.000013e-003 > > [25] 1.000010e-003 1.000010e-003 1.000013e-003 1.000006e-003 > > 1.000016e-003 > > 1.000001e-003 > > [31] 1.000019e-003 9.999963e-004 1.000022e-003 9.999910e-004 > > 1.000024e-003 > > 9.999852e-004 > > [37] 1.000026e-003 9.999788e-004 1.000028e-003 9.999718e-004 > > 1.000029e-003 > > 9.999643e-004 > > [43] 1.000030e-003 9.999560e-004 1.000031e-003 9.999471e-004 > > 1.000031e-003 > > 9.999374e-004 > > [49] 1.000031e-003 4.432131e-006 > > > > > > > exdata = iris[, 1, 1] > > > density(exdata, width = 4) > > $x: > > [1] 1.300000 1.453061 1.606122 1.759184 1.912245 2.065306 > > 2.218367 2.371429 > > 2.524490 > > [10] 2.677551 2.830612 2.983673 3.136735 3.289796 3.442857 > > 3.595918 3.748980 > > 3.902041 > > [19] 4.055102 4.208163 4.361224 4.514286 4.667347 4.820408 > > 4.973469 5.126531 > > 5.279592 > > [28] 5.432653 5.585714 5.738776 5.891837 6.044898 6.197959 > > 6.351020 6.504082 > > 6.657143 > > [37] 6.810204 6.963265 7.116327 7.269388 7.422449 7.575510 > > 7.728571 7.881633 > > 8.034694 > > [46] 8.187755 8.340816 8.493878 8.646939 8.800000 > > > > $y: > > [1] 0.0007849649 0.0013097474 0.0021225491 0.0033616520 0.0052059615 > > 0.0078856717 > > [7] 0.0116917555 0.0169685132 0.0241073754 0.0335286785 0.0456521053 > > 0.0608554862 > > [13] 0.0794235072 0.1014901241 0.1269807991 0.1555625999 0.1866111931 > > 0.2192033788 > > [19] 0.2521417640 0.2840144993 0.3132881074 0.3384260582 0.3580208688 > > 0.3709241384 > > [25] 0.3763578665 0.3739920600 0.3639778683 0.3469316232 0.3238721233 > > 0.2961200278 > > [31] 0.2651731505 0.2325739601 0.1997853985 0.1680884651 0.1385105802 > > 0.1117884914 > > [37] 0.0883644110 0.0684099972 0.0518702141 0.0385181792 0.0280126487 > > 0.0199513951 > > [43] 0.0139159044 0.0095050745 0.0063575653 0.0041639082 0.0026680819 > > 0.0016700727 > > [49] 0.0010169912 0.0005962089 > > > > > > ### R CODE ### > > > > # S-PLUS CODE: density(1:1000, width = 4) SAME x BUT DIFFERENT y > > density(1:1000, bw = 4, window = "g", n = 50, cut = 0.75)$x > > density(1:1000, bw = 4, window = "g", n = 50, cut = 0.75)$y > > > > # S-PLUS CODE: exdata = iris[, 1, 1]; density(exdata, width > > 4) SAME x > > BUT DIFFERENT y > > exdata = iris$Sepal.Length[iris$Species == "setosa"] > > density(exdata, bw = 4, n = 50, cut = 0.75)$x > > density(exdata, bw = 4, n = 50, cut = 0.75)$y > > > > > > > > 2010/11/2 Joshua Wiley <jwiley.psych@gmail.com> > > > > > Dear Nicola, > > > > > > There are undoubtedly people here who are familiar with > > both S+ and R, > > > but they may not always be around or get to every question. In that > > > case there are (at least) two good options for you: > > > > > > 1) Say what you want mathematically (something of a universal > > > language) or statistically > > > > > > 2) Rather than just give us S+ code, show sample data > > (e.g., 1:1000), > > > and the values you would like obtained (in this case whatever the > > > output from S+ was). This would let us *try* to figure out what > > > happened and duplicate it in R. > > > > > > From the arcane step of reading R's documentation for > > density (?density): > > > > > > width: this exists for compatibility with S; if given, and 'bw' is > > > not, will set 'bw' to 'width' if this is a > > character string, > > > or to a kernel-dependent multiple of 'width' if this is > > > numeric. > > > > > > Which makes me wonder if this works for you (in R)? > > > > > > density(1:1000, width = 4) > > > > > > > > > Cheers, > > > > > > Josh > > > > > > > > > On Tue, Nov 2, 2010 at 3:04 AM, Nicola Sturaro Sommacal > > (Quantide srl) > > > <mailinglist@sturaro.net> wrote: > > > > Hello! > > > > > > > > Someone know what are the difference between R and S-PLUS in the > > > density() > > > > function? > > > > > > > > For example, I would like to reply this simple S-PLUS > > code in R, but I > > > don't > > > > understand which parameter I should modify to get the > > same results. > > > > > > > > S-PLUS CODE: > > > > density(1:1000, width = 4) > > > > > > > > R-CODE: > > > > density(1:1000, bw = 4, window = "g", n = 50, cut = 0.75) > > > > > > > > I obtain the same x values, but different y values. I try > > also different > > > > examples, with different parameter. > > > > > > > > Can you help me? > > > > > > > > Thank you in advance. > > > > > > > > Nicola Sturaro > > > > > > > > [[alternative HTML version deleted]] > > > > > > > > ______________________________________________ > > > > R-help@r-project.org 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. > > > > > > > > > > > > > > > > -- > > > Joshua Wiley > > > Ph.D. Student, Health Psychology > > > University of California, Los Angeles > > > http://www.joshuawiley.com/ > > > > > > > [[alternative HTML version deleted]] > > > > >[[alternative HTML version deleted]]
Nicola Sturaro Sommacal (Quantide srl)
2010-Nov-04 09:37 UTC
[R] density() function: differences with S-PLUS
Dear William, I obtained the same x values also without the from= and to= argument, using bw instead width in R. At this point I try to use a two step procedure for the y: - in the first step I obtained the x as below, - in the second step I used the minimum and the maximum values for the x as from= and to= arguments. In this way I obtain, in R, y values close to the S+ ones, but not the same. R code and S+ code and output are below. Thanks again. Nicola # R CODE exdata = iris$Sepal.Length[iris$Species == "setosa"] density(exdata, bw = 4, n = 50, cut = 0.75)$x # SAME AS S+ density(exdata, bw = 4, n = 50, cut = 0.75)$y # COMPLETELY DIFFERENT density(exdata, width = 4, n = 50, from = 1.3, to = 8.8, cut = 0.75)$y # CLOSE TO S+ # SPLUS CODE AND OUTPUT> exdata = iris[, 1, 1] > density(exdata, width = 4)$x: [1] 1.300000 1.453061 1.606122 1.759184 1.912245 2.065306 [7] 2.218367 2.371429 2.524490 2.677551 2.830612 2.983673 [13] 3.136735 3.289796 3.442857 3.595918 3.748980 3.902041 [19] 4.055102 4.208163 4.361224 4.514286 4.667347 4.820408 [25] 4.973469 5.126531 5.279592 5.432653 5.585714 5.738776 [31] 5.891837 6.044898 6.197959 6.351020 6.504082 6.657143 [37] 6.810204 6.963265 7.116327 7.269388 7.422449 7.575510 [43] 7.728571 7.881633 8.034694 8.187755 8.340816 8.493878 [49] 8.646939 8.800000 $y: [1] 0.0007849649 0.0013097474 0.0021225491 0.0033616520 [5] 0.0052059615 0.0078856717 0.0116917555 0.0169685132 [9] 0.0241073754 0.0335286785 0.0456521053 0.0608554862 [13] 0.0794235072 0.1014901241 0.1269807991 0.1555625999 [17] 0.1866111931 0.2192033788 0.2521417640 0.2840144993 [21] 0.3132881074 0.3384260582 0.3580208688 0.3709241384 [25] 0.3763578665 0.3739920600 0.3639778683 0.3469316232 [29] 0.3238721233 0.2961200278 0.2651731505 0.2325739601 [33] 0.1997853985 0.1680884651 0.1385105802 0.1117884914 [37] 0.0883644110 0.0684099972 0.0518702141 0.0385181792 [41] 0.0280126487 0.0199513951 0.0139159044 0.0095050745 [45] 0.0063575653 0.0041639082 0.0026680819 0.0016700727 [49] 0.0010169912 0.0005962089 2010/11/3 William Dunlap <wdunlap@tibco.com>> Did you get my reply (1:31pm PST Tuesday) > to your request? It showed how you needed > to use the from= and to= argument to density > to get identical x components to the output > and that the small differences in the y > component were due to S+ truncating the > gaussian kernel at +- 4 standard deviations > from the center while R does not truncate > the gaussian kernel (it output looks like it > uses a Fourier transform to do the convolution). > > > Bill Dunlap > Spotfire, TIBCO Software > wdunlap tibco.com > > > -----Original Message----- > > From: r-help-bounces@r-project.org > > [mailto:r-help-bounces@r-project.org] On Behalf Of Nicola > > Sturaro Sommacal (Quantide srl) > > Sent: Wednesday, November 03, 2010 3:34 AM > > To: Joshua Wiley > > Cc: r-help@r-project.org > > Subject: Re: [R] density() function: differences with S-PLUS > > > > Dear Joshua, > > > > first of all, thank you very much for reply. I hoped that > > someone who's > > familiar with both S+ and R can reply to me, because I spent > > some hours to > > looking for a solution. > > > > If someone else would try, this is the SPLUS code and output, > > while below > > there is the R code. I obtain the same x values, while y values are > > differents for both examples. > > > > Thank you very much. > > > > Nicola > > > > > > ### S-PLUS CODE AND OUTPUT ### > > > > > density(1:1000, width = 4) > > $x: > > [1] -2.00000 18.51020 39.02041 59.53061 80.04082 > > 100.55102 121.06122 > > [8] 141.57143 162.08163 182.59184 203.10204 223.61224 > > 244.12245 264.63265 > > [15] 285.14286 305.65306 326.16327 346.67347 367.18367 > > 387.69388 408.20408 > > [22] 428.71429 449.22449 469.73469 490.24490 510.75510 > > 531.26531 551.77551 > > [29] 572.28571 592.79592 613.30612 633.81633 654.32653 > > 674.83673 695.34694 > > [36] 715.85714 736.36735 756.87755 777.38776 797.89796 > > 818.40816 838.91837 > > [43] 859.42857 879.93878 900.44898 920.95918 941.46939 > > 961.97959 982.48980 > > [50] 1003.00000 > > > > $y: > > [1] 4.565970e-006 1.000031e-003 9.999374e-004 1.000031e-003 > > 9.999471e-004 > > 1.000031e-003 > > [7] 9.999560e-004 1.000030e-003 9.999643e-004 1.000029e-003 > > 9.999718e-004 > > 1.000028e-003 > > [13] 9.999788e-004 1.000026e-003 9.999852e-004 1.000024e-003 > > 9.999910e-004 > > 1.000022e-003 > > [19] 9.999963e-004 1.000019e-003 1.000001e-003 1.000016e-003 > > 1.000006e-003 > > 1.000013e-003 > > [25] 1.000010e-003 1.000010e-003 1.000013e-003 1.000006e-003 > > 1.000016e-003 > > 1.000001e-003 > > [31] 1.000019e-003 9.999963e-004 1.000022e-003 9.999910e-004 > > 1.000024e-003 > > 9.999852e-004 > > [37] 1.000026e-003 9.999788e-004 1.000028e-003 9.999718e-004 > > 1.000029e-003 > > 9.999643e-004 > > [43] 1.000030e-003 9.999560e-004 1.000031e-003 9.999471e-004 > > 1.000031e-003 > > 9.999374e-004 > > [49] 1.000031e-003 4.432131e-006 > > > > > > > exdata = iris[, 1, 1] > > > density(exdata, width = 4) > > $x: > > [1] 1.300000 1.453061 1.606122 1.759184 1.912245 2.065306 > > 2.218367 2.371429 > > 2.524490 > > [10] 2.677551 2.830612 2.983673 3.136735 3.289796 3.442857 > > 3.595918 3.748980 > > 3.902041 > > [19] 4.055102 4.208163 4.361224 4.514286 4.667347 4.820408 > > 4.973469 5.126531 > > 5.279592 > > [28] 5.432653 5.585714 5.738776 5.891837 6.044898 6.197959 > > 6.351020 6.504082 > > 6.657143 > > [37] 6.810204 6.963265 7.116327 7.269388 7.422449 7.575510 > > 7.728571 7.881633 > > 8.034694 > > [46] 8.187755 8.340816 8.493878 8.646939 8.800000 > > > > $y: > > [1] 0.0007849649 0.0013097474 0.0021225491 0.0033616520 0.0052059615 > > 0.0078856717 > > [7] 0.0116917555 0.0169685132 0.0241073754 0.0335286785 0.0456521053 > > 0.0608554862 > > [13] 0.0794235072 0.1014901241 0.1269807991 0.1555625999 0.1866111931 > > 0.2192033788 > > [19] 0.2521417640 0.2840144993 0.3132881074 0.3384260582 0.3580208688 > > 0.3709241384 > > [25] 0.3763578665 0.3739920600 0.3639778683 0.3469316232 0.3238721233 > > 0.2961200278 > > [31] 0.2651731505 0.2325739601 0.1997853985 0.1680884651 0.1385105802 > > 0.1117884914 > > [37] 0.0883644110 0.0684099972 0.0518702141 0.0385181792 0.0280126487 > > 0.0199513951 > > [43] 0.0139159044 0.0095050745 0.0063575653 0.0041639082 0.0026680819 > > 0.0016700727 > > [49] 0.0010169912 0.0005962089 > > > > > > ### R CODE ### > > > > # S-PLUS CODE: density(1:1000, width = 4) SAME x BUT DIFFERENT y > > density(1:1000, bw = 4, window = "g", n = 50, cut = 0.75)$x > > density(1:1000, bw = 4, window = "g", n = 50, cut = 0.75)$y > > > > # S-PLUS CODE: exdata = iris[, 1, 1]; density(exdata, width > > 4) SAME x > > BUT DIFFERENT y > > exdata = iris$Sepal.Length[iris$Species == "setosa"] > > density(exdata, bw = 4, n = 50, cut = 0.75)$x > > density(exdata, bw = 4, n = 50, cut = 0.75)$y > > > > > > > > 2010/11/2 Joshua Wiley <jwiley.psych@gmail.com> > > > > > Dear Nicola, > > > > > > There are undoubtedly people here who are familiar with > > both S+ and R, > > > but they may not always be around or get to every question. In that > > > case there are (at least) two good options for you: > > > > > > 1) Say what you want mathematically (something of a universal > > > language) or statistically > > > > > > 2) Rather than just give us S+ code, show sample data > > (e.g., 1:1000), > > > and the values you would like obtained (in this case whatever the > > > output from S+ was). This would let us *try* to figure out what > > > happened and duplicate it in R. > > > > > > From the arcane step of reading R's documentation for > > density (?density): > > > > > > width: this exists for compatibility with S; if given, and 'bw' is > > > not, will set 'bw' to 'width' if this is a > > character string, > > > or to a kernel-dependent multiple of 'width' if this is > > > numeric. > > > > > > Which makes me wonder if this works for you (in R)? > > > > > > density(1:1000, width = 4) > > > > > > > > > Cheers, > > > > > > Josh > > > > > > > > > On Tue, Nov 2, 2010 at 3:04 AM, Nicola Sturaro Sommacal > > (Quantide srl) > > > <mailinglist@sturaro.net> wrote: > > > > Hello! > > > > > > > > Someone know what are the difference between R and S-PLUS in the > > > density() > > > > function? > > > > > > > > For example, I would like to reply this simple S-PLUS > > code in R, but I > > > don't > > > > understand which parameter I should modify to get the > > same results. > > > > > > > > S-PLUS CODE: > > > > density(1:1000, width = 4) > > > > > > > > R-CODE: > > > > density(1:1000, bw = 4, window = "g", n = 50, cut = 0.75) > > > > > > > > I obtain the same x values, but different y values. I try > > also different > > > > examples, with different parameter. > > > > > > > > Can you help me? > > > > > > > > Thank you in advance. > > > > > > > > Nicola Sturaro > > > > > > > > [[alternative HTML version deleted]] > > > > > > > > ______________________________________________ > > > > R-help@r-project.org 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. > > > > > > > > > > > > > > > > -- > > > Joshua Wiley > > > Ph.D. Student, Health Psychology > > > University of California, Los Angeles > > > http://www.joshuawiley.com/ > > > > > > > [[alternative HTML version deleted]] > > > > >[[alternative HTML version deleted]]
I suspect that R's help("density") will tell about the difference between its bw and width arguments. In Splus help("density") says about "width" width width of the window. ... The standard error of a Gaussian window is width/4. For the other windows width is the width of the interval on which the window is non-zero. I believe R's bw argument is the standard deviation of the density used for the kernel. In R 'width' has the same meaning as in S+. The small difference between estimates when using the same bandwidth is mainly due to S+ using a truncated gaussian kernel (at 4 standard deviations out) and R not truncating the kernal. Part of the difference is due to R using the Fourier transform to do the convolution of the kernel and the data, while S+ uses a direct approach. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com ________________________________ From: mailinglist@sturaro.net [mailto:mailinglist@sturaro.net] On Behalf Of Nicola Sturaro Sommacal (Quantide srl) Sent: Thursday, November 04, 2010 2:36 AM To: William Dunlap Subject: Re: [R] density() function: differences with S-PLUS Dear William, I obtained the same x values also without the from= and toargument, using bw instead width in R. At this point I try to use a two step procedure for the y: - in the first step I obtained the x as below, - in the second step I used the minimum and the maximum values for the x as from= and to= arguments. In this way I obtain, in R, y values close to the S+ ones, but not the same. R code and S+ code and output are below. Thanks again. Nicola # R CODE exdata = iris$Sepal.Length[iris$Species == "setosa"] density(exdata, bw = 4, n = 50, cut = 0.75)$x # SAME AS S+ density(exdata, bw = 4, n = 50, cut = 0.75)$y # COMPLETELY DIFFERENT density(exdata, width = 4, n = 50, from = 1.3, to = 8.8, cut 0.75)$y # CLOSE TO S+ # SPLUS CODE AND OUTPUT > exdata = iris[, 1, 1] > density(exdata, width = 4) $x: [1] 1.300000 1.453061 1.606122 1.759184 1.912245 2.065306 [7] 2.218367 2.371429 2.524490 2.677551 2.830612 2.983673 [13] 3.136735 3.289796 3.442857 3.595918 3.748980 3.902041 [19] 4.055102 4.208163 4.361224 4.514286 4.667347 4.820408 [25] 4.973469 5.126531 5.279592 5.432653 5.585714 5.738776 [31] 5.891837 6.044898 6.197959 6.351020 6.504082 6.657143 [37] 6.810204 6.963265 7.116327 7.269388 7.422449 7.575510 [43] 7.728571 7.881633 8.034694 8.187755 8.340816 8.493878 [49] 8.646939 8.800000 $y: [1] 0.0007849649 0.0013097474 0.0021225491 0.0033616520 [5] 0.0052059615 0.0078856717 0.0116917555 0.0169685132 [9] 0.0241073754 0.0335286785 0.0456521053 0.0608554862 [13] 0.0794235072 0.1014901241 0.1269807991 0.1555625999 [17] 0.1866111931 0.2192033788 0.2521417640 0.2840144993 [21] 0.3132881074 0.3384260582 0.3580208688 0.3709241384 [25] 0.3763578665 0.3739920600 0.3639778683 0.3469316232 [29] 0.3238721233 0.2961200278 0.2651731505 0.2325739601 [33] 0.1997853985 0.1680884651 0.1385105802 0.1117884914 [37] 0.0883644110 0.0684099972 0.0518702141 0.0385181792 [41] 0.0280126487 0.0199513951 0.0139159044 0.0095050745 [45] 0.0063575653 0.0041639082 0.0026680819 0.0016700727 [49] 0.0010169912 0.0005962089 2010/11/3 William Dunlap <wdunlap@tibco.com> Did you get my reply (1:31pm PST Tuesday) to your request? It showed how you needed to use the from= and to= argument to density to get identical x components to the output and that the small differences in the y component were due to S+ truncating the gaussian kernel at +- 4 standard deviations from the center while R does not truncate the gaussian kernel (it output looks like it uses a Fourier transform to do the convolution). Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: r-help-bounces@r-project.org > [mailto:r-help-bounces@r-project.org] On Behalf Of Nicola > Sturaro Sommacal (Quantide srl) > Sent: Wednesday, November 03, 2010 3:34 AM > To: Joshua Wiley > Cc: r-help@r-project.org > Subject: Re: [R] density() function: differences with S-PLUS > > Dear Joshua, > > first of all, thank you very much for reply. I hoped that > someone who's > familiar with both S+ and R can reply to me, because I spent > some hours to > looking for a solution. > > If someone else would try, this is the SPLUS code and output, > while below > there is the R code. I obtain the same x values, while y values are > differents for both examples. > > Thank you very much. > > Nicola > > > ### S-PLUS CODE AND OUTPUT ### > > > density(1:1000, width = 4) > $x: > [1] -2.00000 18.51020 39.02041 59.53061 80.04082 > 100.55102 121.06122 > [8] 141.57143 162.08163 182.59184 203.10204 223.61224 > 244.12245 264.63265 > [15] 285.14286 305.65306 326.16327 346.67347 367.18367 > 387.69388 408.20408 > [22] 428.71429 449.22449 469.73469 490.24490 510.75510 > 531.26531 551.77551 > [29] 572.28571 592.79592 613.30612 633.81633 654.32653 > 674.83673 695.34694 > [36] 715.85714 736.36735 756.87755 777.38776 797.89796 > 818.40816 838.91837 > [43] 859.42857 879.93878 900.44898 920.95918 941.46939 > 961.97959 982.48980 > [50] 1003.00000 > > $y: > [1] 4.565970e-006 1.000031e-003 9.999374e-004 1.000031e-003 > 9.999471e-004 > 1.000031e-003 > [7] 9.999560e-004 1.000030e-003 9.999643e-004 1.000029e-003 > 9.999718e-004 > 1.000028e-003 > [13] 9.999788e-004 1.000026e-003 9.999852e-004 1.000024e-003 > 9.999910e-004 > 1.000022e-003 > [19] 9.999963e-004 1.000019e-003 1.000001e-003 1.000016e-003 > 1.000006e-003 > 1.000013e-003 > [25] 1.000010e-003 1.000010e-003 1.000013e-003 1.000006e-003 > 1.000016e-003 > 1.000001e-003 > [31] 1.000019e-003 9.999963e-004 1.000022e-003 9.999910e-004 > 1.000024e-003 > 9.999852e-004 > [37] 1.000026e-003 9.999788e-004 1.000028e-003 9.999718e-004 > 1.000029e-003 > 9.999643e-004 > [43] 1.000030e-003 9.999560e-004 1.000031e-003 9.999471e-004 > 1.000031e-003 > 9.999374e-004 > [49] 1.000031e-003 4.432131e-006 > > > > exdata = iris[, 1, 1] > > density(exdata, width = 4) > $x: > [1] 1.300000 1.453061 1.606122 1.759184 1.912245 2.065306 > 2.218367 2.371429 > 2.524490 > [10] 2.677551 2.830612 2.983673 3.136735 3.289796 3.442857 > 3.595918 3.748980 > 3.902041 > [19] 4.055102 4.208163 4.361224 4.514286 4.667347 4.820408 > 4.973469 5.126531 > 5.279592 > [28] 5.432653 5.585714 5.738776 5.891837 6.044898 6.197959 > 6.351020 6.504082 > 6.657143 > [37] 6.810204 6.963265 7.116327 7.269388 7.422449 7.575510 > 7.728571 7.881633 > 8.034694 > [46] 8.187755 8.340816 8.493878 8.646939 8.800000 > > $y: > [1] 0.0007849649 0.0013097474 0.0021225491 0.0033616520 0.0052059615 > 0.0078856717 > [7] 0.0116917555 0.0169685132 0.0241073754 0.0335286785 0.0456521053 > 0.0608554862 > [13] 0.0794235072 0.1014901241 0.1269807991 0.1555625999 0.1866111931 > 0.2192033788 > [19] 0.2521417640 0.2840144993 0.3132881074 0.3384260582 0.3580208688 > 0.3709241384 > [25] 0.3763578665 0.3739920600 0.3639778683 0.3469316232 0.3238721233 > 0.2961200278 > [31] 0.2651731505 0.2325739601 0.1997853985 0.1680884651 0.1385105802 > 0.1117884914 > [37] 0.0883644110 0.0684099972 0.0518702141 0.0385181792 0.0280126487 > 0.0199513951 > [43] 0.0139159044 0.0095050745 0.0063575653 0.0041639082 0.0026680819 > 0.0016700727 > [49] 0.0010169912 0.0005962089 > > > ### R CODE ### > > # S-PLUS CODE: density(1:1000, width = 4) SAME x BUT DIFFERENT y > density(1:1000, bw = 4, window = "g", n = 50, cut 0.75)$x > density(1:1000, bw = 4, window = "g", n = 50, cut 0.75)$y > > # S-PLUS CODE: exdata = iris[, 1, 1]; density(exdata, width > 4) SAME x > BUT DIFFERENT y > exdata = iris$Sepal.Length[iris$Species == "setosa"] > density(exdata, bw = 4, n = 50, cut = 0.75)$x > density(exdata, bw = 4, n = 50, cut = 0.75)$y > > > > 2010/11/2 Joshua Wiley <jwiley.psych@gmail.com> > > > Dear Nicola, > > > > There are undoubtedly people here who are familiar with > both S+ and R, > > but they may not always be around or get to every question. In that > > case there are (at least) two good options for you: > > > > 1) Say what you want mathematically (something of a universal > > language) or statistically > > > > 2) Rather than just give us S+ code, show sample data > (e.g., 1:1000), > > and the values you would like obtained (in this case whatever the > > output from S+ was). This would let us *try* to figure out what > > happened and duplicate it in R. > > > > From the arcane step of reading R's documentation for > density (?density): > > > > width: this exists for compatibility with S; if given, and 'bw' is > > not, will set 'bw' to 'width' if this is a > character string, > > or to a kernel-dependent multiple of 'width' if this is > > numeric. > > > > Which makes me wonder if this works for you (in R)? > > > > density(1:1000, width = 4) > > > > > > Cheers, > > > > Josh > > > > > > On Tue, Nov 2, 2010 at 3:04 AM, Nicola Sturaro Sommacal > (Quantide srl) > > <mailinglist@sturaro.net> wrote: > > > Hello! > > > > > > Someone know what are the difference between R and S-PLUS in the > > density() > > > function? > > > > > > For example, I would like to reply this simple S-PLUS > code in R, but I > > don't > > > understand which parameter I should modify to get the > same results. > > > > > > S-PLUS CODE: > > > density(1:1000, width = 4) > > > > > > R-CODE: > > > density(1:1000, bw = 4, window = "g", n = 50, cut = 0.75) > > > > > > I obtain the same x values, but different y values. I try > also different > > > examples, with different parameter. > > > > > > Can you help me? > > > > > > Thank you in advance. > > > > > > Nicola Sturaro > > > > > > [[alternative HTML version deleted]] > > > > > > ______________________________________________ > > > R-help@r-project.org 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. > > > > > > > > > > > -- > > Joshua Wiley > > Ph.D. Student, Health Psychology > > University of California, Los Angeles > > http://www.joshuawiley.com/ > > > > [[alternative HTML version deleted]] > > [[alternative HTML version deleted]]