Please tell me the glm of each group on Rstudio.

Asked 1 years ago, Updated 1 years ago, 347 views


in the data frame (df) below glm(BP to age+sex+BMI, data=df).
Is there a way to analyze data frames by group (sex)?

Data frames interested:

IDage Sex BP BMI
1  43  0 120 21
2  62  1 130 26
3  54  1 132 23
4  55  0 110 19

r tidyverse rstudio

2022-10-22 09:12

1 Answers

If you use dplyr.

suppressMessages (library(dplyr))

df<-data.frame(
    ID = 1:4, age = c (43, 62, 54, 55), sex = c (0, 1, 1, 0),
    BP = c (120, 130, 132, 110), BMI = c (21, 26, 23, 19)
)

result<-df%>%group_by(sex)%>%do(model=glm(BP to age+sex+BMI, data=.))

result$sex
result$model

I also want to see the results of using summary() by group

purrr If you use the library map.

library(purrrr)

result $model%>%map(summary)

add

Is it possible to output to the model by group?

You may have a slightly different taste, but you can name each one with names().

names(result$model)=c("men", "woman")
result $model $men
result $model $woman


2022-10-22 09:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.