Reputation: 237
In my analysis code I've been using broom::tidy
to extract a regression slope per group in a data frame:
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')
I had an issue with R so I reinstalled R, Rstudio and all my packages, and I guess on the way I've upgraded to a new version of broom. So I'm now getting the following error message:
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
Any idea what's happening here, and how to fix it? Thanks!
Upvotes: 2
Views: 1197
Reputation: 3173
An alternate to tidyr
using base
package,
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)
Upvotes: 3
Reputation: 21938
You could modify your code in the following way:
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
Upvotes: 1