替代broom::tidy?
alternative to broom::tidy?
在我的分析代码中,我一直在使用 broom::tidy
来提取数据框中每组的回归斜率:
E1.first_trial_df <- data.frame(
search_type = factor(rep(1:3, each = 10)),
set_size = rep(1:10,3),
RT = runif(300, min = 0, max = 2500)
)
E1.search_slopes_first_trial <- E1.first_trial_df %>%
group_by(search_type) %>%
do(model=lm(RT~set_size,data=.)) %>%
broom::tidy(model) %>%
filter(term=='set_size')
我遇到了 R 的问题,所以我重新安装了 R、Rstudio 和我的所有软件包,我想我已经升级到新版本的 broom。所以我现在收到以下错误消息:
Error in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) :
Calling var(x) on a factor x is defunct.
Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.
In addition: Warning messages:
1: Data frame tidiers are deprecated and will be removed in an upcoming release of broom.
2: In mean.default(X[[i]], ...) :
argument is not numeric or logical: returning NA
3: In mean.default(X[[i]], ...) :
argument is not numeric or logical: returning NA
知道这里发生了什么吗?如何解决?
谢谢!
您可以通过以下方式修改您的代码:
library(tidyverse)
library(broom)
E1.first_trial_df %>%
group_by(search_type) %>%
nest() %>%
mutate(model = map(data, ~ lm(RT ~ set_size, data = .x)),
tidy = map(model, ~ tidy(.x))) %>%
unnest(tidy) %>%
filter(term == 'set_size') %>%
select(!c(data, model))
# A tibble: 3 x 6
# Groups: search_type [3]
search_type term estimate std.error statistic p.value
<fct> <chr> <dbl> <dbl> <dbl> <dbl>
1 1 set_size -0.898 23.9 -0.0376 0.970
2 2 set_size -27.0 24.4 -1.11 0.271
3 3 set_size -2.00 26.0 -0.0768 0.939
tidyr
的替代品,使用 base
包,
df = E1.first_trial_df
df$search_type = as.factor(df$search_type)
#Splitting data frame
df = split(df, df$search_type)
#Using `lm` to fit the model
lmr = lapply(df, function(x){
lmr = lm(x$RT ~ x$set_size, x)
})
#Extract regression co-efficient for each model
rslope = lapply(lmr, function(x) x[["coefficients"]][["(Intercept)"]])
rslope = unlist(rslope)
#Extract intercept for each model
intercept = lapply(lmr, function(x) x[["coefficients"]][["x$set_size"]])
intercept = unlist(intercept)
在我的分析代码中,我一直在使用 broom::tidy
来提取数据框中每组的回归斜率:
E1.first_trial_df <- data.frame(
search_type = factor(rep(1:3, each = 10)),
set_size = rep(1:10,3),
RT = runif(300, min = 0, max = 2500)
)
E1.search_slopes_first_trial <- E1.first_trial_df %>%
group_by(search_type) %>%
do(model=lm(RT~set_size,data=.)) %>%
broom::tidy(model) %>%
filter(term=='set_size')
我遇到了 R 的问题,所以我重新安装了 R、Rstudio 和我的所有软件包,我想我已经升级到新版本的 broom。所以我现在收到以下错误消息:
Error in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) : Calling var(x) on a factor x is defunct. Use something like 'all(duplicated(x)[-1L])' to test for a constant vector. In addition: Warning messages: 1: Data frame tidiers are deprecated and will be removed in an upcoming release of broom. 2: In mean.default(X[[i]], ...) : argument is not numeric or logical: returning NA 3: In mean.default(X[[i]], ...) : argument is not numeric or logical: returning NA
知道这里发生了什么吗?如何解决? 谢谢!
您可以通过以下方式修改您的代码:
library(tidyverse)
library(broom)
E1.first_trial_df %>%
group_by(search_type) %>%
nest() %>%
mutate(model = map(data, ~ lm(RT ~ set_size, data = .x)),
tidy = map(model, ~ tidy(.x))) %>%
unnest(tidy) %>%
filter(term == 'set_size') %>%
select(!c(data, model))
# A tibble: 3 x 6
# Groups: search_type [3]
search_type term estimate std.error statistic p.value
<fct> <chr> <dbl> <dbl> <dbl> <dbl>
1 1 set_size -0.898 23.9 -0.0376 0.970
2 2 set_size -27.0 24.4 -1.11 0.271
3 3 set_size -2.00 26.0 -0.0768 0.939
tidyr
的替代品,使用 base
包,
df = E1.first_trial_df
df$search_type = as.factor(df$search_type)
#Splitting data frame
df = split(df, df$search_type)
#Using `lm` to fit the model
lmr = lapply(df, function(x){
lmr = lm(x$RT ~ x$set_size, x)
})
#Extract regression co-efficient for each model
rslope = lapply(lmr, function(x) x[["coefficients"]][["(Intercept)"]])
rslope = unlist(rslope)
#Extract intercept for each model
intercept = lapply(lmr, function(x) x[["coefficients"]][["x$set_size"]])
intercept = unlist(intercept)