I'm trying to convert a list of data frames from a ggeffects object into one data frame, so I can use it better in ggplot2. This is an simple example of what I'm trying:
library(ggeffects)
library(dplyr)
data(efc)
fit <- lm(barthtot ~ c12hour neg_c_7 c161sex c172code, data = efc)
full <- ggpredict(fit)
df <- bind_rows(full, .id="id")
But this gives me the following error:
Error: Can't recycle c12hour
(size 35) to match neg_c_7
(size 12).
I'm new to R and Stackoverflow, so I hope this is all clear. Thank you!
CodePudding user response:
I don't fully understand your goal, but one way of binding the data frames in a list is using do.call(bind_rows, thelist)
:
do.call(bind_rows, full)
# Predicted values of Total score BARTHEL INDEX
# c12hour
c12hour | Predicted | 95% CI
------------------------------------
0 | 75.44 | [73.26, 77.63]
35 | 66.58 | [64.91, 68.25]
70 | 57.71 | [55.81, 59.61]
100 | 50.11 | [47.55, 52.68]
170 | 32.38 | [27.73, 37.03]
# c161sex
c12hour | Predicted | 95% CI
------------------------------------
1 | 63.96 | [60.57, 67.35]
2 | 65.00 | [63.11, 66.90]
# c172code
c12hour | Predicted | 95% CI
------------------------------------
1 | 64.06 | [61.01, 67.10]
2 | 64.78 | [63.12, 66.43]
3 | 65.49 | [62.32, 68.67]
# neg_c_7
c12hour | Predicted | 95% CI
------------------------------------
6 | 78.17 | [75.11, 81.22]
10 | 68.98 | [67.14, 70.81]
14 | 59.79 | [57.88, 61.69]
20 | 46.00 | [42.04, 49.97]
28 | 27.63 | [20.31, 34.95]
Adjusted for:
* neg_c_7 = 11.84
* c161sex = 1.76
* c172code = 1.97
However, this form does not show all the data and the columns. To show all of them, you can use as.data.frame()
or as_tibble()
.
do.call(bind_rows, full) |> as_tibble()
# A tibble: 52 × 6
x predicted std.error conf.low conf.high group
<dbl> <dbl> <dbl> <dbl> <dbl> <chr>
1 0 75.4 1.12 73.3 77.6 c12hour
2 5 74.2 1.06 72.1 76.3 c12hour
3 10 72.9 1.01 70.9 74.9 c12hour
4 15 71.6 0.965 69.8 73.5 c12hour
5 20 70.4 0.925 68.6 72.2 c12hour
6 25 69.1 0.893 67.4 70.9 c12hour
7 30 67.8 0.868 66.1 69.5 c12hour
8 35 66.6 0.851 64.9 68.2 c12hour
9 40 65.3 0.842 63.7 67.0 c12hour
10 45 64.0 0.843 62.4 65.7 c12hour
# … with 42 more rows
This can then be used to create a plot by using ggplot. For example:
do.call(bind_rows, full) |>
ggplot(aes(x =x, y = predicted, col = group))
geom_point()
The resulted plot: