Re: [R] syvcoxph and cox.zph for testing the PH assumption
Thank you, Terry. We look forward to hearing from you again. Youyi On Mon, Jul 12, 2021 at 3:13 PM Therneau, Terry M., Ph.D. wrote: > > > On 7/11/21 5:00 AM, r-help-requ...@r-project.org wrote: > > Hello, is it kosher to call cox.zph on a syvcoxph model fit? I see that > > someone proposed a modified version of cox.zph that uses resid(fit, > > 'schoenfeld', **weighted=TRUE**). > > > > > https://stats.stackexchange.com/questions/265307/assessing-proportional-hazards-assumption-of-a-cox-model-with-caseweights > > Is that all it takes? > > Thanks, > > Youyi > > The cox.zph function does a formal score test. No, it does not account > for robust > variance. I hadn't considered that case, but will now think about it. It > is quite easy > to show that there is a problem: just give everyone a weight of 100. > > The stackexchange conversation was new to me. The solution there won't > work with the > current code, which does not make use of resid(). It has been updated to > do the proper > score test, the older version of cox.zph, which they modified, used an > approximation. > > Terry T. > [[alternative HTML version deleted]] __ R-help@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.
Re: [R] syvcoxph and cox.zph for testing the PH assumption
On 7/11/21 5:00 AM, r-help-requ...@r-project.org wrote: Hello, is it kosher to call cox.zph on a syvcoxph model fit? I see that someone proposed a modified version of cox.zph that uses resid(fit, 'schoenfeld', **weighted=TRUE**). https://stats.stackexchange.com/questions/265307/assessing-proportional-hazards-assumption-of-a-cox-model-with-caseweights Is that all it takes? Thanks, Youyi The cox.zph function does a formal score test. No, it does not account for robust variance. I hadn't considered that case, but will now think about it. It is quite easy to show that there is a problem: just give everyone a weight of 100. The stackexchange conversation was new to me. The solution there won't work with the current code, which does not make use of resid(). It has been updated to do the proper score test, the older version of cox.zph, which they modified, used an approximation. Terry T. __ R-help@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.
Re: [R] density with weights missing values
On 12/07/2021 1:22 p.m., matthias-gondan wrote: You're right, of course. Extrapolating your argument a bit, the whole practice of na.rm is questionable, since there's always a reason for missingness (that is not in x and rarely elsewhere in the data)Best wishes Matthias For what it's worth, I partly agree with you: if you specify na.rm = TRUE, it shouldn't make your x and weights vectors incompatible. Regarding the warning about the sum of weights: perhaps there's some reason that someone would want to create an unnormalized density, and that lets you do it. An unnormalized mean doesn't make any sense, so I wouldn't call it a design flaw that the weighted density behaves differently than the weighted mean. On the other hand, it would likely make more sense to normalize the density, and that's how I hope I would have designed it. Thinking about this, I guessed density() was a really old function, so this was a case of trying to be S-compatible, but it turns out the weights argument was added in 2005 in r34130, so perhaps someone still remembers what the thinking was. Duncan Murdoch P.S. I think you're posting in HTML, which makes your messages look really messy. If you can turn that off, they'd be clearer. __ R-help@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.
Re: [R] density with weights missing values
I think the missing weights are more crucial than equally-weighted missing data would be. what if there is a heavy weight on the missing values? it could completely change the interpretation of the result. On July 12, 2021 10:22:19 AM PDT, matthias-gondan wrote: >You're right, of course. Extrapolating your argument a bit, the whole >practice of na.rm is questionable, since there's always a reason for >missingness (that is not in x and rarely elsewhere in the data)Best >wishes Matthias > Ursprüngliche Nachricht Von: Jeff Newmiller > Datum: 12.07.21 18:44 (GMT+01:00) An: >r-help@r-project.org, matthias-gondan , Bert >Gunter Cc: r-help@r-project.org Betreff: Re: >[R] density with weights missing values Sure, you might think that.But >most likely the reason this code has not been corrected is that when >you give weights for missing data the most correct result is for your >entire density to be invalid.Fix your inputs so they make sense to you >and there is no problem. But absent your intellectual input to >restructure your problem the weights no longer make sense once >density() removes the NAs from the data.On July 12, 2021 9:13:12 AM >PDT, matthias-gondan wrote:>The thing is that >for na.rm=TRUE, I would expect the weights>corresponding to the missing >x to be removed, as well. Like in>weighted.mean. So this one shouldn't >raise an error,density(c(1, 2, 3,>4, 5, NA), na.rm=TRUE, weights=c(1, >1, 1, 1, 1, 1))Or am I missing>something? > Ursprüngliche >Nachricht Von: Bert Gunter> Datum: >12.07.21 16:25 (GMT+01:00) An:>Matthias Gondan > Cc: r-help@r-project.org>Betreff: Re: [R] >density with weights missing values The behavior is as>documented >AFAICS.na.rmlogical; if TRUE, missing values are removed>from x. If >FALSE anymissing values cause an error.The default >is>FALSE.weightsnumeric vector of non-negative observation weights.NA >is>not a non-negative numeric.Bert Gunter"The trouble with having an >open>mind is that people keep coming alongand sticking things into >it."-->Opus (aka Berkeley Breathed in his "Bloom County" comic strip >)Bert>Gunter"The trouble with having an open mind is that people keep >coming>alongand sticking things into it."-- Opus (aka Berkeley Breathed >in his>"Bloom County" comic strip )On Mon, Jul 12, 2021 at 6:10 AM >Matthias>Gondan wrote:>> Weighted mean >behaves>differently:> • weight is excluded for missing x> • no warning >for>sum(weights) != 1>> > weighted.mean(c(1, 2, 3, 4), weights=c(1, 1, >1,>1))> [1] 2.5> > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, >1))>>[1] NA> > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, >1),>na.rm=TRUE)> [1] 2> Von: Richard O'Keefe> Gesendet: Montag, >12.>Juli 2021 13:18> An: Matthias Gondan> Betreff: Re: [R] density >with>weights missing values>> Does your copy of R say that the weights >must>add up to 1?> ?density doesn't say that in mine. But it does >check.>>>On Mon, 12 Jul 2021 at 22:42, Matthias Gondan >>wrote:> >> > Dear R users,> >> > This works as >expected:> >> > •>plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE))> >> > >This raises an>error> >> > • plot(density(c(1,2, 3, 4, 5, NA), >na.rm=TRUE,>weights=c(1, 1, 1, 1, 1, 1)))> > • plot(density(c(1,2, 3, >4, 5, NA),>na.rm=TRUE, weights=c(1, 1, 1, 1, 1, NA)))> >> > This seems >to work (it>triggers a warning that the weights don’t add up to 1, >which makes>sense*):> >> > • plot(density(c(1,2, 3, 4, 5, NA), >na.rm=TRUE,>weights=c(1, 1, 1, 1, 1)))> >> > Questions> >> > • But >shouldn’t the>na.rm filter also filter the corresponding weights?> > • >Extra>question: In case the na.rm filter is changed to filter the >weights,>the check for sum(weights) == 1 might trigger false positive >warnings>since the weights might not add up to 1 anymore> >> > Best >wishes,> Matthias> >> >> > [[alternative HTML version >deleted]]> >> >>__> > >R-help@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@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@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.-- Sent from my >phone. Please excuse my brevity. -- Sent from my phone. Please excuse my brevity. __
Re: [R] density with weights missing values
You're right, of course. Extrapolating your argument a bit, the whole practice of na.rm is questionable, since there's always a reason for missingness (that is not in x and rarely elsewhere in the data)Best wishes Matthias Ursprüngliche Nachricht Von: Jeff Newmiller Datum: 12.07.21 18:44 (GMT+01:00) An: r-help@r-project.org, matthias-gondan , Bert Gunter Cc: r-help@r-project.org Betreff: Re: [R] density with weights missing values Sure, you might think that.But most likely the reason this code has not been corrected is that when you give weights for missing data the most correct result is for your entire density to be invalid.Fix your inputs so they make sense to you and there is no problem. But absent your intellectual input to restructure your problem the weights no longer make sense once density() removes the NAs from the data.On July 12, 2021 9:13:12 AM PDT, matthias-gondan wrote:>The thing is that for na.rm=TRUE, I would expect the weights>corresponding to the missing x to be removed, as well. Like in>weighted.mean. So this one shouldn't raise an error,density(c(1, 2, 3,>4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, 1))Or am I missing>something? > Ursprüngliche Nachricht Von: Bert Gunter> Datum: 12.07.21 16:25 (GMT+01:00) An:>Matthias Gondan Cc: r-help@r-project.org>Betreff: Re: [R] density with weights missing values The behavior is as>documented AFAICS.na.rmlogical; if TRUE, missing values are removed>from x. If FALSE anymissing values cause an error.The default is>FALSE.weightsnumeric vector of non-negative observation weights.NA is>not a non-negative numeric.Bert Gunter"The trouble with having an open>mind is that people keep coming alongand sticking things into it."-->Opus (aka Berkeley Breathed in his "Bloom County" comic strip )Bert>Gunter"The trouble with having an open mind is that people keep coming>alongand sticking things into it."-- Opus (aka Berkeley Breathed in his>"Bloom County" comic strip )On Mon, Jul 12, 2021 at 6:10 AM Matthias>Gondan wrote:>> Weighted mean behaves>differently:> • weight is excluded for missing x> • no warning for>sum(weights) != 1>> > weighted.mean(c(1, 2, 3, 4), weights=c(1, 1, 1,>1))> [1] 2.5> > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1))>>[1] NA> > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1),>na.rm=TRUE)> [1] 2> Von: Richard O'Keefe> Gesendet: Montag, 12.>Juli 2021 13:18> An: Matthias Gondan> Betreff: Re: [R] density with>weights missing values>> Does your copy of R say that the weights must>add up to 1?> ?density doesn't say that in mine. But it does check.>>>On Mon, 12 Jul 2021 at 22:42, Matthias Gondan >wrote:> >> > Dear R users,> >> > This works as expected:> >> > •>plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE))> >> > This raises an>error> >> > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE,>weights=c(1, 1, 1, 1, 1, 1)))> > • plot(density(c(1,2, 3, 4, 5, NA),>na.rm=TRUE, weights=c(1, 1, 1, 1, 1, NA)))> >> > This seems to work (it>triggers a warning that the weights don’t add up to 1, which makes>sense*):> >> > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE,>weights=c(1, 1, 1, 1, 1)))> >> > Questions> >> > • But shouldn’t the>na.rm filter also filter the corresponding weights?> > • Extra>question: In case the na.rm filter is changed to filter the weights,>the check for sum(weights) == 1 might trigger false positive warnings>since the weights might not add up to 1 anymore> >> > Best wishes,> Matthias> >> >> > [[alternative HTML version deleted]]> >> >>__> > R-help@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@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@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.-- Sent from my phone. Please excuse my brevity. [[alternative HTML version deleted]] __ R-help@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, rep
Re: [R] density with weights missing values
Sure, you might think that. But most likely the reason this code has not been corrected is that when you give weights for missing data the most correct result is for your entire density to be invalid. Fix your inputs so they make sense to you and there is no problem. But absent your intellectual input to restructure your problem the weights no longer make sense once density() removes the NAs from the data. On July 12, 2021 9:13:12 AM PDT, matthias-gondan wrote: >The thing is that for na.rm=TRUE, I would expect the weights >corresponding to the missing x to be removed, as well. Like in >weighted.mean. So this one shouldn't raise an error,density(c(1, 2, 3, >4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, 1))Or am I missing >something? > Ursprüngliche Nachricht Von: Bert Gunter > Datum: 12.07.21 16:25 (GMT+01:00) An: >Matthias Gondan Cc: r-help@r-project.org >Betreff: Re: [R] density with weights missing values The behavior is as >documented AFAICS.na.rmlogical; if TRUE, missing values are removed >from x. If FALSE anymissing values cause an error.The default is >FALSE.weightsnumeric vector of non-negative observation weights.NA is >not a non-negative numeric.Bert Gunter"The trouble with having an open >mind is that people keep coming alongand sticking things into it."-- >Opus (aka Berkeley Breathed in his "Bloom County" comic strip )Bert >Gunter"The trouble with having an open mind is that people keep coming >alongand sticking things into it."-- Opus (aka Berkeley Breathed in his >"Bloom County" comic strip )On Mon, Jul 12, 2021 at 6:10 AM Matthias >Gondan wrote:>> Weighted mean behaves >differently:> • weight is excluded for missing x> • no warning for >sum(weights) != 1>> > weighted.mean(c(1, 2, 3, 4), weights=c(1, 1, 1, >1))> [1] 2.5> > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1))> >[1] NA> > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1), >na.rm=TRUE)> [1] 2> Von: Richard O'Keefe> Gesendet: Montag, 12. >Juli 2021 13:18> An: Matthias Gondan> Betreff: Re: [R] density with >weights missing values>> Does your copy of R say that the weights must >add up to 1?> ?density doesn't say that in mine. But it does check.>> >On Mon, 12 Jul 2021 at 22:42, Matthias Gondan >wrote:> >> > Dear R users,> >> > This works as expected:> >> > • >plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE))> >> > This raises an >error> >> > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, >weights=c(1, 1, 1, 1, 1, 1)))> > • plot(density(c(1,2, 3, 4, 5, NA), >na.rm=TRUE, weights=c(1, 1, 1, 1, 1, NA)))> >> > This seems to work (it >triggers a warning that the weights don’t add up to 1, which makes >sense*):> >> > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, >weights=c(1, 1, 1, 1, 1)))> >> > Questions> >> > • But shouldn’t the >na.rm filter also filter the corresponding weights?> > • Extra >question: In case the na.rm filter is changed to filter the weights, >the check for sum(weights) == 1 might trigger false positive warnings >since the weights might not add up to 1 anymore> >> > Best wishes,> >> >> Matthias> >> >> > [[alternative HTML version deleted]]> >> > >__> > R-help@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@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@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. -- Sent from my phone. Please excuse my brevity. __ R-help@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.
Re: [R] density with weights missing values
My point (confusingly made!) is that documented behavior is all you should expect. The docs say that weights must be non-negative numeric. If they aren't... "Consistency" of behavior among different functions is highly subjective -- it depends exactly on what one considers to be "consistent", nicht wahr? And, of course, with thousands of packages and hundreds of weight functions used for different purposes, this seems a practical impossibility here. However, I would agree that given R's "organic" growth over time, "jarring" inconsistencies (i.e. that most would agree are inconsistent) may exist. This may be such a case. But, again, all you can do is follow the docs whether or not the behavior meets your "reasonable" expectations. Just my opinion, of course. Consume at your own risk. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Jul 12, 2021 at 9:13 AM matthias-gondan wrote: > > The thing is that for na.rm=TRUE, I would expect the weights corresponding to > the missing x to be removed, as well. Like in weighted.mean. So this one > shouldn't raise an error, > > density(c(1, 2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, 1)) > > Or am I missing something? > > Ursprüngliche Nachricht > Von: Bert Gunter > Datum: 12.07.21 16:25 (GMT+01:00) > An: Matthias Gondan > Cc: r-help@r-project.org > Betreff: Re: [R] density with weights missing values > > The behavior is as documented AFAICS. > > na.rm > logical; if TRUE, missing values are removed from x. If FALSE any > missing values cause an error. > > The default is FALSE. > > weights > numeric vector of non-negative observation weights. > > NA is not a non-negative numeric. > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > Bert Gunter > > "The trouble with having an open mind is that people keep coming along > and sticking things into it." > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) > > > On Mon, Jul 12, 2021 at 6:10 AM Matthias Gondan > wrote: > > > > Weighted mean behaves differently: > > • weight is excluded for missing x > > • no warning for sum(weights) != 1 > > > > > weighted.mean(c(1, 2, 3, 4), weights=c(1, 1, 1, 1)) > > [1] 2.5 > > > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1)) > > [1] NA > > > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1), na.rm=TRUE) > > [1] 2 > > > > > > > > > > Von: Richard O'Keefe > > Gesendet: Montag, 12. Juli 2021 13:18 > > An: Matthias Gondan > > Betreff: Re: [R] density with weights missing values > > > > Does your copy of R say that the weights must add up to 1? > > ?density doesn't say that in mine. But it does check. > > > > On Mon, 12 Jul 2021 at 22:42, Matthias Gondan > > wrote: > > > > > > Dear R users, > > > > > > This works as expected: > > > > > > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE)) > > > > > > This raises an error > > > > > > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, > > > 1))) > > > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, > > > NA))) > > > > > > This seems to work (it triggers a warning that the weights don’t add up > > > to 1, which makes sense*): > > > > > > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1))) > > > > > > Questions > > > > > > • But shouldn’t the na.rm filter also filter the corresponding weights? > > > • Extra question: In case the na.rm filter is changed to filter the > > > weights, the check for sum(weights) == 1 might trigger false positive > > > warnings since the weights might not add up to 1 anymore > > > > > > Best wishes, > > > > > > Matthias > > > > > > > > > [[alternative HTML version deleted]] > > > > > > __ > > > R-help@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@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@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.
Re: [R] density with weights missing values
The thing is that for na.rm=TRUE, I would expect the weights corresponding to the missing x to be removed, as well. Like in weighted.mean. So this one shouldn't raise an error,density(c(1, 2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, 1))Or am I missing something? Ursprüngliche Nachricht Von: Bert Gunter Datum: 12.07.21 16:25 (GMT+01:00) An: Matthias Gondan Cc: r-help@r-project.org Betreff: Re: [R] density with weights missing values The behavior is as documented AFAICS.na.rmlogical; if TRUE, missing values are removed from x. If FALSE anymissing values cause an error.The default is FALSE.weightsnumeric vector of non-negative observation weights.NA is not a non-negative numeric.Bert Gunter"The trouble with having an open mind is that people keep coming alongand sticking things into it."-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )Bert Gunter"The trouble with having an open mind is that people keep coming alongand sticking things into it."-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )On Mon, Jul 12, 2021 at 6:10 AM Matthias Gondan wrote:>> Weighted mean behaves differently:> • weight is excluded for missing x> • no warning for sum(weights) != 1>> > weighted.mean(c(1, 2, 3, 4), weights=c(1, 1, 1, 1))> [1] 2.5> > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1))> [1] NA> > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1), na.rm=TRUE)> [1] 2> Von: Richard O'Keefe> Gesendet: Montag, 12. Juli 2021 13:18> An: Matthias Gondan> Betreff: Re: [R] density with weights missing values>> Does your copy of R say that the weights must add up to 1?> ?density doesn't say that in mine. But it does check.>> On Mon, 12 Jul 2021 at 22:42, Matthias Gondan wrote:> >> > Dear R users,> >> > This works as expected:> >> > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE))> >> > This raises an error> >> > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, 1)))> > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, NA)))> >> > This seems to work (it triggers a warning that the weights don’t add up to 1, which makes sense*):> >> > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1)))> >> > Questions> >> > • But shouldn’t the na.rm filter also filter the corresponding weights?> > • Extra question: In case the na.rm filter is changed to filter the weights, the check for sum(weights) == 1 might trigger false positive warnings since the weights might not add up to 1 anymore> >> > Best wishes,> >> > Matthias> >> >> > [[alternative HTML version deleted]]> >> > __> > R-help@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@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@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.
Re: [R] density with weights missing values
The behavior is as documented AFAICS. na.rm logical; if TRUE, missing values are removed from x. If FALSE any missing values cause an error. The default is FALSE. weights numeric vector of non-negative observation weights. NA is not a non-negative numeric. Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Jul 12, 2021 at 6:10 AM Matthias Gondan wrote: > > Weighted mean behaves differently: > • weight is excluded for missing x > • no warning for sum(weights) != 1 > > > weighted.mean(c(1, 2, 3, 4), weights=c(1, 1, 1, 1)) > [1] 2.5 > > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1)) > [1] NA > > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1), na.rm=TRUE) > [1] 2 > > > > > Von: Richard O'Keefe > Gesendet: Montag, 12. Juli 2021 13:18 > An: Matthias Gondan > Betreff: Re: [R] density with weights missing values > > Does your copy of R say that the weights must add up to 1? > ?density doesn't say that in mine. But it does check. > > On Mon, 12 Jul 2021 at 22:42, Matthias Gondan wrote: > > > > Dear R users, > > > > This works as expected: > > > > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE)) > > > > This raises an error > > > > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, > > 1))) > > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, > > NA))) > > > > This seems to work (it triggers a warning that the weights don’t add up to > > 1, which makes sense*): > > > > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1))) > > > > Questions > > > > • But shouldn’t the na.rm filter also filter the corresponding weights? > > • Extra question: In case the na.rm filter is changed to filter the > > weights, the check for sum(weights) == 1 might trigger false positive > > warnings since the weights might not add up to 1 anymore > > > > Best wishes, > > > > Matthias > > > > > > [[alternative HTML version deleted]] > > > > __ > > R-help@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@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@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.
Re: [R] density with weights missing values
Weighted mean behaves differently: • weight is excluded for missing x • no warning for sum(weights) != 1 > weighted.mean(c(1, 2, 3, 4), weights=c(1, 1, 1, 1)) [1] 2.5 > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1)) [1] NA > weighted.mean(c(1, 2, 3, NA), weights=c(1, 1, 1, 1), na.rm=TRUE) [1] 2 Von: Richard O'Keefe Gesendet: Montag, 12. Juli 2021 13:18 An: Matthias Gondan Betreff: Re: [R] density with weights missing values Does your copy of R say that the weights must add up to 1? ?density doesn't say that in mine. But it does check. On Mon, 12 Jul 2021 at 22:42, Matthias Gondan wrote: > > Dear R users, > > This works as expected: > > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE)) > > This raises an error > > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, 1))) > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, NA))) > > This seems to work (it triggers a warning that the weights don’t add up to 1, > which makes sense*): > > • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1))) > > Questions > > • But shouldn’t the na.rm filter also filter the corresponding weights? > • Extra question: In case the na.rm filter is changed to filter the weights, > the check for sum(weights) == 1 might trigger false positive warnings since > the weights might not add up to 1 anymore > > Best wishes, > > Matthias > > > [[alternative HTML version deleted]] > > __ > R-help@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@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] density with weights missing values
Dear R users, This works as expected: • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE)) This raises an error • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, 1))) • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1, NA))) This seems to work (it triggers a warning that the weights don’t add up to 1, which makes sense*): • plot(density(c(1,2, 3, 4, 5, NA), na.rm=TRUE, weights=c(1, 1, 1, 1, 1))) Questions • But shouldn’t the na.rm filter also filter the corresponding weights? • Extra question: In case the na.rm filter is changed to filter the weights, the check for sum(weights) == 1 might trigger false positive warnings since the weights might not add up to 1 anymore Best wishes, Matthias [[alternative HTML version deleted]] __ R-help@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.