Hi all, How do I perform log(x+1) in R? log1p_trans() from the package ?scales" doesn?t seem to work for me. Best, Faradj
Log plus one transformation in R
17 messages · Faradj Koliev, Spencer Graves, William Dunlap +7 more
"?log" includes documentation for "log1p" in the base package
Will that work?
Spencer Graves
On 12/12/2016 10:23 AM, Faradj Koliev wrote:
Hi all, How do I perform log(x+1) in R? log1p_trans() from the package ?scales" doesn?t seem to work for me. Best, Faradj
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Faradj, I all you need to do is newvalue <- log(x+1) John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)
Faradj Koliev <faradj.g at gmail.com> 12/12/16 11:24 AM >>>
Hi all, How do I perform log(x+1) in R? log1p_trans() from the package ?scales" doesn?t seem to work for me. Best, Faradj ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. Confidentiality Statement: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
log1p(x), in the base package computes log(1+x) accurately for small x (and large). E.g.,
options(digits=16) base::log1p(1e-14)
[1] 9.99999999999995e-15
base::log1p(1e-14) - base::log(1+1e-14)
[1] 7.992778373591124e-18
as.numeric(log(Rmpfr::mpfr(1,precBits=1000) + Rmpfr::mpfr(1e-14,
precBits=1000))) - log1p(1e-14) [1] 0 Bill Dunlap TIBCO Software wdunlap tibco.com
On Mon, Dec 12, 2016 at 8:23 AM, Faradj Koliev <faradj.g at gmail.com> wrote:
Hi all, How do I perform log(x+1) in R? log1p_trans() from the package ?scales" doesn?t seem to work for me. Best, Faradj
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Faradj Koliev <faradj.g at gmail.com>
on Mon, 12 Dec 2016 17:23:20 +0100 writes:
> Hi all, How do I perform log(x+1) in R?
> log1p_trans() from the package ?scales" doesn?t seem to
> work for me.
amazing that you did not find log1p() in base R.
At the risk of being flamed . . . What is the difference between log1p(x) and log(x+1)? The two methods appear to give the same results:
log1p(0.000001)/log(0.000001+1)
[1] 1 John John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)
William Dunlap via R-help <r-help at r-project.org> 12/12/16 11:38 AM
log1p(x), in the base package computes log(1+x) accurately for small x (and large). E.g.,
options(digits=16) base::log1p(1e-14)
[1] 9.99999999999995e-15
base::log1p(1e-14) - base::log(1+1e-14)
[1] 7.992778373591124e-18
as.numeric(log(Rmpfr::mpfr(1,precBits=1000) + Rmpfr::mpfr(1e-14,
precBits=1000))) - log1p(1e-14) [1] 0 Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Dec 12, 2016 at 8:23 AM, Faradj Koliev <faradj.g at gmail.com> wrote:
Hi all, How do I perform log(x+1) in R? log1p_trans() from the package ?scales" doesn?t seem to work for me. Best, Faradj
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. Confidentiality Statement: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
On Dec 12, 2016, at 8:53 AM, John Sorkin <jsorkin at grecc.umaryland.edu> wrote: At the risk of being flamed . . . What is the difference between log1p(x) and log(x+1)? The two methods appear to give the same results:
log1p(0.000001)/log(0.000001+1)
[1] 1 John
Read the help page more carefully.
David. > > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and > Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) >>>> William Dunlap via R-help <r-help at r-project.org> 12/12/16 11:38 AM >>>> > log1p(x), in the base package computes log(1+x) accurately for small x > (and > large). > > E.g., >> options(digits=16) >> base::log1p(1e-14) > [1] 9.99999999999995e-15 >> base::log1p(1e-14) - base::log(1+1e-14) > [1] 7.992778373591124e-18 >> as.numeric(log(Rmpfr::mpfr(1,precBits=1000) + Rmpfr::mpfr(1e-14, > precBits=1000))) - log1p(1e-14) > [1] 0 > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > On Mon, Dec 12, 2016 at 8:23 AM, Faradj Koliev <faradj.g at gmail.com> > wrote: > >> Hi all, >> >> How do I perform log(x+1) in R? >> >> log1p_trans() from the package ?scales" doesn?t seem to work for me. >> >> Best, >> Faradj >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > Confidentiality Statement: > This email message, including any attachments, is for ...{{dropped:16}}
The difference increases as the value gets closer to 0:
dput(log1p(0.000001))
9.99999500000333e-07
dput(log(1+0.000001))
9.99999499918067e-07
dput(log1p(0.000001)/log(0.000001+1))
1.00000000008227
dput(log1p(0.00000001)/log(0.00000001+1))
1.00000000607747
dput(log1p(0.0000000001)/log(0.0000000001+1))
0.999999917259636
dput(log1p(0.000000000001)/log(0.000000000001+1))
0.99991110732027
dput(log1p(0.00000000000001)/log(0.00000000000001+1))
1.00079991719344 ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of John Sorkin Sent: Monday, December 12, 2016 10:54 AM To: faradj.g at gmail.com; r-help at r-project.org; wdunlap at tibco.com Subject: Re: [R] Log plus one transformation in R At the risk of being flamed . . . What is the difference between log1p(x) and log(x+1)? The two methods appear to give the same results:
log1p(0.000001)/log(0.000001+1)
[1] 1 John John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)
William Dunlap via R-help <r-help at r-project.org> 12/12/16 11:38 AM
log1p(x), in the base package computes log(1+x) accurately for small x (and large). E.g.,
options(digits=16) base::log1p(1e-14)
[1] 9.99999999999995e-15
base::log1p(1e-14) - base::log(1+1e-14)
[1] 7.992778373591124e-18
as.numeric(log(Rmpfr::mpfr(1,precBits=1000) + Rmpfr::mpfr(1e-14,
precBits=1000))) - log1p(1e-14) [1] 0 Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Dec 12, 2016 at 8:23 AM, Faradj Koliev <faradj.g at gmail.com> wrote:
Hi all, How do I perform log(x+1) in R? log1p_trans() from the package ?scales" doesn?t seem to work for me. Best, Faradj
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. Confidentiality Statement: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
David, I did read the help page. All it says is log1p(x) computes log(1+x) accurately also for |x| << 1 (and less accurately when x is approximately -1). This gives me pause. Does it mean that log(x) does not give accurate results? If log1p gives more accurate values than log, why is the log function not written to use the more accurate computation performed by log1p. I don't believe I can look directly at the code for log and log1p, so I need to rely on the kindness of others to explain the differences between the computations performed by the functions. I guess the test I ran, log1p(0.000001)/log(0.000001+1), did not have enough precision to demonstrate a difference between the two functions. John John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)
David Winsemius <dwinsemius at comcast.net> 12/12/16 12:05 PM >>>
On Dec 12, 2016, at 8:53 AM, John Sorkin <jsorkin at grecc.umaryland.edu>
wrote:
At the risk of being flamed . . . What is the difference between log1p(x) and log(x+1)? The two methods appear to give the same results:
log1p(0.000001)/log(0.000001+1)
[1] 1 John
Read the help page more carefully.
David. > > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and > Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) >>>> William Dunlap via R-help <r-help at r-project.org> 12/12/16 11:38 AM >>>> > log1p(x), in the base package computes log(1+x) accurately for small x > (and > large). > > E.g., >> options(digits=16) >> base::log1p(1e-14) > [1] 9.99999999999995e-15 >> base::log1p(1e-14) - base::log(1+1e-14) > [1] 7.992778373591124e-18 >> as.numeric(log(Rmpfr::mpfr(1,precBits=1000) + Rmpfr::mpfr(1e-14, > precBits=1000))) - log1p(1e-14) > [1] 0 > > > Bill Dunlap > TIBCO Software > wdunlap tibco.com > > On Mon, Dec 12, 2016 at 8:23 AM, Faradj Koliev <faradj.g at gmail.com> > wrote: > >> Hi all, >> >> How do I perform log(x+1) in R? >> >> log1p_trans() from the package ?scales" doesn?t seem to work for me. >> >> Best, >> Faradj >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. > Confidentiality Statement: > This email message, including any attachments, is for the sole use of > the intended recipient(s) and may contain confidential and privileged > information. Any unauthorized use, disclosure or distribution is > prohibited. If you are not the intended recipient, please contact the > sender by reply email and destroy all copies of the original message. > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. David Winsemius the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
Print more digits of the quotient or subtract one from it and you will see the difference:
log1p(0.000001)/log(0.000001+1) - 1
[1] 8.22666379463044e-11 Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Dec 12, 2016 at 8:53 AM, John Sorkin <jsorkin at grecc.umaryland.edu> wrote:
At the risk of being flamed . . . What is the difference between log1p(x) and log(x+1)? The two methods appear to give the same results:
log1p(0.000001)/log(0.000001+1)
[1] 1 John John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)
William Dunlap via R-help <r-help at r-project.org> 12/12/16 11:38 AM >>>
log1p(x), in the base package computes log(1+x) accurately for small x (and large). E.g.,
options(digits=16) base::log1p(1e-14)
[1] 9.99999999999995e-15
base::log1p(1e-14) - base::log(1+1e-14)
[1] 7.992778373591124e-18
as.numeric(log(Rmpfr::mpfr(1,precBits=1000) + Rmpfr::mpfr(1e-14,
precBits=1000))) - log1p(1e-14) [1] 0 Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Dec 12, 2016 at 8:23 AM, Faradj Koliev <faradj.g at gmail.com> wrote:
Hi all, How do I perform log(x+1) in R? log1p_trans() from the package ?scales" doesn?t seem to work for me. Best, Faradj
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. *Confidentiality Statement:* This email message, including any attachments, is for ...{{dropped:10}}
The problem is that 1+x does not give accurate results for small x:
> (1+1e-17) == 1
[1] TRUE
Bill Dunlap
TIBCO Software
wdunlap tibco.com
On Mon, Dec 12, 2016 at 9:26 AM, John Sorkin <jsorkin at grecc.umaryland.edu>
wrote:
David, I did read the help page. All it says is log1p(x) computes *log(1+x)* accurately also for *|x| << 1* (and less accurately when *x is approximately -1*). This gives me pause. Does it mean that log(x) does not give accurate results? If log1p gives more accurate values than log, why is the log function not written to use the more accurate computation performed by log1p. I don't believe I can look directly at the code for log and log1p, so I need to rely on the kindness of others to explain the differences between the computations performed by the functions. I guess the test I ran, log1p(0.000001)/log(0.000001+1), did not have enough precision to demonstrate a difference between the two functions. John John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)
David Winsemius <dwinsemius at comcast.net> 12/12/16 12:05 PM >>>
On Dec 12, 2016, at 8:53 AM, John Sorkin <jsorkin at grecc.umaryland.edu>
wrote:
At the risk of being flamed . . . What is the difference between log1p(x) and log(x+1)? The two methods appear to give the same results:
log1p(0.000001)/log(0.000001+1)
[1] 1 John
Read the help page more carefully. -- David.
John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)
William Dunlap via R-help <r-help at r-project.org> 12/12/16 11:38 AM
log1p(x), in the base package computes log(1+x) accurately for small x (and large). E.g.,
options(digits=16) base::log1p(1e-14)
[1] 9.99999999999995e-15
base::log1p(1e-14) - base::log(1+1e-14)
[1] 7.992778373591124e-18
as.numeric(log(Rmpfr::mpfr(1,precBits=1000) + Rmpfr::mpfr(1e-14,
precBits=1000))) - log1p(1e-14) [1] 0 Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Dec 12, 2016 at 8:23 AM, Faradj Koliev <faradj.g at gmail.com> wrote:
Hi all, How do I perform log(x+1) in R? log1p_trans() from the package ?scales" doesn?t seem to work for me. Best, Faradj
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. Confidentiality Statement: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
David Winsemius
Alameda, CA, USA
*Confidentiality Statement:*
This email message, including any attachments, is for ...{{dropped:10}}
On Dec 12, 2016, at 9:26 AM, John Sorkin <jsorkin at grecc.umaryland.edu> wrote: David, I did read the help page. All it says is log1p(x) computes log(1+x) accurately also for |x| << 1 (and less accurately when x is approximately -1).
Not true. You evidently did not run the examples.
David. > > This gives me pause. Does it mean that log(x) does not give accurate results? If log1p gives more accurate values than log, why is the log function not written to use the more accurate computation performed by log1p. I don't believe I can look directly at the code for log and log1p, so I need to rely on the kindness of others to explain the differences between the computations performed by the functions. I guess the test I ran, log1p(0.000001)/log(0.000001+1), did not have enough precision to demonstrate a difference between the two functions. > > John > > > > > > John David Sorkin M.D., Ph.D. > Professor of Medicine > Chief, Biostatistics and Informatics > University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine > Baltimore VA Medical Center > 10 North Greene Street > GRECC (BT/18/GR) > Baltimore, MD 21201-1524 > (Phone) 410-605-7119 > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > > >>> David Winsemius <dwinsemius at comcast.net> 12/12/16 12:05 PM >>> > > > On Dec 12, 2016, at 8:53 AM, John Sorkin <jsorkin at grecc.umaryland.edu> wrote: > > > > At the risk of being flamed . . . > > What is the difference between log1p(x) and log(x+1)? > > The two methods appear to give the same results: > >> log1p(0.000001)/log(0.000001+1) > > [1] 1 > > John > > Read the help page more carefully. > > -- > David. > > > > > > John David Sorkin M.D., Ph.D. > > Professor of Medicine > > Chief, Biostatistics and Informatics > > University of Maryland School of Medicine Division of Gerontology and > > Geriatric Medicine > > Baltimore VA Medical Center > > 10 North Greene Street > > GRECC (BT/18/GR) > > Baltimore, MD 21201-1524 > > (Phone) 410-605-7119 > > (Fax) 410-605-7913 (Please call phone number above prior to faxing) > >>>> William Dunlap via R-help <r-help at r-project.org> 12/12/16 11:38 AM > >>>> > > log1p(x), in the base package computes log(1+x) accurately for small x > > (and > > large). > > > > E.g., > >> options(digits=16) > >> base::log1p(1e-14) > > [1] 9.99999999999995e-15 > >> base::log1p(1e-14) - base::log(1+1e-14) > > [1] 7.992778373591124e-18 > >> as.numeric(log(Rmpfr::mpfr(1,precBits=1000) + Rmpfr::mpfr(1e-14, > > precBits=1000))) - log1p(1e-14) > > [1] 0 > > > > > > Bill Dunlap > > TIBCO Software > > wdunlap tibco.com > > > > On Mon, Dec 12, 2016 at 8:23 AM, Faradj Koliev <faradj.g at gmail.com> > > wrote: > > > >> Hi all, > >> > >> How do I perform log(x+1) in R? > >> > >> log1p_trans() from the package ?scales" doesn?t seem to work for me. > >> > >> Best, > >> Faradj > >> ______________________________________________ > >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > >> 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. > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. > > Confidentiality Statement: > > This email message, including any attachments, is for the sole use of > > the intended recipient(s) and may contain confidential and privileged > > information. Any unauthorized use, disclosure or distribution is > > prohibited. If you are not the intended recipient, please contact the > > sender by reply email and destroy all copies of the original message. > > ______________________________________________ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > 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. > > David Winsemius > Alameda, CA, USA > > > > > Confidentiality Statement: > > This email message, including any attachments, is for ...{{dropped:10}}
Numerical accuracy in floating point math is a much broader discussion than R, but [1] seems to summarize it reasonably well. There are whole courses on this topic at university. [1] http://www.johndcook.com/blog/2010/06/07/math-library-functions-that-seem-unnecessary/
Sent from my phone. Please excuse my brevity. On December 12, 2016 9:26:17 AM PST, John Sorkin <jsorkin at grecc.umaryland.edu> wrote: >David, > >I did read the help page. All it says is >log1p(x) computes log(1+x) accurately also for |x| << 1 (and less >accurately when x is approximately -1). >This gives me pause. Does it mean that log(x) does not give accurate >results? If log1p gives more accurate values than log, why is the log >function not written to use the more accurate computation performed by >log1p. I don't believe I can look directly at the code for log and >log1p, so I need to rely on the kindness of others to explain the >differences between the computations performed by the functions. I >guess >the test I ran, log1p(0.000001)/log(0.000001+1), did not have enough >precision to demonstrate a difference between the two functions. >John > > > >John David Sorkin M.D., Ph.D. >Professor of Medicine >Chief, Biostatistics and Informatics >University of Maryland School of Medicine Division of Gerontology and >Geriatric Medicine >Baltimore VA Medical Center >10 North Greene Street >GRECC (BT/18/GR) >Baltimore, MD 21201-1524 >(Phone) 410-605-7119 >(Fax) 410-605-7913 (Please call phone number above prior to faxing) >>>> David Winsemius <dwinsemius at comcast.net> 12/12/16 12:05 PM >>> > >> On Dec 12, 2016, at 8:53 AM, John Sorkin ><jsorkin at grecc.umaryland.edu> >wrote: >> >> At the risk of being flamed . . . >> What is the difference between log1p(x) and log(x+1)? >> The two methods appear to give the same results: >>> log1p(0.000001)/log(0.000001+1) >> [1] 1 >> John > >Read the help page more carefully. > >-- >David. >> >> >> John David Sorkin M.D., Ph.D. >> Professor of Medicine >> Chief, Biostatistics and Informatics >> University of Maryland School of Medicine Division of Gerontology and >> Geriatric Medicine >> Baltimore VA Medical Center >> 10 North Greene Street >> GRECC (BT/18/GR) >> Baltimore, MD 21201-1524 >> (Phone) 410-605-7119 >> (Fax) 410-605-7913 (Please call phone number above prior to faxing) >>>>> William Dunlap via R-help <r-help at r-project.org> 12/12/16 11:38 AM >>>>> >> log1p(x), in the base package computes log(1+x) accurately for small >x >> (and >> large). >> >> E.g., >>> options(digits=16) >>> base::log1p(1e-14) >> [1] 9.99999999999995e-15 >>> base::log1p(1e-14) - base::log(1+1e-14) >> [1] 7.992778373591124e-18 >>> as.numeric(log(Rmpfr::mpfr(1,precBits=1000) + Rmpfr::mpfr(1e-14, >> precBits=1000))) - log1p(1e-14) >> [1] 0 >> >> >> Bill Dunlap >> TIBCO Software >> wdunlap tibco.com >> >> On Mon, Dec 12, 2016 at 8:23 AM, Faradj Koliev <faradj.g at gmail.com> >> wrote: >> >>> Hi all, >>> >>> How do I perform log(x+1) in R? >>> >>> log1p_trans() from the package ?scales" doesn?t seem to work for me. >>> >>> Best, >>> Faradj >>> ______________________________________________ >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> 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. >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. >> Confidentiality Statement: >> This email message, including any attachments, is for the sole use of >> the intended recipient(s) and may contain confidential and privileged >> information. Any unauthorized use, disclosure or distribution is >> prohibited. If you are not the intended recipient, please contact the >> sender by reply email and destroy all copies of the original message. > >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. > >David Winsemius >the intended recipient(s) and may contain confidential and privileged >information. Any unauthorized use, disclosure or distribution is >prohibited. If you are not the intended recipient, please contact the >sender by reply email and destroy all copies of the original message. >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.
And, for crying out loud... just try it with x = 1.234e-16 or so. One would think that the hint |x| << 1 was obvious enough. -pd
On 12 Dec 2016, at 18:26 , William Dunlap via R-help <r-help at r-project.org> wrote: Print more digits of the quotient or subtract one from it and you will see the difference:
log1p(0.000001)/log(0.000001+1) - 1
[1] 8.22666379463044e-11 Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Dec 12, 2016 at 8:53 AM, John Sorkin <jsorkin at grecc.umaryland.edu> wrote:
At the risk of being flamed . . . What is the difference between log1p(x) and log(x+1)? The two methods appear to give the same results:
log1p(0.000001)/log(0.000001+1)
[1] 1 John John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)
William Dunlap via R-help <r-help at r-project.org> 12/12/16 11:38 AM >>>
log1p(x), in the base package computes log(1+x) accurately for small x (and large). E.g.,
options(digits=16) base::log1p(1e-14)
[1] 9.99999999999995e-15
base::log1p(1e-14) - base::log(1+1e-14)
[1] 7.992778373591124e-18
as.numeric(log(Rmpfr::mpfr(1,precBits=1000) + Rmpfr::mpfr(1e-14,
precBits=1000))) - log1p(1e-14) [1] 0 Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Dec 12, 2016 at 8:23 AM, Faradj Koliev <faradj.g at gmail.com> wrote:
Hi all, How do I perform log(x+1) in R? log1p_trans() from the package ?scales" doesn?t seem to work for me. Best, Faradj
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. *Confidentiality Statement:* This email message, including any attachments, is for ...{{dropped:10}}
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Many thanks! logp1(x) worked just fine. Best, Faradj Skickat fr?n min iPhone
12 dec. 2016 kl. 22:54 skrev peter dalgaard <pdalgd at gmail.com>: And, for crying out loud... just try it with x = 1.234e-16 or so. One would think that the hint |x| << 1 was obvious enough. -pd
On 12 Dec 2016, at 18:26 , William Dunlap via R-help <r-help at r-project.org> wrote: Print more digits of the quotient or subtract one from it and you will see the difference:
log1p(0.000001)/log(0.000001+1) - 1
[1] 8.22666379463044e-11 Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Dec 12, 2016 at 8:53 AM, John Sorkin <jsorkin at grecc.umaryland.edu> wrote:
At the risk of being flamed . . . What is the difference between log1p(x) and log(x+1)? The two methods appear to give the same results:
log1p(0.000001)/log(0.000001+1)
[1] 1 John John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)
William Dunlap via R-help <r-help at r-project.org> 12/12/16 11:38 AM >>>
log1p(x), in the base package computes log(1+x) accurately for small x (and large). E.g.,
options(digits=16) base::log1p(1e-14)
[1] 9.99999999999995e-15
base::log1p(1e-14) - base::log(1+1e-14)
[1] 7.992778373591124e-18
as.numeric(log(Rmpfr::mpfr(1,precBits=1000) + Rmpfr::mpfr(1e-14,
precBits=1000))) - log1p(1e-14) [1] 0 Bill Dunlap TIBCO Software wdunlap tibco.com
On Mon, Dec 12, 2016 at 8:23 AM, Faradj Koliev <faradj.g at gmail.com> wrote: Hi all, How do I perform log(x+1) in R? log1p_trans() from the package ?scales" doesn?t seem to work for me. Best, Faradj
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. *Confidentiality Statement:* This email message, including any attachments, is for ...{{dropped:10}}
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
-- Peter Dalgaard, Professor, Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Office: A 4.23 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
On 12/12/2016 12:26 PM, John Sorkin wrote:
David, I did read the help page. All it says is log1p(x) computes log(1+x) accurately also for |x| << 1 (and less accurately when x is approximately -1). This gives me pause. Does it mean that log(x) does not give accurate results? If log1p gives more accurate values than log, why is the log function not written to use the more accurate computation performed by log1p.
I don't think anyone has directly answered these questions. The problem isn't with log(), it's with the representation of floating point numbers in R. The log of 1 + 10^(-100) is very close to 10^(-100), but since 1 + 1e-100 evaluates to 1 (we only keep 15 or 16 digits of precision), log(1 + 1e-100) will come out as zero. On the other hand, log1p(1e-100) evaluates correctly to 1e-100.
I don't believe I can look directly at the code for log and log1p,
R is open source, so you could, but those are likely coming from system libraries, so it isn't easy to see how the approximations are being done. Duncan Murdoch
so I need to rely on the kindness of others to explain the differences between the computations performed by the functions. I guess the test I ran, log1p(0.000001)/log(0.000001+1), did not have enough precision to demonstrate a difference between the two functions. John John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)
David Winsemius <dwinsemius at comcast.net> 12/12/16 12:05 PM >>>
On Dec 12, 2016, at 8:53 AM, John Sorkin <jsorkin at grecc.umaryland.edu>
wrote:
At the risk of being flamed . . . What is the difference between log1p(x) and log(x+1)? The two methods appear to give the same results:
log1p(0.000001)/log(0.000001+1)
[1] 1 John
Read the help page more carefully.
Duncan, Thank you. You are correct no one answered my question , despite the fact that several people reply to my email, until you replied. Your exclamation is quite clear and I thank you for your kindness. I did not pursue my question any further as I was concerned that I would be flamed. John
John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)
On Dec 13, 2016, at 12:12 PM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
On 12/12/2016 12:26 PM, John Sorkin wrote: David, I did read the help page. All it says is log1p(x) computes log(1+x) accurately also for |x| << 1 (and less accurately when x is approximately -1). This gives me pause. Does it mean that log(x) does not give accurate results? If log1p gives more accurate values than log, why is the log function not written to use the more accurate computation performed by log1p.
I don't think anyone has directly answered these questions. The problem isn't with log(), it's with the representation of floating point numbers in R. The log of 1 + 10^(-100) is very close to 10^(-100), but since 1 + 1e-100 evaluates to 1 (we only keep 15 or 16 digits of precision), log(1 + 1e-100) will come out as zero. On the other hand, log1p(1e-100) evaluates correctly to 1e-100.
I don't believe I can look directly at the code for log and log1p,
R is open source, so you could, but those are likely coming from system libraries, so it isn't easy to see how the approximations are being done. Duncan Murdoch
so I need to rely on the kindness of others to explain the differences between the computations performed by the functions. I guess the test I ran, log1p(0.000001)/log(0.000001+1), did not have enough precision to demonstrate a difference between the two functions. John John David Sorkin M.D., Ph.D. Professor of Medicine Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology and Geriatric Medicine Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing)
David Winsemius <dwinsemius at comcast.net> 12/12/16 12:05 PM >>>
On Dec 12, 2016, at 8:53 AM, John Sorkin <jsorkin at grecc.umaryland.edu>
wrote:
At the risk of being flamed . . . What is the difference between log1p(x) and log(x+1)? The two methods appear to give the same results:
log1p(0.000001)/log(0.000001+1)
[1] 1 John
Read the help page more carefully.
Confidentiality Statement: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.