使用 purrr::map() 更改和分配新变量名称 [英] Changing and assigning new variable names with purrr::map()

查看:51
本文介绍了使用 purrr::map() 更改和分配新变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始掌握编写函数并使用 lapply/purrr::map() 使我的代码更简洁的窍门,但显然还没有完全理解它.在我当前的示例中,我想重命名 lm_robust 对象的系数名称,然后更改 lm_robust 对象以合并新名称.我目前这样做:

I am just starting to get the hang of writing functions and using lapply / purrr::map() to make my code more concise, but clearly have not understood it completely yet. In my current example, I want to rename coefficient names of lm_robust objects and then change the lm_robust object to incorporate the new names. I currently do this:

library(dplyr)
library(purrr)
library(estimatr)

df <- tibble(interest = rnorm(1000), maturity = runif(1000, 1, 12), genderfemale = rbernoulli(1000),
            y = 0.5*interest + 2*maturity - 3*genderfemale + rnorm(1000, sd = 0.25))

model1 <- lm_robust(y ~ interest + maturity + genderfemale, data = df, se_type = "stata")
model2 <- lm_robust(y ~ interest + I(interest^2) + maturity + genderfemale, data = df, se_type = "stata")

rename_coefficients <- function(x) {
  x$term[which(x$term == "interest")] <- "Interest Rate"
  x$term[which(x$term == "I(interest^2)")] <- "Interest Squared"
  x$term[which(x$term == "maturity")] <- "Loan Maturity"
  x$term[which(x$term == "genderfemaleTRUE")] <- "Female Borrower"

  return(x$term)
}

temp <- map(list(model1, model2), rename_coefficients)
model1$term <- temp[[1]]
model2$term <- temp[[2]]

这是可行的,但在我的用例中,我有更多的模型,首先将 map() 的结果分配给 temp 然后包含部分 model1$每个模型的术语 <- temp[[1]].

This works, but in my use-case I have many more models and it bothers me to first assign the result of the map() to temp and then include the part model1$term <- temp[[1]] for each model.

必须有更有效的方法来做到这一点吗?

There must be a more efficient way to do this?

推荐答案

我们可以把这两个步骤结合起来做

We can combine the two steps by doing

purrr::map(list(model1, model2), ~{.x$term <- rename_coefficients(.x);.x})

#[[1]]
#                Estimate Std. Error   t value Pr(>|t|) CI Lower CI Upper  DF
#(Intercept)     -0.01957   0.020690   -0.9457   0.3445 -0.06017  0.02104 996
#Interest Rate    0.50310   0.008145   61.7719   0.0000  0.48712  0.51909 996
#Loan Maturity    2.00225   0.002563  781.3051   0.0000  1.99722  2.00728 996
#Female Borrower -2.97232   0.015790 -188.2375   0.0000 -3.00331 -2.94134 996

#[[2]]
#                  Estimate Std. Error   t value Pr(>|t|) CI Lower  CI Upper  DF
#(Intercept)      -0.016819   0.021597   -0.7787   0.4363 -0.05920  0.025563 995
#Interest Rate     0.502921   0.008105   62.0532   0.0000  0.48702  0.518825 995
#Interest Squared -0.002588   0.005618   -0.4606   0.6452 -0.01361  0.008436 995
#Loan Maturity     2.002219   0.002568  779.8058   0.0000  1.99718  2.007257 995
#Female Borrower  -2.972270   0.015799 -188.1354   0.0000 -3.00327 -2.941268 995

这将返回一个模型列表,其中 term 已更改.

This would return you a list of models backs with term changed.

或者同样使用 lapply

lapply(list(model1, model2), function(x) {x$term <- rename_coefficients(x);x})

这篇关于使用 purrr::map() 更改和分配新变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆