[R] density with weights missing values

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Mon Jul 12 18:44:43 CEST 2021


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 <matthias-gondan using gmx.de> 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
><bgunter.4567 using gmail.com> Datum: 12.07.21  16:25  (GMT+01:00) An:
>Matthias Gondan <matthias-gondan using gmx.de> Cc: r-help using 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 <matthias-gondan using gmx.de> 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 <matthias-gondan using gmx.de>
>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 using 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 using 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 using 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.



More information about the R-help mailing list