Re: [R] Dividing by 0
I suspect you should be smoothing the series in a manner that replaces zeros by some usually small larger number before you start. Without more details on what you are trying to do, it is impossible to know what is sensible. You are proposing to leave all smoothing ("rolling"?) till later; why not do some smoothing at the start? John Maindonald email: [EMAIL PROTECTED] phone : +61 2 (6125)3473fax : +61 2(6125)5549 Centre for Mathematics & Its Applications, Room 1194, John Dedman Mathematical Sciences Building (Building 27) Australian National University, Canberra ACT 0200. On 26 Jul 2008, at 8:00 PM, [EMAIL PROTECTED] wrote: From: nmarti <[EMAIL PROTECTED]> Date: 26 July 2008 1:42:09 AM To: r-help@r-project.org Subject: Re: [R] Dividing by 0 I'm well aware these are not errors, I guess I miss-wrote. I understand your concern. Thanks for passionately looking out for my well being, you saved my life. My variable has about 10,000 elements and sometime for the first 100 to 500 elements there is lots of 0's, so I end up with lots of NA/NaN/Inf's. However, when I try to use "Rolling" calculations I recieve error messages because the "Rolling" functions reject the NA/NaN/Inf's. So, I need 0's in place of the NA/NaN/Inf's so I can run the "Rolling" calculations. I can't just delete these observations, because it messes up lots of other other things within these dataframes. I'm well aware these "Rolling" calculations will be wrong in the beginning of the dataframe, so I just throw these out. The rolling window is only about 50 odservations, so out of 10,000, I still end up with ample correct data and calculations. So is this still idiotic? Thanks again for your concern. Now that you understand my situation a little better, you might be less distracted today and be able to sleep better tonight. __ 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.
Re: [R] Dividing by 0
I would think that the result of your rolling calculation should be NA if there are NAs or NaNs in the window. Producing an error given NAs seems like a broken function to me. One of the main purposes of NA is so that you can do operations like what you want to do and get reasonable answers. Patrick Burns [EMAIL PROTECTED] +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") nmarti wrote: I'm well aware these are not errors, I guess I miss-wrote. I understand your concern. Thanks for passionately looking out for my well being, you saved my life. My variable has about 10,000 elements and sometime for the first 100 to 500 elements there is lots of 0's, so I end up with lots of NA/NaN/Inf's. However, when I try to use "Rolling" calculations I recieve error messages because the "Rolling" functions reject the NA/NaN/Inf's. So, I need 0's in place of the NA/NaN/Inf's so I can run the "Rolling" calculations. I can't just delete these observations, because it messes up lots of other other things within these dataframes. I'm well aware these "Rolling" calculations will be wrong in the beginning of the dataframe, so I just throw these out. The rolling window is only about 50 odservations, so out of 10,000, I still end up with ample correct data and calculations. So is this still idiotic? Thanks again for your concern. Now that you understand my situation a little better, you might be less distracted today and be able to sleep better tonight. Rolf Turner-3 wrote: On 25/07/2008, at 5:24 AM, Robert Baer wrote: I'm trying to calculate the percent change for a time-series variable. Basically the first several observations often look like this, x <- c(100, 0, 0, 150, 130, 0, 0, 200, 0) and then later in the life of the variable they're are generally no more 0's. So when I try to calculate the percent change from one observation to the next, I end up with a lot of NA/Nan/INF, and sometimes 0's which is what I want, in the beginning. I know I can use x <- na.omit(x), and other forms of this, to get rid of some of these errors. But I would rather use some kind of function that would by defult give a 0 while dividing by zero so that I don't lose the observation, which is what happens when I use na.omit. Well, this is not an error but proper behavior in the world of math that I know. However, to get what you want you could try x=(100-0)/0 if(!is.finite(x))x=0 x The foregoing response exemplifies what I think is the ***RIGHT*** way to answer wrong-headed questions on this list. ``What you want to do makes no sense, but if you insist on doing it, here's how.'' To my mind, wanting the result of division by zero to be zero *in general* is nothing short of idiotic. But if someone wants to impose this convention in his or her own calculations, well that's their ``democratic right''. And Robert Baer clearly and succinctly (and more tactfully than I) makes this clear. A similar style of response would have been appropriate in respect of the fooferaw that has been going on, on this mailing list on the topic of ``Coefficients of Logistic Regression from bootstrap - how to get them?'' cheers, Rolf Turner ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ 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. __ 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.
Re: [R] Dividing by 0
Hi, what about: mydata <- c(1,2,3,NA, Inf, -Inf, NaN, 5, 6, 7) mydata2 <- ifelse(is.na(mydata) | is.infinite(mydata), 0, mydata) mydata mydata2 nmarti wrote: I know I can use x <- na.omit(x), and other forms of this, to get rid of some of these errors. I know what you mean, I think, but I would not call it errors. Rather, it is following a standard specification. Check ?is.finite for further information (and the links therein). Hope this helps, Roland __ 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.
Re: [R] Dividing by 0
I'm well aware these are not errors, I guess I miss-wrote. I understand your concern. Thanks for passionately looking out for my well being, you saved my life. My variable has about 10,000 elements and sometime for the first 100 to 500 elements there is lots of 0's, so I end up with lots of NA/NaN/Inf's. However, when I try to use "Rolling" calculations I recieve error messages because the "Rolling" functions reject the NA/NaN/Inf's. So, I need 0's in place of the NA/NaN/Inf's so I can run the "Rolling" calculations. I can't just delete these observations, because it messes up lots of other other things within these dataframes. I'm well aware these "Rolling" calculations will be wrong in the beginning of the dataframe, so I just throw these out. The rolling window is only about 50 odservations, so out of 10,000, I still end up with ample correct data and calculations. So is this still idiotic? Thanks again for your concern. Now that you understand my situation a little better, you might be less distracted today and be able to sleep better tonight. Rolf Turner-3 wrote: > > > On 25/07/2008, at 5:24 AM, Robert Baer wrote: > >>> >>> I'm trying to calculate the percent change for a time-series >>> variable. >>> Basically the first several observations often look like this, >>> >>> x <- c(100, 0, 0, 150, 130, 0, 0, 200, 0) >>> >>> and then later in the life of the variable they're are generally >>> no more >>> 0's. So when I try to calculate the percent change from one >>> observation to >>> the next, I end up with a lot of NA/Nan/INF, and sometimes 0's >>> which is what >>> I want, in the beginning. >>> >>> I know I can use x <- na.omit(x), and other forms of this, to get >>> rid of >>> some of these errors. But I would rather use some kind of >>> function that >>> would by defult give a 0 while dividing by zero so that I don't >>> lose the >>> observation, which is what happens when I use na.omit. >>> >> >> Well, this is not an error but proper behavior in the world of math >> that I know. >> >> However, to get what you want you could try >> x=(100-0)/0 >> if(!is.finite(x))x=0 >> x > > The foregoing response exemplifies what I think is the ***RIGHT*** way > to answer wrong-headed questions on this list. ``What you want to do > makes no sense, but if you insist on doing it, here's how.'' > > To my mind, wanting the result of division by zero to be zero *in > general* > is nothing short of idiotic. But if someone wants to impose this > convention > in his or her own calculations, well that's their ``democratic right''. > And Robert Baer clearly and succinctly (and more tactfully than I) makes > this clear. > > A similar style of response would have been appropriate in respect of > the > fooferaw that has been going on, on this mailing list on the topic of > ``Coefficients of Logistic Regression from bootstrap - how to get > them?'' > > cheers, > > Rolf Turner > > ## > Attention:\ This e-mail message is privileged and confid...{{dropped:9}} > > __ > 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. > > -- View this message in context: http://www.nabble.com/Dividing-by-0-tp18632469p18654242.html Sent from the R help mailing list archive at Nabble.com. __ 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.
Re: [R] Dividing by 0
On Thu, 2008-07-24 at 06:57 -0700, nmarti wrote: > I'm trying to calculate the percent change for a time-series variable. > Basically the first several observations often look like this, > > x <- c(100, 0, 0, 150, 130, 0, 0, 200, 0) > > and then later in the life of the variable they're are generally no more > 0's. So when I try to calculate the percent change from one observation to > the next, I end up with a lot of NA/Nan/INF, and sometimes 0's which is what > I want, in the beginning. > > I know I can use x <- na.omit(x), and other forms of this, to get rid of > some of these errors. But I would rather use some kind of function that > would by defult give a 0 while dividing by zero so that I don't lose the > observation, which is what happens when I use na.omit. > > I would imagine this is a common problem. I tried finding something in zoo, > but I haven't found what I'm looking for. > Hi nmarti, If you are looking for percent change, it is probably easiest to write a little function that you can call for each pair of values. I'm assuming that all of your values are >= 0. pctchng<-function(x1,x2) { # don't try to calculate the value if(x1==0) { # if the second value is zero, there is no change if(x1==0) return(0) # otherwise there is infinite change # you may want to return another value here else return(Inf) } # it's okay, calculate the percentage change return(100*(x2-x1)/x1) } Jim __ 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.
Re: [R] Dividing by 0
On 25/07/2008, at 5:24 AM, Robert Baer wrote: I'm trying to calculate the percent change for a time-series variable. Basically the first several observations often look like this, x <- c(100, 0, 0, 150, 130, 0, 0, 200, 0) and then later in the life of the variable they're are generally no more 0's. So when I try to calculate the percent change from one observation to the next, I end up with a lot of NA/Nan/INF, and sometimes 0's which is what I want, in the beginning. I know I can use x <- na.omit(x), and other forms of this, to get rid of some of these errors. But I would rather use some kind of function that would by defult give a 0 while dividing by zero so that I don't lose the observation, which is what happens when I use na.omit. Well, this is not an error but proper behavior in the world of math that I know. However, to get what you want you could try x=(100-0)/0 if(!is.finite(x))x=0 x The foregoing response exemplifies what I think is the ***RIGHT*** way to answer wrong-headed questions on this list. ``What you want to do makes no sense, but if you insist on doing it, here's how.'' To my mind, wanting the result of division by zero to be zero *in general* is nothing short of idiotic. But if someone wants to impose this convention in his or her own calculations, well that's their ``democratic right''. And Robert Baer clearly and succinctly (and more tactfully than I) makes this clear. A similar style of response would have been appropriate in respect of the fooferaw that has been going on, on this mailing list on the topic of ``Coefficients of Logistic Regression from bootstrap - how to get them?'' cheers, Rolf Turner ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ 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.
Re: [R] Dividing by 0
I'm trying to calculate the percent change for a time-series variable. Basically the first several observations often look like this, x <- c(100, 0, 0, 150, 130, 0, 0, 200, 0) and then later in the life of the variable they're are generally no more 0's. So when I try to calculate the percent change from one observation to the next, I end up with a lot of NA/Nan/INF, and sometimes 0's which is what I want, in the beginning. I know I can use x <- na.omit(x), and other forms of this, to get rid of some of these errors. But I would rather use some kind of function that would by defult give a 0 while dividing by zero so that I don't lose the observation, which is what happens when I use na.omit. Well, this is not an error but proper behavior in the world of math that I know. However, to get what you want you could try x=(100-0)/0 if(!is.finite(x))x=0 x __ 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.