2
votes

I have a data frame with n=18 participants. There are 90 observations across 3 IVs and 1 binary DV (see below for shortened example).

data <- data.frame(age = c(21, 30, 25, 41, 29, 33),IQ=c(60,70,80,90,100,200),SAT=(2400,2200,1400,1550,1470,1300), sex = factor(c(1, 2, 1, 2, 1, 2), labels = c("Female", "Male")))

I've already done a logistic glm regression using the entire sample. This was the code used:

model2<-glm(Sex~Age+IQ+SAT,family=binomial(link="logit"))
summary(model2)

I want to compute the beta coefficients for each participant-- Age, IQ, and SAT scores to predict Sex. I should have 18 coefficients to plot. Thanks in advance!!

1
Try this loading broom and tidyverse. If you use only one observation all coefs will be NA: data %>% mutate(id=1:n()) %>% group_by(id) %>% do(fitmod = tidy(glm(sex~age+IQ+SAT,family=binomial(link="logit"), data = .))) %>% unnest(fitmod) Duck
thanks for your response. I'm getting an error message:Chester
Error in mutate(., id = 1:n()) : is.data.frame(.data) || is.list(.data) || is.environment(.data) is not TRUEChester
Did you first used library(tidyverse) and library(broom)?Duck
Fantastic, so I will add the code as solution. It can help others.Duck

1 Answers

1
votes

You can try this approach using broom and tidyverse:

library(tidyverse)
library(broom)
#Code
data %>% mutate(id=1:n()) %>% group_by(id) %>%
  do(fitmod = tidy(glm(sex~age+IQ+SAT,family=binomial(link="logit"), data = .))) %>% 
  unnest(fitmod)