如何在R中使用迭代来简化GLM的代码?

发布于 2025-02-01 15:07:10 字数 949 浏览 3 评论 0原文

我刚刚开始使用R,在尝试简化我的代码时遇到了一些问题。我无法共享我的真实数据,但是已经使用一个开放数据集问我的问题(代表我的IV和年龄代表DV的品种)。

在我的数据集中,我都有所有因素变量 - 我的自变量具有3个级别,而因变量都有2个级别(0/1)。在一个较大的数据集中,我有六个因变量,并且希望为每个变量运行一些描述性统计数据和GLM。我已经找出了独立运行每个DV的工作代码,请参见下文。但是,我目前只是复制&粘贴此代码并每次替换DV变量。我想创建一个可以应用的函数,以简化我的代码。

我试图使用Purr软件包(地图)来执行此操作,但没有运气。如果有人可以使用下面的示例数据提供一个如何执行此操作的示例,这将有助于我很多(尽管我在下面的数据中知道只提供了一张DV)。

install.packages("GLMsData")
library(GLMsData)
data(butterfat)

library(tidyverse)
library(dplyr)
#Descriptive summaries
butterfat %>%
group_by(Breed, Age) %>%
summarise(n())

prop.table(table(butterfat$Breed, butterfat$Age), 1)

#Model
Age_model1 <- glm(Age ~ Breed, family=binomial, data=butterfat, na.action = na.omit)

#Get summary, including coefficients and p-values
summary(Age_model1)

#See coefficients, get odds ratio and confidence intervals
Age_model1$coefficients
exp(Age_model1$coefficients)
exp(confint(Age_model1))

I've just started using R and am having some issues when trying to simplify my code. I can't share my real data, but have used an open dataset to ask my question (Breed to represent my IV and Age to represent a DV).

In my dataset, I have all factor variables - my independent variable has 3 levels and my dependent variables all have 2 levels (0/1). Out of a larger dataset, I have six dependent variables and would like to run some descriptive stats and GLM for each. I have figured out working code for running each DV independently, see below. However, I am currently just copying & pasting this code and replacing the DV variables each time. I would like to instead create a function that I can apply to simplify my code.

I have attempted to do this using the purr package (map) but have had no luck. If someone could provide an example of how to do this using the sample data below, it would help me out a lot (though I know in the below data there is only one DV provided).

install.packages("GLMsData")
library(GLMsData)
data(butterfat)

library(tidyverse)
library(dplyr)
#Descriptive summaries
butterfat %>%
group_by(Breed, Age) %>%
summarise(n())

prop.table(table(butterfat$Breed, butterfat$Age), 1)

#Model
Age_model1 <- glm(Age ~ Breed, family=binomial, data=butterfat, na.action = na.omit)

#Get summary, including coefficients and p-values
summary(Age_model1)

#See coefficients, get odds ratio and confidence intervals
Age_model1$coefficients
exp(Age_model1$coefficients)
exp(confint(Age_model1))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2025-02-08 15:07:10

许多型号:用于数据科学的R

library(tidyverse)

iris %>%
  group_by(Species) %>%
  nest() %>% 
  mutate(lm = map(data,
                  ~glm(Sepal.Length~Sepal.Width,data = .))) %>% 
  mutate(tidy = map(lm,
                    broom::tidy)) %>% 
  unnest(tidy)

Many Models: R for Data Science

library(tidyverse)

iris %>%
  group_by(Species) %>%
  nest() %>% 
  mutate(lm = map(data,
                  ~glm(Sepal.Length~Sepal.Width,data = .))) %>% 
  mutate(tidy = map(lm,
                    broom::tidy)) %>% 
  unnest(tidy)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文