John Smith
2020-Aug-25 15:33 UTC
[R] How to obtain individual log-likelihood value from glm?
Dear R-help, The function logLik can be used to obtain the maximum log-likelihood value from a glm object. This is an aggregated value, a summation of individual log-likelihood values. How do I obtain individual values? In the following example, I would expect 9 numbers since the response has length 9. I could write a function to compute the values, but there are lots of family members in glm, and I am trying not to reinvent wheels. Thanks! counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3,1,9) treatment <- gl(3,3) data.frame(treatment, outcome, counts) # showing data glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) (ll <- logLik(glm.D93)) [[alternative HTML version deleted]]
Bert Gunter
2020-Aug-25 16:29 UTC
[R] How to obtain individual log-likelihood value from glm?
If you look at stats:::logLik.glm #3 ":" because it's unexported, as is true of most methods it should be obvious. Cheers, Bert 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 Tue, Aug 25, 2020 at 8:34 AM John Smith <jswhct at gmail.com> wrote:> Dear R-help, > > The function logLik can be used to obtain the maximum log-likelihood value > from a glm object. This is an aggregated value, a summation of individual > log-likelihood values. How do I obtain individual values? In the following > example, I would expect 9 numbers since the response has length 9. I could > write a function to compute the values, but there are lots of > family members in glm, and I am trying not to reinvent wheels. Thanks! > > counts <- c(18,17,15,20,10,20,25,13,12) > outcome <- gl(3,1,9) > treatment <- gl(3,3) > data.frame(treatment, outcome, counts) # showing data > glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) > (ll <- logLik(glm.D93)) > > [[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. >[[alternative HTML version deleted]]
peter dalgaard
2020-Aug-25 16:40 UTC
[R] How to obtain individual log-likelihood value from glm?
If you don't worry too much about an additive constant, then half the negative squared deviance residuals should do. (Not quite sure how weights factor in. Looks like they are accounted for.) -pd> On 25 Aug 2020, at 17:33 , John Smith <jswhct at gmail.com> wrote: > > Dear R-help, > > The function logLik can be used to obtain the maximum log-likelihood value > from a glm object. This is an aggregated value, a summation of individual > log-likelihood values. How do I obtain individual values? In the following > example, I would expect 9 numbers since the response has length 9. I could > write a function to compute the values, but there are lots of > family members in glm, and I am trying not to reinvent wheels. Thanks! > > counts <- c(18,17,15,20,10,20,25,13,12) > outcome <- gl(3,1,9) > treatment <- gl(3,3) > data.frame(treatment, outcome, counts) # showing data > glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) > (ll <- logLik(glm.D93)) > > [[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.-- 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
John Smith
2020-Aug-28 01:58 UTC
[R] How to obtain individual log-likelihood value from glm?
Thanks Peter for a very promising tip. On Tue, Aug 25, 2020 at 11:40 AM peter dalgaard <pdalgd at gmail.com> wrote:> If you don't worry too much about an additive constant, then half the > negative squared deviance residuals should do. (Not quite sure how weights > factor in. Looks like they are accounted for.) > > -pd > > > On 25 Aug 2020, at 17:33 , John Smith <jswhct at gmail.com> wrote: > > > > Dear R-help, > > > > The function logLik can be used to obtain the maximum log-likelihood > value > > from a glm object. This is an aggregated value, a summation of individual > > log-likelihood values. How do I obtain individual values? In the > following > > example, I would expect 9 numbers since the response has length 9. I > could > > write a function to compute the values, but there are lots of > > family members in glm, and I am trying not to reinvent wheels. Thanks! > > > > counts <- c(18,17,15,20,10,20,25,13,12) > > outcome <- gl(3,1,9) > > treatment <- gl(3,3) > > data.frame(treatment, outcome, counts) # showing data > > glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) > > (ll <- logLik(glm.D93)) > > > > [[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. > > -- > 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 > > > > > > > > > >[[alternative HTML version deleted]]
John Smith
2020-Aug-29 01:28 UTC
[R] How to obtain individual log-likelihood value from glm?
If the weights < 1, then we have different values! See an example below. How should I interpret logLik value then? set.seed(135) y <- c(rep(0, 50), rep(1, 50)) x <- rnorm(100) data <- data.frame(cbind(x, y)) weights <- c(rep(1, 50), rep(2, 50)) fit <- glm(y~x, data, family=binomial(), weights/10) res.dev <- residuals(fit, type="deviance") res2 <- -0.5*res.dev^2 cat("loglikelihood value", logLik(fit), sum(res2), "\n") On Tue, Aug 25, 2020 at 11:40 AM peter dalgaard <pdalgd at gmail.com> wrote:> If you don't worry too much about an additive constant, then half the > negative squared deviance residuals should do. (Not quite sure how weights > factor in. Looks like they are accounted for.) > > -pd > > > On 25 Aug 2020, at 17:33 , John Smith <jswhct at gmail.com> wrote: > > > > Dear R-help, > > > > The function logLik can be used to obtain the maximum log-likelihood > value > > from a glm object. This is an aggregated value, a summation of individual > > log-likelihood values. How do I obtain individual values? In the > following > > example, I would expect 9 numbers since the response has length 9. I > could > > write a function to compute the values, but there are lots of > > family members in glm, and I am trying not to reinvent wheels. Thanks! > > > > counts <- c(18,17,15,20,10,20,25,13,12) > > outcome <- gl(3,1,9) > > treatment <- gl(3,3) > > data.frame(treatment, outcome, counts) # showing data > > glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) > > (ll <- logLik(glm.D93)) > > > > [[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. > > -- > 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 > > > > > > > > > >[[alternative HTML version deleted]]
John Fox
2020-Aug-29 03:51 UTC
[R] How to obtain individual log-likelihood value from glm?
Dear John I think that you misunderstand the use of the weights argument to glm() for a binomial GLM. From ?glm: "For a binomial GLM prior weights are used to give the number of trials when the response is the proportion of successes." That is, in this case y should be the observed proportion of successes (i.e., between 0 and 1) and the weights are integers giving the number of trials for each binomial observation. I hope this helps, John John Fox, Professor Emeritus McMaster University Hamilton, Ontario, Canada web: https://socialsciences.mcmaster.ca/jfox/ On 2020-08-28 9:28 p.m., John Smith wrote:> If the weights < 1, then we have different values! See an example below. > How should I interpret logLik value then? > > set.seed(135) > y <- c(rep(0, 50), rep(1, 50)) > x <- rnorm(100) > data <- data.frame(cbind(x, y)) > weights <- c(rep(1, 50), rep(2, 50)) > fit <- glm(y~x, data, family=binomial(), weights/10) > res.dev <- residuals(fit, type="deviance") > res2 <- -0.5*res.dev^2 > cat("loglikelihood value", logLik(fit), sum(res2), "\n") > > On Tue, Aug 25, 2020 at 11:40 AM peter dalgaard <pdalgd at gmail.com> wrote: > >> If you don't worry too much about an additive constant, then half the >> negative squared deviance residuals should do. (Not quite sure how weights >> factor in. Looks like they are accounted for.) >> >> -pd >> >>> On 25 Aug 2020, at 17:33 , John Smith <jswhct at gmail.com> wrote: >>> >>> Dear R-help, >>> >>> The function logLik can be used to obtain the maximum log-likelihood >> value >>> from a glm object. This is an aggregated value, a summation of individual >>> log-likelihood values. How do I obtain individual values? In the >> following >>> example, I would expect 9 numbers since the response has length 9. I >> could >>> write a function to compute the values, but there are lots of >>> family members in glm, and I am trying not to reinvent wheels. Thanks! >>> >>> counts <- c(18,17,15,20,10,20,25,13,12) >>> outcome <- gl(3,1,9) >>> treatment <- gl(3,3) >>> data.frame(treatment, outcome, counts) # showing data >>> glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) >>> (ll <- logLik(glm.D93)) >>> >>> [[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. >> >> -- >> 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 >> >> >> >> >> >> >> >> >> >> > > [[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. >
John Smith
2020-Aug-29 05:30 UTC
[R] How to obtain individual log-likelihood value from glm?
Thanks Prof. Fox. I am curious: what is the model estimated below? I guess my inquiry seems more complicated than I thought: with y being 0/1, how to fit weighted logistic regression with weights <1, in the sense of weighted least squares? Thanks> On Aug 28, 2020, at 10:51 PM, John Fox <jfox at mcmaster.ca> wrote: > > Dear John > > I think that you misunderstand the use of the weights argument to glm() for a binomial GLM. From ?glm: "For a binomial GLM prior weights are used to give the number of trials when the response is the proportion of successes." That is, in this case y should be the observed proportion of successes (i.e., between 0 and 1) and the weights are integers giving the number of trials for each binomial observation. > > I hope this helps, > John > > John Fox, Professor Emeritus > McMaster University > Hamilton, Ontario, Canada > web: https://socialsciences.mcmaster.ca/jfox/ > >> On 2020-08-28 9:28 p.m., John Smith wrote: >> If the weights < 1, then we have different values! See an example below. >> How should I interpret logLik value then? >> set.seed(135) >> y <- c(rep(0, 50), rep(1, 50)) >> x <- rnorm(100) >> data <- data.frame(cbind(x, y)) >> weights <- c(rep(1, 50), rep(2, 50)) >> fit <- glm(y~x, data, family=binomial(), weights/10) >> res.dev <- residuals(fit, type="deviance") >> res2 <- -0.5*res.dev^2 >> cat("loglikelihood value", logLik(fit), sum(res2), "\n") >>> On Tue, Aug 25, 2020 at 11:40 AM peter dalgaard <pdalgd at gmail.com> wrote: >>> If you don't worry too much about an additive constant, then half the >>> negative squared deviance residuals should do. (Not quite sure how weights >>> factor in. Looks like they are accounted for.) >>> >>> -pd >>> >>>> On 25 Aug 2020, at 17:33 , John Smith <jswhct at gmail.com> wrote: >>>> >>>> Dear R-help, >>>> >>>> The function logLik can be used to obtain the maximum log-likelihood >>> value >>>> from a glm object. This is an aggregated value, a summation of individual >>>> log-likelihood values. How do I obtain individual values? In the >>> following >>>> example, I would expect 9 numbers since the response has length 9. I >>> could >>>> write a function to compute the values, but there are lots of >>>> family members in glm, and I am trying not to reinvent wheels. Thanks! >>>> >>>> counts <- c(18,17,15,20,10,20,25,13,12) >>>> outcome <- gl(3,1,9) >>>> treatment <- gl(3,3) >>>> data.frame(treatment, outcome, counts) # showing data >>>> glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) >>>> (ll <- logLik(glm.D93)) >>>> >>>> [[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. >>> >>> -- >>> 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 >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >> [[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.
peter dalgaard
2020-Aug-29 09:31 UTC
[R] How to obtain individual log-likelihood value from glm?
> On 25 Aug 2020, at 18:40 , peter dalgaard <pdalgd at gmail.com> wrote: > > If you don't worry too much about an additive constant, then half the negative squared deviance residuals should do. (Not quite sure how weights factor in. Looks like they are accounted for.) > > -pd > >> On 25 Aug 2020, at 17:33 , John Smith <jswhct at gmail.com> wrote: >> >> Dear R-help, >> >> The function logLik can be used to obtain the maximum log-likelihood value >> from a glm object. This is an aggregated value, a summation of individual >> log-likelihood values. How do I obtain individual values? In the following >> example, I would expect 9 numbers since the response has length 9. I could >> write a function to compute the values, but there are lots of >> family members in glm, and I am trying not to reinvent wheels. Thanks! >> >> counts <- c(18,17,15,20,10,20,25,13,12) >> outcome <- gl(3,1,9) >> treatment <- gl(3,3) >> data.frame(treatment, outcome, counts) # showing data >> glm.D93 <- glm(counts ~ outcome + treatment, family = poisson()) >> (ll <- logLik(glm.D93)) >> >> [[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. > > -- > 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 > > > > > > > > >-- 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