Marginaleffects - obtaining contrasts and plotting predictions - r

Using marginaleffects, I was trying to
obtain contrasts by "period"
visualize the predictions by "period"
visualize the predictions by "session"
And failed in all! Any help is appreciated.
Df in the end
library(lme4)
library(lmerTest)
library(marginaleffects)
library(dplyr)
import dat_long
dat_long$group <- as.factor(dat_long$group)
dat_long$period <- as.factor(dat_long$period)
dat_long <- dat_long %>%
mutate(group2 = group)
m222 <- lmer(money ~ session + period + group2 + (1 | id2) + (1 | session / date / period), data = dat_long )
summary(m222)
contrasts_periods <- comparisons(
m222,
variables = "period",
include_random = FALSE,
newdata = datagrid(
period = c("p1", "p2", "p3", "p4")
)
)
#2
pred_period <- predictions( m222,
newdata = datagrid(id2 = NA,
period = c("p1", "p2", "p3", "p4"),
include_random = FALSE))
ggplot(pred, aes(x = period, y = predicted,
ymin = conf.low, ymax = conf.high))
#3
pred_session <- predictions( m222,
newdata = datagrid(id2 = NA,
session = seq(from = 16, to = 38, by = 1),
include_random = FALSE))
error codes:
Error: Unable to compute predicted values with this model. You can
try to supply a different dataset to the newdata argument. If this
does not work, you can file a report on the Github Issue Tracker:
https://github.com/vincentarelbundock/marginaleffects/issues
Error: Unable to compute predicted values with this model. You can try
to supply a different dataset to the newdata argument. If this does
not work, you can file a report on the Github Issue Tracker:
https://github.com/vincentarelbundock/marginaleffects/issues
This error was also raised: Invalid grouping factor specification,
id2 In addition: Warning message: Some of the variable names are
missing from the model data: include_random
df below:
dat_long <- structure(list(money = c(22625, 23349, 18189, 16302, 12874, 17343,
15912, 15300, 18762, 23506, 18290, 10296, 13172, 15288, 12462,
16380, 14352, 15052, 14497, 16241, 14832, 14304, 15120, 3745,
15012, 13916, 13056, 12432, 12441, 15762, 10660, 18150, 15496,
16905, 14872, 16166, 15892, 18755, 16241, 16874, 15836, 15225,
32190, 30450, 25200, 19840, 31800, 29892, 10416, 26520, 29029,
28623, 26544, 16988, 22801, 19317, 30694, 20447, 26030, 22378,
27267, 21760, 26334, 26896, 32085, 28914, 26892, 18683, 19468,
16920, 17640, 20829, 17920, 17424, 20538, 21760, 14985, 13407,
13624, 15470, 21252, 15129, 21336, 17760, 22908, 16940, 15860,
17732, 18048, 16002, 18480, 20328, 22848, 19630, 17030, 24220,
16074, 20234, 20413, 20448, 23715, 22010, 24000, 25245, 23088,
16445, 22200, 24786, 20100, 17766, 20022, 22194, 16284, 23560,
16638, 23345, 26788, 21462, 16786, 16362, 22176, 21600, 21744,
21432, 19026, 22330, 20049, 19968, 18876, 20850, 19126, 18788,
19650, 24320, 17100, 22785, 18875, 23520, 21252, 17766, 20304,
19170, 17780, 19296, 15855, 16244, 19875, 18476, 16284, 17780,
14279, 20562, 17556, 17568, 20700, 19750, 22401, 19625, 20264,
18176, 19272, 24180, 21855, 22490, 22560, 19599, 20550, 17856,
20670, 18768, 20385, 17856, 16891, 18081, 18755, 18796, 21450,
18576, 16263, 18460, 16616, 16992, 17250, 18995, 21021, 20368,
17536, 18626, 11742, 15872, 19684, 17250, 15616, 17176, 17653,
17690, 19890, 18054, 17760, 17346, 17316, 17316, 16610, 15428,
19950, 17424, 18720, 18029, 20724, 21574, 21632, 23584, 22059,
17741, 19328, 21120, 18029, 20295, 21679, 19803, 16157, 20250,
21870, 15052, 19782, 21528, 22275, 21285, 17787, 19635, 20768,
19965, 19203, 21666, 23472, 22270, 21528, 14900, 14070, 15120,
18306, 15707, 17810, 18630, 13552, 20691, 18375, 21376, 17732,
16512, 16896, 22410, 22022, 27512, 18796, 26274, 19877, 24462,
29722, 21823, 18834, 23856, 22491, 23055, 26568, 19096, 20944,
21320, 21140, 20124, 17415, 15776, 20034, 20698, 19723, 19845,
22139, 17272, 18720, 23616, 18144, 21312, 20150, 13560, 13560,
15470, 19458, 18944, 19044, 16129, 18354, 23400, 20155, 18161,
19881, 20002, 21060, 20436, 16637, 16968, 15656, 12870, 17767,
17160, 17549, 15696, 18860, 22116, 14602, 20648, 20680, 17549,
19184, 21756, 23718, 24742, 22848, 18511, 23230, 22987, 25480,
26064, 18300, 18161, 18300, 17628, 18720, 24072, 23760, 21672,
20060, 20280, 20482, 18620, 20160, 16764, 15990, 19328, 18125,
18864, 12870, 13899, 16254, 16891, 14742, 16482, 16520, 14278,
16074, 16610, 14848, 16002, 16675, 18850, 14964, 15738, 13254,
18720, 17135, 21352, 17040, 14784, 20592, 19044, 20770, 18560,
13800, 12996, 16256, 18476, 20572, 20445, 16576, 14319, 17408,
16128, 16124, 16065, 14756, 12432, 15029, 21352, 17810, 19932,
18495, 14720, 22914, 17063, 15645, 20735, 22960, 22925, 19845,
15708, 21942, 27531, 20850, 22475, 22484, 22140, 15260, 21106,
19817, 15360, 18480, 14586, 20433, 20838, 23881, 21679, 17612,
19952, 17856, 23560, 19311, 19728, 18850, 18560, 20139, 15840,
14824, 11210, 19728, 14784, 15065, 22638, 18216, 26219, 22797,
37047, 20687, 22176, 19519, 18492, 13516, 18327, 15616, 16616,
24928, 19840, 20838, 18460, 19176, 17825, 16950, 16786, 23254,
20655, 19352, 22632, 19684, 15312, 16770, 17010, 16464, 17135,
16568, 15494, 18327, 17136, 19221, 16166, 18944, 16541, 15622,
13746, 19720, 16640, 16303, 17690, 15132, 14400, 14060, 14835,
13320, 14322, 13860, 14796, 14946, 14790, 12600, 19460, 16940,
15708, 18176, 16080, 18161, 14260, 18358, 14632, 16482, 19964,
22218, 20139, 19040, 15368, 19880, 17286, 17388, 20424, 17400,
16445, 18760, 16958, 13334, 10608, 5940, 23068, 20300, 20944,
22046, 23256, 19418, 19456, 20328, 20536, 17343, 18161, 18070,
22632, 21624, 22620, 23850, 21840, 20174, 18250, 20172, 15260,
18120, 15038, 18445, 15048, 19456, 20328, 20536, 17343, 15594,
14124, 12862, 16899, 17160, 15080, 12971, 16430, 13356, 12947,
14430, 16764, 17136, 21965, 15729, 18000, 14304, 14214, 19470,
17380, 14688, 17666, 16470, 17334, 14976, 14688, 26196, 25993,
22704, 24639, 19352, 22188, 21924, 18271, 20240, 14874, 16320,
13923, 26989, 24464, 24830, 20320, 21195, 20212, 17168, 18612,
19454, 15510, 25277, 19872, 21033, 20808, 20824, 23250, 16002,
18492, 18900, 20413, 17446, 20124, 24257, 19557, 23919, 25610,
17490, 16157, 17545, 18370, 21357, 23058, 21888, 18796, 22388,
18200, 19140, 21808, 19844, 19530, 21879, 22880, 19239, 22230,
23166, 18871, 17480, 17199, 16874, 24206, 13224, 16905, 11322,
17880, 19536, 13910, 15972, 17145, 16750, 16226, 15972, 14875,
15128, 14756, 15972, 14875, 15128, 14756, 17112, 21312, 19126,
17556, 20276, 17100, 18972, 15429, 17820, 17024, 16641, 17135,
16714, 17324, 14125, 13080, 13189, 16000, 15006, 16740, 16092,
17908, 14626, 14859, 15744, 15113, 17292, 13804, 14541, 15113,
17292, 13804, 14541, 22308, 20736, 17958, 14976, 15410, 14762,
14803, 18900, 17792, 19448, 16872, 17013, 14040, 14840, 16520,
14224, 17908, 13462, 16874, 16675, 14715, 16263, 16440, 14317,
17082, 13764, 17052, 18120, 17442, 12838, 2322, 14637, 14934,
20066, 19520, 17880, 19304, 16422, 17526, 21420, 16254, 18090,
14803, 14874, 16320, 13923, 19328, 20592, 17152, 21573, 19716,
19800, 17792, 16896, 15128, 16899, 16510, 16375, 15488, 16974,
15151, 17980, 15946, 20856, 21158, 17493, 19539, 19488, 21060,
19840, 18352, 16974, 20034, 18997, 16758, 16046, 20034, 18997,
16758, 16046, 18600, 19939, 18603, 15184, 20829, 19096, 19630,
15012, 21384, 16750, 18029, 15410, 18724, 14580, 13420, 17664,
16206, 1595, 16675, 17822, 16348, 19304, 17136, 17136, 15812,
12648, 20961, 18544, 11748, 14763, 16758, 16046, 11748, 14763,
11660, 17589, 19200, 19588, 20727, 10725, 13870, 17374, 12354,
16214, 22101, 21080, 21525, 20125, 19434, 19800, 20125, 19434,
19800, 21054, 19800, 24250, 20196, 21175, 24790, 18318, 24024,
25004, 20083, 18144, 23976, 26660, 18688, 23520, 20304, 19832,
19360, 18228, 18921, 20800, 21038, 19873, 22468, 17666, 13635
), session = c(34, 34, 34, 17, 17, 19, 19, 19, 21, 21, 21, 24,
24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 33,
33, 33, 33, 35, 35, 35, 35, 36, 36, 36, 36, 37, 37, 37, 18, 19,
19, 19, 21, 21, 21, 23, 24, 24, 24, 24, 26, 26, 27, 27, 27, 34,
34, 34, 36, 36, 38, 38, 38, 17, 17, 17, 18, 18, 18, 21, 21, 21,
22, 22, 22, 23, 23, 23, 25, 25, 25, 25, 26, 26, 26, 27, 27, 27,
37, 37, 37, 38, 38, 38, 38, 16, 16, 16, 18, 18, 18, 19, 19, 19,
21, 21, 21, 23, 23, 23, 23, 25, 25, 26, 26, 26, 26, 32, 32, 32,
32, 33, 33, 33, 34, 34, 34, 35, 35, 35, 35, 36, 36, 36, 36, 16,
16, 16, 17, 17, 17, 21, 21, 21, 21, 23, 23, 25, 25, 25, 26, 26,
32, 32, 32, 32, 33, 33, 33, 34, 34, 34, 34, 35, 35, 38, 38, 38,
17, 17, 17, 18, 18, 21, 21, 23, 23, 23, 23, 25, 25, 25, 26, 26,
26, 26, 32, 32, 32, 34, 34, 34, 35, 35, 35, 35, 16, 19, 19, 16,
16, 16, 17, 17, 19, 19, 21, 21, 21, 21, 22, 22, 22, 27, 27, 27,
27, 32, 32, 32, 32, 33, 33, 33, 33, 35, 35, 35, 35, 36, 36, 36,
36, 16, 16, 16, 19, 19, 19, 20, 20, 21, 21, 21, 22, 22, 22, 23,
23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 27, 32,
32, 33, 33, 33, 34, 34, 35, 35, 35, 19, 19, 19, 23, 23, 23, 24,
24, 24, 24, 25, 25, 25, 27, 27, 27, 27, 33, 33, 33, 37, 37, 23,
23, 23, 38, 38, 16, 16, 16, 17, 17, 17, 18, 18, 18, 21, 21, 21,
22, 22, 23, 23, 23, 24, 16, 18, 19, 19, 19, 20, 20, 20, 22, 23,
24, 16, 16, 16, 18, 18, 18, 19, 19, 19, 22, 22, 22, 23, 23, 23,
23, 24, 24, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27,
32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36,
38, 38, 38, 17, 17, 17, 18, 18, 18, 20, 20, 20, 21, 21, 21, 22,
22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 35, 35, 35, 36, 36, 36,
36, 37, 37, 16, 17, 17, 17, 18, 18, 18, 19, 19, 21, 23, 23, 23,
25, 25, 25, 25, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 35, 35,
35, 35, 36, 36, 36, 36, 38, 38, 38, 38, 16, 16, 16, 19, 19, 19,
20, 20, 20, 21, 21, 21, 22, 22, 22, 23, 23, 23, 23, 25, 25, 35,
35, 36, 36, 36, 36, 37, 37, 37, 23, 16, 16, 16, 19, 19, 19, 21,
21, 21, 21, 31, 31, 31, 32, 32, 32, 32, 34, 34, 36, 36, 36, 16,
16, 22, 22, 22, 22, 24, 24, 24, 24, 28, 28, 28, 34, 34, 34, 37,
37, 37, 16, 16, 16, 21, 21, 21, 25, 25, 25, 25, 30, 30, 30, 30,
31, 31, 31, 32, 32, 32, 36, 36, 36, 16, 16, 16, 20, 20, 34, 34,
34, 35, 35, 17, 17, 17, 18, 21, 21, 21, 23, 23, 23, 23, 26, 26,
26, 26, 27, 27, 29, 29, 29, 30, 30, 30, 30, 32, 32, 32, 33, 34,
34, 34, 35, 35, 36, 36, 36, 17, 17, 17, 19, 19, 23, 23, 23, 26,
26, 27, 27, 27, 29, 30, 30, 31, 31, 31, 32, 32, 32, 32, 34, 34,
34, 35, 37, 37, 17, 17, 17, 21, 21, 21, 21, 23, 23, 23, 24, 24,
24, 24, 25, 25, 25, 25, 28, 28, 28, 28, 29, 29, 29, 29, 30, 30,
30, 30, 31, 31, 31, 31, 33, 33, 33, 35, 35, 17, 17, 17, 18, 24,
24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 33, 33, 33, 36, 18,
18, 18, 20, 20, 20, 26, 26, 27, 27, 27, 28, 28, 28, 28, 31, 31,
31, 33, 33, 33, 37, 37, 37, 18, 18, 18, 21, 21, 21, 21, 22, 22,
22, 26, 26, 26, 26, 27, 27, 27, 29, 29, 29, 29, 30, 30, 30, 30,
31, 31, 31, 31, 33, 33, 18, 18, 18, 19, 19, 19, 22, 22, 22, 24,
24, 24, 24, 25, 25, 25, 25, 27, 27, 27, 27, 29, 29, 29, 29, 30,
30, 30, 30, 32, 32, 32, 32, 34, 34, 34, 35, 35, 37, 37, 37, 22,
22, 22, 22, 24, 24, 24, 24, 25, 25, 25, 25, 28, 28, 28, 28, 35,
37, 37, 37, 19, 22, 22, 24, 24, 24, 25, 25, 25, 26, 26, 28, 28,
28, 32, 32, 32, 33, 35, 36, 20, 20, 20, 23, 23, 23, 27, 27, 29,
29, 29, 36, 36, 20, 28), period = structure(c(1L, 2L, 4L, 1L,
2L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 1L, 1L, 2L, 3L, 2L, 3L, 4L, 4L, 1L, 2L,
3L, 4L, 3L, 4L, 1L, 3L, 4L, 1L, 2L, 3L, 2L, 4L, 1L, 2L, 4L, 1L,
2L, 3L, 1L, 2L, 3L, 2L, 3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L, 4L, 1L, 2L, 4L, 2L, 3L, 4L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 4L, 2L,
3L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 2L, 3L, 4L, 1L, 2L, 3L, 1L,
2L, 3L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 1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"int"), class = "factor"), id2 = c(1,
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11,
11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 14,
14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14,
14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
18, 18, 18, 18, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22,
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 23, 23, 23,
23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23,
23, 23, 23, 23, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,
25, 25, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 29, 29, 29, 29, 29, 29,
29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29,
29, 29, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
30, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 32, 32, 32, 32, 32, 32,
32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 33, 33,
33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33,
33, 33, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 35,
35), date = c(34, 34, 34, 17, 17, 19, 19, 19, 21, 21, 21, 24,
24, 24, 24, 25, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27, 33,
33, 33, 33, 35, 35, 35, 35, 36, 36, 36, 36, 37, 37, 37, 18, 19,
19, 19, 21, 21, 21, 23, 24, 24, 24, 24, 26, 26, 27, 27, 27, 34,
34, 34, 36, 36, 38, 38, 38, 17, 17, 17, 18, 18, 18, 21, 21, 21,
22, 22, 22, 23, 23, 23, 25, 25, 25, 25, 26, 26, 26, 27, 27, 27,
37, 37, 37, 38, 38, 38, 38, 16, 16, 16, 18, 18, 18, 19, 19, 19,
21, 21, 21, 23, 23, 23, 23, 25, 25, 26, 26, 26, 26, 32, 32, 32,
32, 33, 33, 33, 34, 34, 34, 35, 35, 35, 35, 36, 36, 36, 36, 16,
16, 16, 17, 17, 17, 21, 21, 21, 21, 23, 23, 25, 25, 25, 26, 26,
32, 32, 32, 32, 33, 33, 33, 34, 34, 34, 34, 35, 35, 38, 38, 38,
17, 17, 17, 18, 18, 21, 21, 23, 23, 23, 23, 25, 25, 25, 26, 26,
26, 26, 32, 32, 32, 34, 34, 34, 35, 35, 35, 35, 16, 19, 19, 16,
16, 16, 17, 17, 19, 19, 21, 21, 21, 21, 22, 22, 22, 27, 27, 27,
27, 32, 32, 32, 32, 33, 33, 33, 33, 35, 35, 35, 35, 36, 36, 36,
36, 16, 16, 16, 19, 19, 19, 20, 20, 21, 21, 21, 22, 22, 22, 23,
23, 23, 24, 24, 24, 25, 25, 25, 26, 26, 26, 27, 27, 27, 27, 32,
32, 33, 33, 33, 34, 34, 35, 35, 35, 19, 19, 19, 23, 23, 23, 24,
24, 24, 24, 25, 25, 25, 27, 27, 27, 27, 33, 33, 33, 37, 37, 23,
23, 23, 38, 38, 16, 16, 16, 17, 17, 17, 18, 18, 18, 21, 21, 21,
22, 22, 23, 23, 23, 24, 16, 18, 19, 19, 19, 20, 20, 20, 22, 23,
24, 16, 16, 16, 18, 18, 18, 19, 19, 19, 22, 22, 22, 23, 23, 23,
23, 24, 24, 24, 24, 25, 25, 25, 26, 26, 26, 26, 27, 27, 27, 27,
32, 32, 32, 32, 33, 33, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36,
38, 38, 38, 17, 17, 17, 18, 18, 18, 20, 20, 20, 21, 21, 21, 22,
22, 22, 23, 23, 23, 23, 24, 24, 24, 24, 35, 35, 35, 36, 36, 36,
36, 37, 37, 16, 17, 17, 17, 18, 18, 18, 19, 19, 21, 23, 23, 23,
25, 25, 25, 25, 32, 32, 32, 33, 33, 33, 33, 34, 34, 34, 35, 35,
35, 35, 36, 36, 36, 36, 38, 38, 38, 38, 16, 16, 16, 19, 19, 19,
20, 20, 20, 21, 21, 21, 22, 22, 22, 23, 23, 23, 23, 25, 25, 35,
35, 36, 36, 36, 36, 37, 37, 37, 23, 32, 32, 32, 38, 38, 38, 42,
42, 42, 42, 62, 62, 62, 64, 64, 64, 64, 68, 68, 72, 72, 72, 32,
32, 44, 44, 44, 44, 48, 48, 48, 48, 56, 56, 56, 68, 68, 68, 74,
74, 74, 32, 32, 32, 42, 42, 42, 50, 50, 50, 50, 60, 60, 60, 60,
62, 62, 62, 64, 64, 64, 72, 72, 72, 32, 32, 32, 40, 40, 68, 68,
68, 70, 70, 34, 34, 34, 36, 42, 42, 42, 46, 46, 46, 46, 52, 52,
52, 52, 54, 54, 58, 58, 58, 60, 60, 60, 60, 64, 64, 64, 66, 68,
68, 68, 70, 70, 72, 72, 72, 34, 34, 34, 38, 38, 46, 46, 46, 52,
52, 54, 54, 54, 58, 60, 60, 62, 62, 62, 64, 64, 64, 64, 68, 68,
68, 70, 74, 74, 34, 34, 34, 42, 42, 42, 42, 46, 46, 46, 48, 48,
48, 48, 50, 50, 50, 50, 56, 56, 56, 56, 58, 58, 58, 58, 60, 60,
60, 60, 62, 62, 62, 62, 66, 66, 66, 70, 70, 34, 34, 34, 36, 48,
48, 48, 48, 50, 50, 50, 50, 52, 52, 52, 52, 66, 66, 66, 72, 36,
36, 36, 40, 40, 40, 52, 52, 54, 54, 54, 56, 56, 56, 56, 62, 62,
62, 66, 66, 66, 74, 74, 74, 36, 36, 36, 42, 42, 42, 42, 44, 44,
44, 52, 52, 52, 52, 54, 54, 54, 58, 58, 58, 58, 60, 60, 60, 60,
62, 62, 62, 62, 66, 66, 36, 36, 36, 38, 38, 38, 44, 44, 44, 48,
48, 48, 48, 50, 50, 50, 50, 54, 54, 54, 54, 58, 58, 58, 58, 60,
60, 60, 60, 64, 64, 64, 64, 68, 68, 68, 70, 70, 74, 74, 74, 44,
44, 44, 44, 48, 48, 48, 48, 50, 50, 50, 50, 56, 56, 56, 56, 70,
74, 74, 74, 38, 44, 44, 48, 48, 48, 50, 50, 50, 52, 52, 56, 56,
56, 64, 64, 64, 66, 70, 72, 40, 40, 40, 46, 46, 46, 54, 54, 58,
58, 58, 72, 72, 40, 56)), row.names = c(NA, -834L), class = c("tbl_df",
"tbl", "data.frame"))

This answer uses the development version (0.9.0.9043) of marginaleffects, which you can install by following the instructions here: https://vincentarelbundock.github.io/marginaleffects/
Please note that the extra lme4-related arguments must be supplied to the predictions() function, and not to the datagrid() function as you do in your second example.
Also, I strongly suggest you avoid include_random and use the default arguments supplied by the lme4 modelling package itself (via predict.merMod). In this case: re.form and allow.new.levels.
library(lme4)
library(lmerTest)
library(marginaleffects)
library(dplyr)
dat_long$group <- as.factor(dat_long$group)
dat_long$period <- as.factor(dat_long$period)
dat_long <- dat_long %>% mutate(group2 = group)
m222 <- lmer(money ~ session + period + group2 + (1 | id2) + (1 | session / date / period), data = dat_long )
comparisons(
m222,
variables = "period",
re.form = NA,
newdata = datagrid(period = c("p1", "p2", "p3", "p4")))
#
# Term Contrast Estimate Std. Error z Pr(>|z|) 2.5 % 97.5 % session group2 id2 date
# period p2 - p1 -361.6 260.0 -1.391 0.1643688 -871.3 148.1 23 int 16 37.90168
# period p2 - p1 -361.6 260.0 -1.391 0.1643688 -871.3 148.1 23 int 16 37.90168
# period p2 - p1 -361.6 260.0 -1.391 0.1643688 -871.3 148.1 23 int 16 37.90168
# period p2 - p1 -361.6 260.0 -1.391 0.1643688 -871.3 148.1 23 int 16 37.90168
# period p3 - p1 -745.6 260.0 -2.868 0.0041366 -1255.3 -236.0 23 int 16 37.90168
# period p3 - p1 -745.6 260.0 -2.868 0.0041366 -1255.3 -236.0 23 int 16 37.90168
# period p3 - p1 -745.6 260.0 -2.868 0.0041366 -1255.3 -236.0 23 int 16 37.90168
# period p3 - p1 -745.6 260.0 -2.868 0.0041366 -1255.3 -236.0 23 int 16 37.90168
# period p4 - p1 -1371.6 318.4 -4.308 1.6492e-05 -1995.6 -747.5 23 int 16 37.90168
# period p4 - p1 -1371.6 318.4 -4.308 1.6492e-05 -1995.6 -747.5 23 int 16 37.90168
# period p4 - p1 -1371.6 318.4 -4.308 1.6492e-05 -1995.6 -747.5 23 int 16 37.90168
# period p4 - p1 -1371.6 318.4 -4.308 1.6492e-05 -1995.6 -747.5 23 int 16 37.90168
#
# Prediction type: response
# Columns: rowid, type, term, contrast, estimate, std.error, statistic, p.value, conf.low, conf.high, predicted, predicted_hi, predicted_lo, money, session, group2, id2, date, period
predictions(
m222,
newdata = datagrid(
id2 = NA,
session = seq(from = 16, to = 38, by = 1)),
re.form = NA,
allow.new.levels = TRUE)
#
# Estimate Std. Error z Pr(>|z|) 2.5 % 97.5 % period group2 date id2 session
# 19654 656.1 29.96 < 2.22e-16 18368 20940 p1 int 37.90168 NA 16
# 19649 647.4 30.35 < 2.22e-16 18380 20917 p1 int 37.90168 NA 17
# 19643 639.5 30.72 < 2.22e-16 18390 20896 p1 int 37.90168 NA 18
# 19637 632.5 31.05 < 2.22e-16 18398 20877 p1 int 37.90168 NA 19
# 19632 626.2 31.35 < 2.22e-16 18404 20859 p1 int 37.90168 NA 20
# 19626 620.9 31.61 < 2.22e-16 18409 20843 p1 int 37.90168 NA 21
# 19621 616.5 31.83 < 2.22e-16 18412 20829 p1 int 37.90168 NA 22
# 19615 613.0 32.00 < 2.22e-16 18414 20817 p1 int 37.90168 NA 23
# 19610 610.5 32.12 < 2.22e-16 18413 20806 p1 int 37.90168 NA 24
# 19604 608.9 32.20 < 2.22e-16 18411 20798 p1 int 37.90168 NA 25
# 19599 608.2 32.22 < 2.22e-16 18406 20791 p1 int 37.90168 NA 26
# 19593 608.6 32.19 < 2.22e-16 18400 20786 p1 int 37.90168 NA 27
# 19587 609.9 32.12 < 2.22e-16 18392 20783 p1 int 37.90168 NA 28
# 19582 612.1 31.99 < 2.22e-16 18382 20782 p1 int 37.90168 NA 29
# 19576 615.3 31.82 < 2.22e-16 18370 20782 p1 int 37.90168 NA 30
# 19571 619.4 31.60 < 2.22e-16 18357 20785 p1 int 37.90168 NA 31
# 19565 624.4 31.33 < 2.22e-16 18341 20789 p1 int 37.90168 NA 32
# 19560 630.4 31.03 < 2.22e-16 18324 20795 p1 int 37.90168 NA 33
# 19554 637.1 30.69 < 2.22e-16 18305 20803 p1 int 37.90168 NA 34
# 19549 644.8 30.32 < 2.22e-16 18285 20812 p1 int 37.90168 NA 35
# 19543 653.2 29.92 < 2.22e-16 18263 20823 p1 int 37.90168 NA 36
# 19538 662.4 29.49 < 2.22e-16 18239 20836 p1 int 37.90168 NA 37
# 19532 672.4 29.05 < 2.22e-16 18214 20850 p1 int 37.90168 NA 38
#
# Prediction type: response
# Columns: rowid, type, estimate, std.error, statistic, p.value, conf.low, conf.high, money, period, group2, date, id2, session

Related

Plotting multiple variables in time series with greyscale and shapes [duplicate]

This question already has answers here:
Changing the line type in the ggplot legend
(2 answers)
ggplot2 for grayscale printouts
(3 answers)
Closed 7 months ago.
I am trying to make a time-series graph with multiple y values. I would like to change the shape of the different variables so some are solid, some are dashed etc. I would also like all the colors to be on greyscale.
Does anyone know how I can accomplish this?
I know how to melt my data so that I can plot them all together by the value of the variables but right now I cannot get the shapes to change or the greyscale. Thank you in advance.
ggplot(melted_data, aes(x = Distance, y = value, color = variable)) + geom_line()
data <- structure(list(Distance = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91,
92, 93, 94, 95, 96, 97, 98, 99, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74,
75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
91, 92, 93, 94, 95, 96, 97, 98, 99, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99), variable = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L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, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Mg",
"Mn", "Zn", "Ba"), class = "factor"), value = c(0.903247645,
0.912560748, 0.896003508, 0.909572697, 0.883631829, 0.905722594,
0.892465355, 0.909271173, 0.880506202, 0.889278401, 0.878534542,
0.959209459, 0.913303825, 0.929893977, 0.97778374, 0.9885554,
0.929716333, 1.028422583, 1.025638955, 1.011352651, 1.041343955,
1.092562951, 1.129761801, 1.088857171, 1.107257284, 1.116728405,
1.103053734, 1.041662037, 1.134182243, 1.104550315, 1.086952767,
1.106004784, 1.057688595, 1.034347579, 1.04641385, 1.139270945,
1.048446018, 1.033827731, 1.075554754, 1.029893202, 1.074749532,
1.001626205, 0.977053541, 0.987467665, 0.999540478, 0.945184816,
0.959677178, 0.962807712, 0.967023936, 1.024286493, 0.881264816,
0.967181342, 1.000316876, 0.956168258, 1.003214572, 1.00047837,
0.940103474, 0.929875987, 0.928227112, 0.982410241, 0.983035162,
0.976666772, 1.019755049, 1.075189042, 0.975380543, 0.981316782,
0.986876269, 1.026690916, 1.052379934, 1.001547298, 0.979888683,
1.008209647, 0.976098272, 0.944479556, 0.996767684, 1.018077758,
1.028862706, 1.08510417, 1.08963868, 1.048481179, 1.139954126,
1.107066353, 1.122920581, 1.23904326, 1.19449336, 1.179971969,
1.165865352, 1.068804094, 1.099436469, 1.073307737, 1.07045113,
1.101007051, 1.011962649, 1.11202545, 1.097883672, 1.05361424,
0.993283703, 1.046635444, 1.04951188, 0.086720869, 0.113119382,
0.088197332, 0.081547788, 0.079373211, 0.07888827, 0.072865285,
0.079637996, 0.066314774, 0.097585729, 0.185034982, 0.214466904,
0.294317625, 0.481389256, 0.531196058, 0.715842439, 0.865098887,
0.987242052, 1.081028291, 1.240920518, 1.313524957, 1.543771699,
1.78495042, 1.746572555, 2.048760527, 2.101438775, 1.967474033,
2.000286925, 2.014020838, 1.924470659, 1.75696549, 1.786681246,
1.633290961, 1.455799758, 1.315346538, 1.435348984, 1.27887702,
1.152818928, 1.095127218, 0.987502349, 1.062278922, 0.898540082,
0.83617998, 0.889057689, 0.825563648, 0.788347646, 0.790973555,
0.775541228, 0.815063004, 0.848723108, 0.66783059, 0.672629631,
0.747809615, 0.72338158, 0.666220438, 0.664051795, 0.597260657,
0.689282162, 0.663808452, 0.678551141, 0.672917354, 0.686199986,
0.724202364, 0.746195474, 0.686135659, 0.654148537, 0.713488795,
0.72446665, 0.699529989, 0.630120423, 0.661767463, 0.663290351,
0.705879842, 0.709399338, 0.76228353, 0.714368918, 0.720561695,
0.837036666, 0.923882149, 1.014163852, 1.221410703, 1.315825246,
1.368054705, 1.641746627, 1.630198312, 1.698589629, 1.562956393,
1.427322658, 1.53964983, 1.574583495, 1.527101216, 1.380123116,
1.28649445, 1.29251968, 1.330565441, 1.317758525, 1.19292313,
1.217953538, 1.218591815, 0.746612627, 0.818368055, 0.696689824,
0.748702805, 0.717457681, 0.766243608, 0.805305259, 0.855909762,
0.803357905, 0.889646097, 0.854456208, 1.067795473, 1.051422575,
1.17061972, 1.138440648, 1.052796919, 1.040998633, 1.161739158,
1.025956799, 0.971567748, 1.072911493, 0.952121155, 1.040392714,
1.069745522, 1.068549198, 1.090194087, 1.214584829, 1.157485471,
1.245813376, 1.336359991, 1.204038397, 1.126255292, 1.131057736,
0.922042386, 1.037566449, 1.100852394, 1.121842367, 0.998657748,
1.006938923, 1.002800377, 0.897387497, 0.93902937, 0.889327622,
0.802133735, 0.855245047, 0.860702407, 0.704324249, 0.905827093,
0.760155095, 0.760247698, 0.655991619, 0.677006743, 0.668001976,
0.623410532, 0.569302474, 0.523713794, 0.690042836, 0.539115342,
0.528696218, 0.57851915, 0.60294784, 0.581392042, 0.65277069,
0.65620614, 0.625397246, 0.697647782, 0.6180657, 0.632326126,
0.684659215, 0.606197513, 0.630134281, 0.637151517, 0.574538208,
0.605993607, 0.533522181, 0.544522236, 0.577535469, 0.573427383,
0.672984155, 0.735286828, 0.7532343, 0.881292245, 0.801132661,
1.122761046, 1.137397845, 1.173190388, 1.138033979, 1.126494557,
1.144871399, 1.087042815, 0.981750792, 0.992888445, 0.955352455,
1.074357698, 1.027127808, 1.083248059, 1.010304962, 1.037776316,
1.052809984, 0.742734852, 0.839492568, 0.743899849, 0.817080816,
0.773569657, 0.735728339, 0.715168283, 0.78077814, 0.694280484,
0.773303425, 0.768041196, 0.883401699, 0.818274274, 0.715927964,
0.696938222, 0.832246446, 0.73089346, 0.790965216, 0.799717389,
0.865896893, 0.946771069, 0.954212275, 1.023740345, 1.027036123,
1.086336263, 1.064542815, 0.9463809, 0.924081609, 0.999832641,
0.911277648, 0.922871168, 0.953134033, 0.786732115, 0.802026729,
0.832863371, 0.863952475, 0.817833153, 0.748586924, 0.72095701,
0.738213943, 0.672736744, 0.704947698, 0.531743532, 0.634123809,
0.683548549, 0.733277161, 0.608993729, 0.752162246, 0.568705823,
0.643172511, 0.597251486, 0.655514695, 0.583437677, 0.557676441,
0.646713866, 0.527005047, 0.578023512, 0.576281064, 0.600923204,
0.578475648, 0.551957027, 0.585007991, 0.623858699, 0.630936819,
0.636198589, 0.565476603, 0.658861425, 0.577557604, 0.629178306,
0.646092809, 0.566079299, 0.60953767, 0.680135261, 0.500802233,
0.704656678, 0.61109605, 0.645344144, 0.667139888, 0.734969576,
0.780062983, 0.783090234, 0.83005691, 0.905356723, 0.933746319,
0.947613375, 0.923115827, 0.873482691, 0.746883952, 0.850273618,
0.795256154, 0.800825928, 0.772630039, 0.749567395, 0.7823457,
0.772609842, 0.736269985, 0.699705666, 0.716860238, 0.65909369
)), row.names = c(NA, -396L), class = "data.frame")
You can use the linetype parameter with the aestethics :
ggplot(data) +
geom_line(aes(x = Distance, y = value, color = variable, linetype = variable))

Removing outliers in various columns without creating NAs in the whole row

I am trying to remove the outliers from various variables at the same time in my dataset but with the function used it seems that when it finds one outlier it turns the whole row into NA.
That´s a problem because I have to apply the same process to a larger dataset and I am worried that it considerably reduces my sample...
So I would like to just turn the case where the outlier is into NA without turning the whole row into NA. Is that eventually possible?
Thank you for your input
#function used for outliers
outliers <- function(x) {
Q1 <- quantile(x, probs=.25, na.rm = TRUE)
Q3 <- quantile(x, probs=.75, na.rm = TRUE)
iqr = Q3-Q1
upper_limit = Q3 + (iqr*1.5)
lower_limit = Q1 - (iqr*1.5)
x > upper_limit | x < lower_limit
}
remove_outliers <- function(dflinear, cols = names(dflinear)) {
for (col in cols) {
dflinear <- dflinear[!outliers(dflinear[[col]]),]
}
dflinear
}
dflinear_without_outliers<-remove_outliers(dflinear, c("insuline", "glucose", "hdl","ldl"))
#Reproducible sample below
dflinear<- structure(list(id = structure(c("SA01", "SA02", "SA03", "SA04",
"SA05", "SA06", "SA07", "SA08", "SA09", "SA10", "SA11", "SA12",
"SA13", "SA14", "SA15", "SA16", "SA17", "SA18", "SA19", "SA20",
"SA21", "SA22", "SA23", "SA24", "SA25", "SA26", "SA27", "SA28",
"SA29", "SA30", "SA31", "SA32", "SA33", "SA34", "SA35", "SA36",
"SA37", "SA38", "SA39", "SA40", "SA41", "SA42", "SA43", "SA44",
"SA45", "SA46", "SA47", "SA48", "SA49", "SA50", "SA51", "SA52",
"SA53", "SA54", "SA56", "SA57", "SA58", "SA59", "SA60", "SA61",
"SA62", "SA63", "SA64", "SA65", "SA66", "SA67", "SA68", "SA69",
"SA72", "SA73", "SA74", "SA75", "SA76", "SA77", "SA78", "SA79",
"SA80", "SA81", "SA82", "SA83", "SA84", "SA85", "SA86", "SA87",
"SA88", "SA89", "SA90", "SA92", "SA93", "SA94", "SA95", "SA96",
"SA97", "SA99", "SA100", "SA101", "SA102", "SA103", "SA104",
"SA105", "SA107", "SA108", "SA109", "SA110", "SA111", "SA112",
"SA113", "SA114", "SA115", "SA116", "SA118", "SC01", "SC02",
"SC03", "SC04", "SC05", "SC06", "SC07", "SC08", "SC09", "SC10",
"SC11", "SC12", "SC13", "SC14", "SC15", "SC16", "SC17", "SC18",
"SC19", "SC20", "SC21", "SC22", "SC23", "SC24", "SC25", "SC26",
"SC27", "SC28", "SC29", "SC30", "SC31", "SC32", "SC33", "SC34",
"SC35", "SC36", "SC37", "SC38", "M01", "M02", "M03", "M04", "M05",
"M06", "M07", "M08", "M09", "M10", "M11", "M12", "M13", "M14",
"M15", "M16", "M17", "M18", "M19", "M20", "M21", "M22", "M23",
"M24", "M25", "M26", "M27", "M28", "M29", "M30", "M31", "M32",
"M33", "M34", "M35", "M36", "M37", "M38", "M39", "M40", "M41",
"M42", "M43", "M44", "M45", "M46", "M47", "M48", "M49", "M50",
"M51", "M52", "M53", "SA01", "SA02", "SA03", "SA04", "SA05",
"SA06", "SA07", "SA08", "SA09", "SA10", "SA11", "SA12", "SA13",
"SA14", "SA15", "SA16", "SA17", "SA18", "SA19", "SA20", "SA21",
"SA22", "SA23", "SA24", "SA25", "SA26", "SA27", "SA28", "SA29",
"SA30", "SA31", "SA32", "SA33", "SA34", "SA35", "SA36", "SA37",
"SA38", "SA39", "SA40", "SA41", "SA42", "SA43", "SA44", "SA45",
"SA46", "SA47", "SA48", "SA49", "SA50", "SA51", "SA52", "SA53",
"SA54", "SA56", "SA57", "SA58", "SA59", "SA60", "SA61", "SA62",
"SA63", "SA64", "SA65", "SA66", "SA67", "SA68", "SA69", "SA72",
"SA73", "SA74", "SA75", "SA76", "SA77", "SA78", "SA79", "SA80",
"SA81", "SA82", "SA83", "SA84", "SA85", "SA86", "SA87", "SA88",
"SA89", "SA90", "SA92", "SA93", "SA94", "SA95", "SA96", "SA97",
"SA99", "SA100", "SA101", "SA102", "SA103", "SA104", "SA105",
"SA107", "SA108", "SA109", "SA110", "SA111", "SA112", "SA113",
"SA114", "SA115", "SA116", "SA118", "SC01", "SC02", "SC03", "SC04",
"SC05", "SC06", "SC07", "SC08", "SC09", "SC10", "SC11", "SC12",
"SC13", "SC14", "SC15", "SC16", "SC17", "SC18", "SC19", "SC20",
"SC21", "SC22", "SC23", "SC24", "SC25", "SC26", "SC27", "SC28",
"SC29", "SC30", "SC31", "SC32", "SC33", "SC34", "SC35", "SC36",
"SC37", "SC38", "M01", "M02", "M03", "M04", "M05", "M06", "M07",
"M08", "M09", "M10", "M11", "M12", "M13", "M14", "M15", "M16",
"M17", "M18", "M19", "M20", "M21", "M22", "M23", "M24", "M25",
"M26", "M27", "M28", "M29", "M30", "M31", "M32", "M33", "M34",
"M35", "M36", "M37", "M38", "M39", "M40", "M41", "M42", "M43",
"M44", "M45", "M46", "M47", "M48", "M49", "M50", "M51", "M52",
"M53"), label = "Code of PrevenGo", format.spss = "A5", display_width = 12L),
group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), insuline = structure(c(9, 4.1, 3.3, 9.4, 22.9, 16.2,
8.7, 16.7, 21.2, 21, 12.8, 7.3, 38.4, 20.2, 19.6, 6.4, 18.9,
12.1, 8.2, 17, 15.6, 12.5, 19.1, 13.7, 8, 20.1, 19.8, 6.8,
15.4, 14.7, 11.9, 8.8, 7.9, 51.2, 10.8, 8.1, 28.6, 8.6, 27.9,
13.3, 9, 16.3, 13.3, 5.8, 27.3, 4.2, 8.2, 9.9, 20.1, 11.7,
8.7, 18.1, 10.9, 27.4, 14.6, 29.1, 10.2, 20.2, 9.7, 12.3,
18.2, 1.9, 11.6, 14.6, 7.9, 11.2, 13.8, 21.2, 23.8, 18, 23.5,
21.4, 11.4, 12, 6.6, 13.5, 10.4, 25.3, 56.8, 10.7, 21.5,
8.5, 30.2, 5.3, 7.5, 15.9, 11.6, 22.4, 25.2, 6.1, 15.1, 9.3,
24.3, 30.8, 8.9, 9.8, 34.1, 13.4, 23.1, 21.1, 4.8, 20.1,
38.5, 16.1, 34.1, 16.1, 17.7, 41.4, 20.4, 21.5, 36.3, 15.9,
8.8, 6.1, 29, 4, 23.1, 36.8, 16.4, 15.5, 28.8, 15.9, NA,
7.1, 6.1, 10, 9.1, 25.2, 19.1, 6.9, 14.7, 23.1, 19.3, 12.3,
7.3, 5.9, 8, 0.5, 9, 4, 10.4, 21.4, 14.6, 8.8, 24.5, 5.3,
9.8, 17.6, 10.2, 10.7, 23, 14.5, 4.6, 33.3, 23.3, 7.2, 3.7,
13.1, 6.7, 20, 7.5, 9.2, 4.5, 2.1, 7.7, 11.7, 7.6, 22.5,
8.8, 5.1, 14.8, 15.1, 18.8, 24.3, 14, 17.2, 16.2, 23.6, 17.4,
16.5, 12.1, 15.3, 11.4, 8.7, 22.6, 10.5, 7.4, 15.1, 13.1,
24.6, 19.3, 19.7, 14.1, 5.9, 19.7, 14.9, 5.9, 17.2, 16.9,
6.2, 11.2, 4.1, 10, 3.7, 3.6, 11.6, 16.9, NA, 8, 17.3, NA,
18.3, 4, 3.1, 26.4, 12.9, 17.9, 10.3, 22.5, NA, NA, 23.4,
15.1, NA, 11.9, 27, 6.2, NA, 21.5, 11.6, 15.8, 8.6, 15.2,
10.1, 20.6, 21.7, 45.3, 8.3, 19.5, 29.2, 21.5, 11.4, 9.5,
31.8, 35.3, 11.2, 15.4, NA, 8.5, 22.6, 14.3, NA, 11.8, 11.4,
4.2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 35.8, NA, NA,
NA, NA, NA, 19.7, 42.8, 30.6, 12.2, 5.2, 4.9, 20.4, NA, 23.5,
NA, 13.6, 19.4, 6.9, 16.7, 7.2, 14.7, 59.2, 22, 41.4, 18.1,
10.5, 19.8, 17.4, NA, 25.9, NA, 8.3, 25.9, 5.7, 17.1, 25.2,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 10.3, 9.1, 14.8,
13.7, 6.2, 17.9, 13.9, 14.6, 70.4, 23.6, 13.8, 15.2, 9.9,
14, 27.6, 14.3, 23.7, 11, 12.1, 13.5, 21, NA, 7.2, 12.3,
4.4, 6.2, 3.9, 15, 9.6, NA, 9, 10.3, NA, 13.3, 6, 11.3, 17.6,
8.5, 10, NA, 11.8, 10.4, 26.2, NA, 10, 5.7, 16.3, 4.7, 20.3,
7.7, 14.6, 9.4, 6.3, 10, 11.1, 6.7, 42.5, NA, NA, NA, 7.7,
18.6, NA, 16.7, 25.4, 21.8, 26.8, 10.2, 13.8, 11.6, 19.1,
8.3, 3.8, 31.1, NA, 7.1, 11.1, 8.7, 19, 16, 31.8, 11.7, 3.4,
17.6, 12.3, 5.1, 17.5, 6.7, 3.8, 16.6, 6.1), format.spss = "F4.2", display_width = 11L),
glucose = structure(c(90, 95, 79, 85, 95, 97, 86, 74, 88,
95, 94, 88, 86, 94, 86, 95, 97, 88, 88, 88, 83, 103, 79,
67, 88, 79, 90, 79, 97, 94, 85, 83, 88, 97, 81, 95, 92, 94,
99, 79, 83, 92, 81, 92, 79, 94, 83, 79, 81, 92, 86, 95, 92,
95, 92, 85, 94, 81, 86, 85, 99, 92, 85, 72, 86, 81, 79, 86,
97, 88, 92, 97, 83, 103, 97, 95, 85, 77, 77, 83, 99, 90,
77, 77, 83, 92, 88, 83, 88, 86, 88, 97, 101, 99, 88, 101,
94, 86, 85, 83, 86, 88, 92, 94, 94, 90, 160, 94, 83, 95,
97, 88, 88, 95, 90, 92, 113, 104, 85, 101, 91.8, 99, 94,
85, 85, 83, 86, 88, 95, 79, 101, 92, 83, 90, 85, 95, 88,
79, 90, 79, 94, 99, 83, 85, 85, 77, 99, 81, 92, 86.4, 95.4,
82.8, 73.8, 81, 90, 82.8, 79.2, 90, 82.8, 91.8, 90, 84.6,
84.6, 84.6, 77.4, 77.4, 75.6, 88.2, 79.2, 92, 90, 113, 81,
81, 81, 84.6, 88.2, 73.8, 81, 81, 82.8, 79.2, 70.2, 91.8,
97.2, 82.8, 70.2, 91.8, 93.6, 86.4, 93.6, 73.8, 95.4, 81,
97.2, 77.4, 90, 82.8, 86.4, 88.2, 88.2, 73.8, 90, 92, 83,
86, 99, NA, 86, 81, NA, 99, 83, 86, 76, 90, 85, 90, 92, NA,
NA, 79, 79, NA, 86, 81, 88, NA, 90, 86, 92, 85, 92, 83, 92,
90, 92, 95, 94, 88, 90, 86, 88, 101, 95, 92, 81, NA, 92,
90, 81, NA, 90, 81, 88, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 85, NA, NA, NA, NA, NA, 85, 88, 86, 88, 106, 101, 88,
NA, 79, NA, 85, 99, 92, 79, 88, 88, 95, 81, 86, 77, 81, 92,
97, NA, 86, NA, 88, 94, 81, 86, 85, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 85, 88, 95, 83, 92, 112, 94, 95, 108,
97, 90, 88, 86, 97, 95, 88, 90, 88, 77, 94, 81, NA, 79, 83,
95, 88, 81, 92, 92, NA, 88, 86, NA, 85, 85, 97, 81, 88, 90,
NA, 77.4, 94, 83, NA, 95, 85, 92, 83, 95, 88, 94, 94, 88,
77, 90, 86, 92, NA, NA, NA, 95, 92, NA, 90, 103, 90, 85,
92, 83, 81, 94, 81, 79, 94, NA, 92, 99, 95, 84, 95, 72, 90,
79, 97.5, 85, 88, 79, 81, 72, 85, 88), format.spss = "F4.2", display_width = 11L),
hdl = structure(c(54, 55, 48, 38, 46, 50, 45, 38, 50, 43,
39, 32, 35, 34, 40, 48, 53, 33, 42, 34, 41, 48, 51, 38, 53,
38, 37, 44, 37, 33, 54, 47, 51, 39, 44, 54, 32, 53, 39, 36,
58, 41, 34, 43, 40, 49, 49, 50, 37, 36, 54, 47, 35, 40, 50,
44, 40, 43, 45, 41, 34, 50, 46, 46, 50, 53, 53, 45, 37, 70,
51, 55, 51, 58, 58, 49, 44, 37, 32, 64, 41, 63, 46, 55, 46,
65, 43, 55, 42, 56, 39, 50, 38, 46, 45, 53, 53, 39, 45, 47,
48, 32, 45, 45, 36, 60, 30, 43, 43, 57, 36, 56, 45, 40, 40,
61, 50, 29, 55, 38, 35, 47, 42, 50, 46, 26, 60, 33, 36, 34,
44, 59, 45, 44, 55, 45, 53, 38, 50, 40, 57, 46, 48, 45, 43,
49, 53, 39, 46, 39, 36, 39, 36, 42, 40, 50, 63, 46, 45, 39,
43, 30, 57, 46, 40, 39, 39, 53, 40, 54, 56, 40, 37, 48, 43,
29, 46, 45, 82, 31, 34, 37, 41, 63, 34, 50, 37, 51, 36, 42,
41, 34, 55, 40, 42, 60, 36, 38, 52, 57, 48, 48, 46, 47, 50,
41, 48, NA, 40, 45, NA, 43, 58, 42, 48, 44, 46, 47, 55, NA,
NA, 38, 52, NA, 53, 31, 51, NA, 32, 51, 41, 38, 57, 36, 50,
41, 60, 65, 39, 52, 36, 36, 49, 43, 34, 44, 41, NA, 50, 52,
37, NA, 58, 45, 34, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
46, NA, NA, NA, NA, NA, 59, 55, 50, 46, 58, 58, 42, NA, 31,
NA, 48, 43, 66, 55, 51, 41, 50, 38, 46, 41, 43, 38, 48, NA,
46, NA, 56, 44, 46, 48, 49, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 63, 41, 39, 46, 58, 53, 33, 53, 48, 33, 44, 46,
49, 48, 44, 55, 44, 39, 32, 46, 50, NA, 47, 53, 39, 51, 61,
48, 32, NA, 42, 46, NA, 49, 48, 52, 39, 40, 38, NA, 31, 46,
48, NA, 51, 58, 43, 49, 43, 65, 41, 61, 49, 35, 37, 36, 58,
NA, NA, NA, 38, 45, NA, 58, 31, 49, 52, 65, 32, 45, 39, 37,
41, 34, NA, 42, 51, 39, 48, 36, 35, 55, 38, 48, 53, 41, 39,
49, 63, 41, 47), label = "HDL-Cholesterol", format.spss = "F3.2", display_width = 11L),
ldl = structure(c(100, 104, 171, 153, 107, 152, 87, 101,
70, 137, 96, 95, 98, 94, 92, 102, 63, 104, 62, 75, 125, 117,
114, 132, 112, 146, 121, 91, 113, 120, 96, 96, 95, 87, 96,
134, 98, 92, 88, 101, 133, 113, 77, 128, 97, 169, 136, 96,
74, 59, 121, 66, 109, 103, 116, 86, 87, 124, 88, 94, 77,
98, 90, 133, 79, 78, 98, 129, 62, 62, 96, 72, 85, 98, 101,
132, 69, 196, 76, 125, 105, 108, 89, 108, 123, 51, 92, 50,
121, 105, 80, 103, 59, 96, 89, 65, 77, 90, 92, 65, 123, 96,
80, 128, 92, 124, 96, 83, 120, 145, 114, 134, 116, 65, 91,
103, 84, 123, 99, 96, 61, 82, 85, 116, 116, 113, 121, 69,
82, 100, 108, 99, 144, 152, 158, 128, 112, 89, 119, 61, 99,
147, 109, 121, 92, 115, 95, 62, 72, 130, 96, 76, 117, 96,
108, 131, 120, 67, 99, 105, 63, 63, 103, 128, 92, 120, 146,
106, 103, 94, 85, 122, 111, 102, 143, 74, 87, 80, 67, 140,
85, 87, 101, 94, 122, 124, 82, 150, 92, 84, 119, 98, 89,
97, 117, 122, 111, 86, 90, 110, 107, 150, 103, 94, 149, 159,
91, NA, 109, 126, NA, 167, 77, 90, 103, 80, 68, 75, 55, NA,
NA, 74, 113, NA, 102, 116, 84, NA, 66, 85, 114, 111, 101,
95, 92, 86, 96, 90, 92, 77, 91, 108, 86, 118, 85, 127, 99,
NA, 160, 80, 63, NA, 123, 86, 94, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 106, NA, NA, NA, NA, NA, 70, 85, 70, 96,
102, 117, 101, NA, 146, NA, 94, 122, 122, 94, 110, 121, 39,
72, 48, 109, 110, 60, 95, NA, 83, NA, 79, 87, 113, 103, 55,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 157, 103, 56,
92, 114, 78, 97, 106, 117, 61, 72, 83, 91, 122, 106, 103,
89, 51, 89, 153, 90, NA, 132, 132, 110, 84, 84, 96, 72, NA,
104, 122, NA, 80, 113, 106, 62, 72, 121, NA, 102, 125, 130,
NA, 111, 119, 66, 109, 119, 91, 92, 120, 160, 93, 117, 126,
88, NA, NA, NA, 115, 100, NA, 200, 79, 95, 99, 89, 123, 108,
82, 108, 81, 103, NA, 103, 149, 116, 115, 122, 95, 106, 89,
128, 118, 123, 51, 90, 130, 119, 120), label = "LDL-Cholesterol", format.spss = "F4.2", display_width = 11L)), row.names = c(NA,
-404L), class = c("tbl_df", "tbl", "data.frame"), reshapeLong = list(
varying = list(c("age_1", "age_2"), c("whz_1", "whz_2"),
c("haz_1", "haz_2"), c("waz_1", "waz_2"), c("zbmi_1",
"zbmi_2"), c("wc_1", "wc_2"), c("abc_1", "abc_2"), c("PA_1",
"PA_2"), c("PAextra_1", "PAextra_2"), c("TVweekdays_1",
"TVweekdays_2"), c("TVweekend_1", "TVweekend_2"), c("kidmed_1",
"kidmed_2"), c("totalcholesterol_1", "totalcholesterol_2"
), c("ldl_1", "ldl_2"), c("hdl_1", "hdl_2"), c("triglycerides_1",
"triglycerides_2"), c("glucose_1", "glucose_2"), c("insuline_1",
"insuline_2"), c("hba1c_1", "hba1c_2"), c("homair_1",
"homair_2"), c("fatmass_1", "fatmass_2"), c("energykcal_1",
"energykcal_2"), c("protein_1", "protein_2"), c("proteinpc_1",
"proteinpc_2"), c("carbohydrates_1", "carbohydrates_2"
), c("carbohydratespc_1", "carbohydratespc_2"), c("sugar_1",
"sugar_2"), c("sugarpc_1", "sugarpc_2"), c("starch_1",
"starch_2"), c("fruitportions_1", "fruitportions_2"),
c("vegetablesportions_1", "vegetablesportions_2"), c("vegetalfiber_1",
"vegetalfiber_2"), c("solublefiber_1", "solublefiber_2"
), c("insolublefiber_1", "insolublefiber_2"), c("lipids_1",
"lipids_2"), c("lipidspc_1", "lipidspc_2"), c("sfa_1",
"sfa_2"), c("sfapc_1", "sfapc_2"), c("mufa_1", "mufa_2"
), c("mufapc_1", "mufapc_2"), c("pufa_1", "pufa_2"),
c("pufapc_1", "pufapc_2"), c("cholesterolintake_1", "cholesterolintake_2"
)), v.names = c("age", "whz", "haz", "waz", "zbmi", "wc",
"abc", "PA", "PAextra", "TVweekdays", "TVweekend", "kidmed",
"totalcholesterol", "ldl", "hdl", "triglycerides", "glucose",
"insuline", "hba1c", "homair", "fatmass", "energykcal", "protein",
"proteinpc", "carbohydrates", "carbohydratespc", "sugar",
"sugarpc", "starch", "fruitportions", "vegetablesportions",
"vegetalfiber", "solublefiber", "insolublefiber", "lipids",
" lipidspc", "sfa", "sfapc", "mufa", "mufapc", "pufa", "pufapc",
"cholesterolintake"), idvar = c("id", "group"), timevar = "time"))
You can drop the outliers by changing your remove_outlier function to this:
remove_outliers <- function(dflinear, cols = names(dflinear)) {
for (col in cols) {
dflinear[,col] <- ifelse(outliers(dflinear[[col]]),NA,dflinear[[col]])
}
dflinear
}
But I would think very carefully about whether this is a good approach to outlier detection and removal. This procedure is removing values that look like regular parts of the distribution. With a lot of values you would expect some to be outside of the range Q3+1.5IQR etc.
Eg, this is the qqnorm for the ldl variable. Doesn't look like any problematic values at all really, but your procedure is throwing out the top five and the lowest value:

how to add the names on the y axis when graphing multiple variables through a function

I have this function that allows me to create multiple graphs on various variables of the dataset.
However in the output on the y-axis it always put the name of the list "varlist" instead of the name of each variable in the list, i.e. insuline, glucose, hdl and ldl.
How could I do that? thank you
# Multiple box plot per group per time
library(ggplot2)
names(dflinear) <- c("id", "group", "sex", "time", "insuline", "glucose", "hdl", "ldl")
# Create a list wherein the function will be applied to
varlist<-c(list(dflinear$insuline, dflinear$glucose, dflinear$hdl, dflinear$ldl))
names(varlist)<-c("insuline", "glucose", "hdl", "ldl")
# Create the function boxplot
A <- function (varlist) {
dflinear %>% group_by('group')%>%
ggplot(mapping = aes_string(x='time', y='varlist', fill='group')) +
geom_boxplot()
}
# Apply it to the whole list and graph the plots
plots<-lapply(varlist, FUN = A)
plots
Reproducible dataset
dflinear<- structure(list(id = structure(c("SA01", "SA02", "SA03", "SA04",
"SA05", "SA06", "SA07", "SA08", "SA09", "SA10", "SA11", "SA12",
"SA13", "SA14", "SA15", "SA16", "SA17", "SA18", "SA19", "SA20",
"SA21", "SA22", "SA23", "SA24", "SA25", "SA26", "SA27", "SA28",
"SA29", "SA30", "SA31", "SA32", "SA33", "SA34", "SA35", "SA36",
"SA37", "SA38", "SA39", "SA40", "SA41", "SA42", "SA43", "SA44",
"SA45", "SA46", "SA47", "SA48", "SA49", "SA50", "SA51", "SA52",
"SA53", "SA54", "SA56", "SA57", "SA58", "SA59", "SA60", "SA61",
"SA62", "SA63", "SA64", "SA65", "SA66", "SA67", "SA68", "SA69",
"SA72", "SA73", "SA74", "SA75", "SA76", "SA77", "SA78", "SA79",
"SA80", "SA81", "SA82", "SA83", "SA84", "SA85", "SA86", "SA87",
"SA88", "SA89", "SA90", "SA92", "SA93", "SA94", "SA95", "SA96",
"SA97", "SA99", "SA100", "SA101", "SA102", "SA103", "SA104",
"SA105", "SA107", "SA108", "SA109", "SA110", "SA111", "SA112",
"SA113", "SA114", "SA115", "SA116", "SA118", "SC01", "SC02",
"SC03", "SC04", "SC05", "SC06", "SC07", "SC08", "SC09", "SC10",
"SC11", "SC12", "SC13", "SC14", "SC15", "SC16", "SC17", "SC18",
"SC19", "SC20", "SC21", "SC22", "SC23", "SC24", "SC25", "SC26",
"SC27", "SC28", "SC29", "SC30", "SC31", "SC32", "SC33", "SC34",
"SC35", "SC36", "SC37", "SC38", "M01", "M02", "M03", "M04", "M05",
"M06", "M07", "M08", "M09", "M10", "M11", "M12", "M13", "M14",
"M15", "M16", "M17", "M18", "M19", "M20", "M21", "M22", "M23",
"M24", "M25", "M26", "M27", "M28", "M29", "M30", "M31", "M32",
"M33", "M34", "M35", "M36", "M37", "M38", "M39", "M40", "M41",
"M42", "M43", "M44", "M45", "M46", "M47", "M48", "M49", "M50",
"M51", "M52", "M53", "SA01", "SA02", "SA03", "SA04", "SA05",
"SA06", "SA07", "SA08", "SA09", "SA10", "SA11", "SA12", "SA13",
"SA14", "SA15", "SA16", "SA17", "SA18", "SA19", "SA20", "SA21",
"SA22", "SA23", "SA24", "SA25", "SA26", "SA27", "SA28", "SA29",
"SA30", "SA31", "SA32", "SA33", "SA34", "SA35", "SA36", "SA37",
"SA38", "SA39", "SA40", "SA41", "SA42", "SA43", "SA44", "SA45",
"SA46", "SA47", "SA48", "SA49", "SA50", "SA51", "SA52", "SA53",
"SA54", "SA56", "SA57", "SA58", "SA59", "SA60", "SA61", "SA62",
"SA63", "SA64", "SA65", "SA66", "SA67", "SA68", "SA69", "SA72",
"SA73", "SA74", "SA75", "SA76", "SA77", "SA78", "SA79", "SA80",
"SA81", "SA82", "SA83", "SA84", "SA85", "SA86", "SA87", "SA88",
"SA89", "SA90", "SA92", "SA93", "SA94", "SA95", "SA96", "SA97",
"SA99", "SA100", "SA101", "SA102", "SA103", "SA104", "SA105",
"SA107", "SA108", "SA109", "SA110", "SA111", "SA112", "SA113",
"SA114", "SA115", "SA116", "SA118", "SC01", "SC02", "SC03", "SC04",
"SC05", "SC06", "SC07", "SC08", "SC09", "SC10", "SC11", "SC12",
"SC13", "SC14", "SC15", "SC16", "SC17", "SC18", "SC19", "SC20",
"SC21", "SC22", "SC23", "SC24", "SC25", "SC26", "SC27", "SC28",
"SC29", "SC30", "SC31", "SC32", "SC33", "SC34", "SC35", "SC36",
"SC37", "SC38", "M01", "M02", "M03", "M04", "M05", "M06", "M07",
"M08", "M09", "M10", "M11", "M12", "M13", "M14", "M15", "M16",
"M17", "M18", "M19", "M20", "M21", "M22", "M23", "M24", "M25",
"M26", "M27", "M28", "M29", "M30", "M31", "M32", "M33", "M34",
"M35", "M36", "M37", "M38", "M39", "M40", "M41", "M42", "M43",
"M44", "M45", "M46", "M47", "M48", "M49", "M50", "M51", "M52",
"M53"), label = "Code of PrevenGo", format.spss = "A5", display_width = 12L),
group = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 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 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), insuline = structure(c(9, 4.1, 3.3, 9.4, 22.9, 16.2,
8.7, 16.7, 21.2, 21, 12.8, 7.3, 38.4, 20.2, 19.6, 6.4, 18.9,
12.1, 8.2, 17, 15.6, 12.5, 19.1, 13.7, 8, 20.1, 19.8, 6.8,
15.4, 14.7, 11.9, 8.8, 7.9, 51.2, 10.8, 8.1, 28.6, 8.6, 27.9,
13.3, 9, 16.3, 13.3, 5.8, 27.3, 4.2, 8.2, 9.9, 20.1, 11.7,
8.7, 18.1, 10.9, 27.4, 14.6, 29.1, 10.2, 20.2, 9.7, 12.3,
18.2, 1.9, 11.6, 14.6, 7.9, 11.2, 13.8, 21.2, 23.8, 18, 23.5,
21.4, 11.4, 12, 6.6, 13.5, 10.4, 25.3, 56.8, 10.7, 21.5,
8.5, 30.2, 5.3, 7.5, 15.9, 11.6, 22.4, 25.2, 6.1, 15.1, 9.3,
24.3, 30.8, 8.9, 9.8, 34.1, 13.4, 23.1, 21.1, 4.8, 20.1,
38.5, 16.1, 34.1, 16.1, 17.7, 41.4, 20.4, 21.5, 36.3, 15.9,
8.8, 6.1, 29, 4, 23.1, 36.8, 16.4, 15.5, 28.8, 15.9, NA,
7.1, 6.1, 10, 9.1, 25.2, 19.1, 6.9, 14.7, 23.1, 19.3, 12.3,
7.3, 5.9, 8, 0.5, 9, 4, 10.4, 21.4, 14.6, 8.8, 24.5, 5.3,
9.8, 17.6, 10.2, 10.7, 23, 14.5, 4.6, 33.3, 23.3, 7.2, 3.7,
13.1, 6.7, 20, 7.5, 9.2, 4.5, 2.1, 7.7, 11.7, 7.6, 22.5,
8.8, 5.1, 14.8, 15.1, 18.8, 24.3, 14, 17.2, 16.2, 23.6, 17.4,
16.5, 12.1, 15.3, 11.4, 8.7, 22.6, 10.5, 7.4, 15.1, 13.1,
24.6, 19.3, 19.7, 14.1, 5.9, 19.7, 14.9, 5.9, 17.2, 16.9,
6.2, 11.2, 4.1, 10, 3.7, 3.6, 11.6, 16.9, NA, 8, 17.3, NA,
18.3, 4, 3.1, 26.4, 12.9, 17.9, 10.3, 22.5, NA, NA, 23.4,
15.1, NA, 11.9, 27, 6.2, NA, 21.5, 11.6, 15.8, 8.6, 15.2,
10.1, 20.6, 21.7, 45.3, 8.3, 19.5, 29.2, 21.5, 11.4, 9.5,
31.8, 35.3, 11.2, 15.4, NA, 8.5, 22.6, 14.3, NA, 11.8, 11.4,
4.2, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 35.8, NA, NA,
NA, NA, NA, 19.7, 42.8, 30.6, 12.2, 5.2, 4.9, 20.4, NA, 23.5,
NA, 13.6, 19.4, 6.9, 16.7, 7.2, 14.7, 59.2, 22, 41.4, 18.1,
10.5, 19.8, 17.4, NA, 25.9, NA, 8.3, 25.9, 5.7, 17.1, 25.2,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 10.3, 9.1, 14.8,
13.7, 6.2, 17.9, 13.9, 14.6, 70.4, 23.6, 13.8, 15.2, 9.9,
14, 27.6, 14.3, 23.7, 11, 12.1, 13.5, 21, NA, 7.2, 12.3,
4.4, 6.2, 3.9, 15, 9.6, NA, 9, 10.3, NA, 13.3, 6, 11.3, 17.6,
8.5, 10, NA, 11.8, 10.4, 26.2, NA, 10, 5.7, 16.3, 4.7, 20.3,
7.7, 14.6, 9.4, 6.3, 10, 11.1, 6.7, 42.5, NA, NA, NA, 7.7,
18.6, NA, 16.7, 25.4, 21.8, 26.8, 10.2, 13.8, 11.6, 19.1,
8.3, 3.8, 31.1, NA, 7.1, 11.1, 8.7, 19, 16, 31.8, 11.7, 3.4,
17.6, 12.3, 5.1, 17.5, 6.7, 3.8, 16.6, 6.1), format.spss = "F4.2", display_width = 11L),
glucose = structure(c(90, 95, 79, 85, 95, 97, 86, 74, 88,
95, 94, 88, 86, 94, 86, 95, 97, 88, 88, 88, 83, 103, 79,
67, 88, 79, 90, 79, 97, 94, 85, 83, 88, 97, 81, 95, 92, 94,
99, 79, 83, 92, 81, 92, 79, 94, 83, 79, 81, 92, 86, 95, 92,
95, 92, 85, 94, 81, 86, 85, 99, 92, 85, 72, 86, 81, 79, 86,
97, 88, 92, 97, 83, 103, 97, 95, 85, 77, 77, 83, 99, 90,
77, 77, 83, 92, 88, 83, 88, 86, 88, 97, 101, 99, 88, 101,
94, 86, 85, 83, 86, 88, 92, 94, 94, 90, 160, 94, 83, 95,
97, 88, 88, 95, 90, 92, 113, 104, 85, 101, 91.8, 99, 94,
85, 85, 83, 86, 88, 95, 79, 101, 92, 83, 90, 85, 95, 88,
79, 90, 79, 94, 99, 83, 85, 85, 77, 99, 81, 92, 86.4, 95.4,
82.8, 73.8, 81, 90, 82.8, 79.2, 90, 82.8, 91.8, 90, 84.6,
84.6, 84.6, 77.4, 77.4, 75.6, 88.2, 79.2, 92, 90, 113, 81,
81, 81, 84.6, 88.2, 73.8, 81, 81, 82.8, 79.2, 70.2, 91.8,
97.2, 82.8, 70.2, 91.8, 93.6, 86.4, 93.6, 73.8, 95.4, 81,
97.2, 77.4, 90, 82.8, 86.4, 88.2, 88.2, 73.8, 90, 92, 83,
86, 99, NA, 86, 81, NA, 99, 83, 86, 76, 90, 85, 90, 92, NA,
NA, 79, 79, NA, 86, 81, 88, NA, 90, 86, 92, 85, 92, 83, 92,
90, 92, 95, 94, 88, 90, 86, 88, 101, 95, 92, 81, NA, 92,
90, 81, NA, 90, 81, 88, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 85, NA, NA, NA, NA, NA, 85, 88, 86, 88, 106, 101, 88,
NA, 79, NA, 85, 99, 92, 79, 88, 88, 95, 81, 86, 77, 81, 92,
97, NA, 86, NA, 88, 94, 81, 86, 85, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 85, 88, 95, 83, 92, 112, 94, 95, 108,
97, 90, 88, 86, 97, 95, 88, 90, 88, 77, 94, 81, NA, 79, 83,
95, 88, 81, 92, 92, NA, 88, 86, NA, 85, 85, 97, 81, 88, 90,
NA, 77.4, 94, 83, NA, 95, 85, 92, 83, 95, 88, 94, 94, 88,
77, 90, 86, 92, NA, NA, NA, 95, 92, NA, 90, 103, 90, 85,
92, 83, 81, 94, 81, 79, 94, NA, 92, 99, 95, 84, 95, 72, 90,
79, 97.5, 85, 88, 79, 81, 72, 85, 88), format.spss = "F4.2", display_width = 11L),
hdl = structure(c(54, 55, 48, 38, 46, 50, 45, 38, 50, 43,
39, 32, 35, 34, 40, 48, 53, 33, 42, 34, 41, 48, 51, 38, 53,
38, 37, 44, 37, 33, 54, 47, 51, 39, 44, 54, 32, 53, 39, 36,
58, 41, 34, 43, 40, 49, 49, 50, 37, 36, 54, 47, 35, 40, 50,
44, 40, 43, 45, 41, 34, 50, 46, 46, 50, 53, 53, 45, 37, 70,
51, 55, 51, 58, 58, 49, 44, 37, 32, 64, 41, 63, 46, 55, 46,
65, 43, 55, 42, 56, 39, 50, 38, 46, 45, 53, 53, 39, 45, 47,
48, 32, 45, 45, 36, 60, 30, 43, 43, 57, 36, 56, 45, 40, 40,
61, 50, 29, 55, 38, 35, 47, 42, 50, 46, 26, 60, 33, 36, 34,
44, 59, 45, 44, 55, 45, 53, 38, 50, 40, 57, 46, 48, 45, 43,
49, 53, 39, 46, 39, 36, 39, 36, 42, 40, 50, 63, 46, 45, 39,
43, 30, 57, 46, 40, 39, 39, 53, 40, 54, 56, 40, 37, 48, 43,
29, 46, 45, 82, 31, 34, 37, 41, 63, 34, 50, 37, 51, 36, 42,
41, 34, 55, 40, 42, 60, 36, 38, 52, 57, 48, 48, 46, 47, 50,
41, 48, NA, 40, 45, NA, 43, 58, 42, 48, 44, 46, 47, 55, NA,
NA, 38, 52, NA, 53, 31, 51, NA, 32, 51, 41, 38, 57, 36, 50,
41, 60, 65, 39, 52, 36, 36, 49, 43, 34, 44, 41, NA, 50, 52,
37, NA, 58, 45, 34, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
46, NA, NA, NA, NA, NA, 59, 55, 50, 46, 58, 58, 42, NA, 31,
NA, 48, 43, 66, 55, 51, 41, 50, 38, 46, 41, 43, 38, 48, NA,
46, NA, 56, 44, 46, 48, 49, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 63, 41, 39, 46, 58, 53, 33, 53, 48, 33, 44, 46,
49, 48, 44, 55, 44, 39, 32, 46, 50, NA, 47, 53, 39, 51, 61,
48, 32, NA, 42, 46, NA, 49, 48, 52, 39, 40, 38, NA, 31, 46,
48, NA, 51, 58, 43, 49, 43, 65, 41, 61, 49, 35, 37, 36, 58,
NA, NA, NA, 38, 45, NA, 58, 31, 49, 52, 65, 32, 45, 39, 37,
41, 34, NA, 42, 51, 39, 48, 36, 35, 55, 38, 48, 53, 41, 39,
49, 63, 41, 47), label = "HDL-Cholesterol", format.spss = "F3.2", display_width = 11L),
ldl = structure(c(100, 104, 171, 153, 107, 152, 87, 101,
70, 137, 96, 95, 98, 94, 92, 102, 63, 104, 62, 75, 125, 117,
114, 132, 112, 146, 121, 91, 113, 120, 96, 96, 95, 87, 96,
134, 98, 92, 88, 101, 133, 113, 77, 128, 97, 169, 136, 96,
74, 59, 121, 66, 109, 103, 116, 86, 87, 124, 88, 94, 77,
98, 90, 133, 79, 78, 98, 129, 62, 62, 96, 72, 85, 98, 101,
132, 69, 196, 76, 125, 105, 108, 89, 108, 123, 51, 92, 50,
121, 105, 80, 103, 59, 96, 89, 65, 77, 90, 92, 65, 123, 96,
80, 128, 92, 124, 96, 83, 120, 145, 114, 134, 116, 65, 91,
103, 84, 123, 99, 96, 61, 82, 85, 116, 116, 113, 121, 69,
82, 100, 108, 99, 144, 152, 158, 128, 112, 89, 119, 61, 99,
147, 109, 121, 92, 115, 95, 62, 72, 130, 96, 76, 117, 96,
108, 131, 120, 67, 99, 105, 63, 63, 103, 128, 92, 120, 146,
106, 103, 94, 85, 122, 111, 102, 143, 74, 87, 80, 67, 140,
85, 87, 101, 94, 122, 124, 82, 150, 92, 84, 119, 98, 89,
97, 117, 122, 111, 86, 90, 110, 107, 150, 103, 94, 149, 159,
91, NA, 109, 126, NA, 167, 77, 90, 103, 80, 68, 75, 55, NA,
NA, 74, 113, NA, 102, 116, 84, NA, 66, 85, 114, 111, 101,
95, 92, 86, 96, 90, 92, 77, 91, 108, 86, 118, 85, 127, 99,
NA, 160, 80, 63, NA, 123, 86, 94, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 106, NA, NA, NA, NA, NA, 70, 85, 70, 96,
102, 117, 101, NA, 146, NA, 94, 122, 122, 94, 110, 121, 39,
72, 48, 109, 110, 60, 95, NA, 83, NA, 79, 87, 113, 103, 55,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 157, 103, 56,
92, 114, 78, 97, 106, 117, 61, 72, 83, 91, 122, 106, 103,
89, 51, 89, 153, 90, NA, 132, 132, 110, 84, 84, 96, 72, NA,
104, 122, NA, 80, 113, 106, 62, 72, 121, NA, 102, 125, 130,
NA, 111, 119, 66, 109, 119, 91, 92, 120, 160, 93, 117, 126,
88, NA, NA, NA, 115, 100, NA, 200, 79, 95, 99, 89, 123, 108,
82, 108, 81, 103, NA, 103, 149, 116, 115, 122, 95, 106, 89,
128, 118, 123, 51, 90, 130, 119, 120), label = "LDL-Cholesterol", format.spss = "F4.2", display_width = 11L)), row.names = c(NA,
-404L), class = c("tbl_df", "tbl", "data.frame"), reshapeLong = list(
varying = list(c("age_1", "age_2"), c("whz_1", "whz_2"),
c("haz_1", "haz_2"), c("waz_1", "waz_2"), c("zbmi_1",
"zbmi_2"), c("wc_1", "wc_2"), c("abc_1", "abc_2"), c("PA_1",
"PA_2"), c("PAextra_1", "PAextra_2"), c("TVweekdays_1",
"TVweekdays_2"), c("TVweekend_1", "TVweekend_2"), c("kidmed_1",
"kidmed_2"), c("totalcholesterol_1", "totalcholesterol_2"
), c("ldl_1", "ldl_2"), c("hdl_1", "hdl_2"), c("triglycerides_1",
"triglycerides_2"), c("glucose_1", "glucose_2"), c("insuline_1",
"insuline_2"), c("hba1c_1", "hba1c_2"), c("homair_1",
"homair_2"), c("fatmass_1", "fatmass_2"), c("energykcal_1",
"energykcal_2"), c("protein_1", "protein_2"), c("proteinpc_1",
"proteinpc_2"), c("carbohydrates_1", "carbohydrates_2"
), c("carbohydratespc_1", "carbohydratespc_2"), c("sugar_1",
"sugar_2"), c("sugarpc_1", "sugarpc_2"), c("starch_1",
"starch_2"), c("fruitportions_1", "fruitportions_2"),
c("vegetablesportions_1", "vegetablesportions_2"), c("vegetalfiber_1",
"vegetalfiber_2"), c("solublefiber_1", "solublefiber_2"
), c("insolublefiber_1", "insolublefiber_2"), c("lipids_1",
"lipids_2"), c("lipidspc_1", "lipidspc_2"), c("sfa_1",
"sfa_2"), c("sfapc_1", "sfapc_2"), c("mufa_1", "mufa_2"
), c("mufapc_1", "mufapc_2"), c("pufa_1", "pufa_2"),
c("pufapc_1", "pufapc_2"), c("cholesterolintake_1", "cholesterolintake_2"
)), v.names = c("age", "whz", "haz", "waz", "zbmi", "wc",
"abc", "PA", "PAextra", "TVweekdays", "TVweekend", "kidmed",
"totalcholesterol", "ldl", "hdl", "triglycerides", "glucose",
"insuline", "hba1c", "homair", "fatmass", "energykcal", "protein",
"proteinpc", "carbohydrates", "carbohydratespc", "sugar",
"sugarpc", "starch", "fruitportions", "vegetablesportions",
"vegetalfiber", "solublefiber", "insolublefiber", "lipids",
" lipidspc", "sfa", "sfapc", "mufa", "mufapc", "pufa", "pufapc",
"cholesterolintake"), idvar = c("id", "group"), timevar = "time"))
Instead of making your varlist a list of vectors you could simply pass a vector with names of the colums you want to plot. Then use aes_string(..., y = varlist) inside your function and you will automatically get the name of the variable as the y axis title:
# Multiple box plot per group per time
library(ggplot2)
library(dplyr)
# Create a list wherein the function will be applied to
varlist <- c("insuline", "glucose", "hdl", "ldl")
names(varlist) <- varlist
# Create the function boxplot
A <- function(varlist) {
dflinear %>%
group_by("group") %>%
ggplot(mapping = aes_string(x = "time", y = varlist, fill = "group")) +
geom_boxplot()
}
# Apply it to the whole list and graph the plots
plots <- lapply(varlist, FUN = A)
plots[[1]]

Variance-covariance matrix of repeated measurements from data in a long format?

I have repeated measurements data on 66 patients with either endogenous or exogenous depression (endo) and depression scores measured weekly for 0-5 weeks (hdrs, so six measurements per patients including baseline). The data is in a long format:
mydata <- structure(list(id = c(101, 101, 101, 101, 101, 101, 103, 103,
103, 103, 103, 103, 104, 104, 104, 104, 104, 104, 105, 105, 105,
105, 105, 105, 106, 106, 106, 106, 106, 106, 107, 107, 107, 107,
107, 107, 108, 108, 108, 108, 108, 108, 113, 113, 113, 113, 113,
113, 114, 114, 114, 114, 114, 114, 115, 115, 115, 115, 115, 115,
117, 117, 117, 117, 117, 117, 118, 118, 118, 118, 118, 118, 120,
120, 120, 120, 120, 120, 121, 121, 121, 121, 121, 121, 123, 123,
123, 123, 123, 123, 302, 302, 302, 302, 302, 302, 303, 303, 303,
303, 303, 303, 304, 304, 304, 304, 304, 304, 305, 305, 305, 305,
305, 305, 308, 308, 308, 308, 308, 308, 309, 309, 309, 309, 309,
309, 310, 310, 310, 310, 310, 310, 311, 311, 311, 311, 311, 311,
312, 312, 312, 312, 312, 312, 313, 313, 313, 313, 313, 313, 315,
315, 315, 315, 315, 315, 316, 316, 316, 316, 316, 316, 318, 318,
318, 318, 318, 318, 319, 319, 319, 319, 319, 319, 322, 322, 322,
322, 322, 322, 327, 327, 327, 327, 327, 327, 328, 328, 328, 328,
328, 328, 331, 331, 331, 331, 331, 331, 333, 333, 333, 333, 333,
333, 334, 334, 334, 334, 334, 334, 335, 335, 335, 335, 335, 335,
337, 337, 337, 337, 337, 337, 338, 338, 338, 338, 338, 338, 339,
339, 339, 339, 339, 339, 344, 344, 344, 344, 344, 344, 345, 345,
345, 345, 345, 345, 346, 346, 346, 346, 346, 346, 347, 347, 347,
347, 347, 347, 348, 348, 348, 348, 348, 348, 349, 349, 349, 349,
349, 349, 350, 350, 350, 350, 350, 350, 351, 351, 351, 351, 351,
351, 352, 352, 352, 352, 352, 352, 353, 353, 353, 353, 353, 353,
354, 354, 354, 354, 354, 354, 355, 355, 355, 355, 355, 355, 357,
357, 357, 357, 357, 357, 360, 360, 360, 360, 360, 360, 361, 361,
361, 361, 361, 361, 501, 501, 501, 501, 501, 501, 502, 502, 502,
502, 502, 502, 504, 504, 504, 504, 504, 504, 505, 505, 505, 505,
505, 505, 507, 507, 507, 507, 507, 507, 603, 603, 603, 603, 603,
603, 604, 604, 604, 604, 604, 604, 606, 606, 606, 606, 606, 606,
607, 607, 607, 607, 607, 607, 608, 608, 608, 608, 608, 608, 609,
609, 609, 609, 609, 609, 610, 610, 610, 610, 610, 610), week = structure(c(0,
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0,
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0,
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0,
1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3,
4, 5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, 9, 21, 25, 23, 18, 20, NA, 21, 21, 16, 19,
NA, 6, 21, 22, 11, 9, 9, 7, 21, 23, 19, 23, 23, NA, NA, 17,
11, 13, 7, 7, NA, 16, 16, 16, 16, 11, 19, 16, 13, 12, 7,
6, NA, 26, 18, 18, 14, 11, 20, 19, 17, 18, 16, 17, 20, 22,
19, 19, 12, 14, 15, 15, 15, 13, 5, 5, 18, 22, 16, 8, 9, 12,
21, 21, 13, 14, 10, 5, 21, 27, 29, NA, 12, 24, 19, 17, 15,
11, 5, 1, 22, 21, 18, 17, 12, 11, 22, 22, 16, 19, 20, 11,
24, 19, 11, 7, 6, NA, 20, 16, 21, 17, NA, 15, 17, NA, 18,
17, 17, 6, 21, 19, 10, 11, 11, 8, 27, 21, 17, 13, 5, NA,
32, 26, 23, 26, 23, 24, 17, 18, 19, 21, 17, 11, 24, 18, 10,
14, 13, 12, 28, 21, 25, 32, 34, NA, 17, 18, 15, 8, 19, 17,
22, 24, 28, 26, 28, 29, 19, 21, 18, 16, 14, 10, 23, 20, 21,
20, 24, 14, 31, 25, NA, 7, 8, 11, 21, 21, 18, 15, 12, 10,
27, 22, 23, 21, 12, 13, 22, 20, 22, 23, 19, 18, 27, NA, 14,
12, 11, 12, NA, 21, 12, 13, 13, 18, 29, 27, 27, 22, 22, 23,
25, 24, 19, 23, 14, 21, 18, 15, 14, 10, 8, NA, 24, 21, 12,
13, 12, 5, 17, 19, 15, 12, 9, 13, 22, 25, 12, 16, 10, 16,
30, 27, 23, 20, 12, 11, 21, 19, 18, 15, 18, 19, 27, 21, 24,
22, 16, 11, 28, 27, 27, 26, 23, NA, 22, 26, 20, 13, 10, 7,
27, 22, 24, 25, 19, 19, 21, 28, 27, 29, 28, 33, 30, 22, 11,
8, 7, 19, 29, 30, 26, 22, 19, 24, 21, 22, 13, 11, 2, 1, 19,
17, 15, 16, 12, 12, 21, 11, 18, 0, 0, 4, 27, 26, 26, 25,
24, 19, 28, 22, 18, 20, 11, 13, 27, 27, 13, 5, 7, NA, 19,
33, 12, 12, 3, 1, 30, 39, 30, 27, 20, 4, 24, 19, 14, 12,
3, 4, NA, 25, 22, 14, 15, 2, 34, NA, 33, 23, NA, 11)), row.names = c(NA,
-396L), class = "data.frame")
And looks like this:
head(reisby_long)
id week week_fact endo hdrs
1 101 0 Week 0 Exogenous 26
2 101 1 Week 1 Exogenous 22
3 101 2 Week 2 Exogenous 18
4 101 3 Week 3 Exogenous 7
5 101 4 Week 4 Exogenous 4
6 101 5 Week 5 Exogenous 3
My question is if I can obtain a variance-covariance matrix from this dataset without converting it to a wide format first. I ask because I have another dataset for which converting to a wide format is going to take me a long time (because I'm not very experienced in doing so) and the only reason for doing it would be to get a variance-covariance matrix.
Thanks!
Not sure if you can use that directly, but you wouldn't have to manually change the data to a wide format. You can use acast from the reshape2 library.
library(reshape2)
mat <- reshape2::acast(data = mydata, formula = id ~ week)
You can change the formula to obtain whichever rows and columns data is needed.

Superimposing two plots in R with same axis and limits

I have two plots from two different data frames
The DPUT from data frame 1 is as follows
ppv_npv2 <- structure(list(pred.prob = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 49, 50), variable = structure(c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L), .Label = c("ppv_2.5", "ppv_50", "ppv_97.5"), class = "factor"),
value = c(4.8, 9.3, 13.4, 17.2, 20.8, 24.2, 27.3, 30.3, 33.1,
35.7, 38.2, 40.5, 42.8, 44.9, 46.9, 48.8, 50.6, 52.3, 54,
55.6, 57.1, 58.5, 59.9, 61.2, 62.5, 63.7, 64.9, 66, 67.1,
68.2, 69.2, 70.2, 71.1, 72, 72.9, 73.8, 74.6, 75.4, 76.2,
76.9, 77.7, 78.4, 79, 79.7, 80.4, 81, 81.6, 82.2, 82.8, 83.3,
7.2, 13.6, 19.3, 24.4, 28.9, 33, 36.8, 40.2, 43.3, 46.2,
48.9, 51.3, 53.6, 55.7, 57.7, 59.6, 61.3, 62.9, 64.5, 65.9,
67.3, 68.6, 69.8, 70.9, 72, 73.1, 74.1, 75, 75.9, 76.8, 77.6,
78.4, 79.2, 79.9, 80.6, 81.3, 82, 82.6, 83.2, 83.8, 84.3,
84.8, 85.4, 85.9, 86.3, 86.8, 87.3, 87.7, 88.1, 88.5, 11.7,
21.1, 28.8, 35.3, 40.8, 45.5, 49.7, 53.3, 56.4, 59.3, 61.8,
64.1, 66.2, 68.1, 69.8, 71.4, 72.9, 74.2, 75.5, 76.6, 77.7,
78.7, 79.7, 80.5, 81.4, 82.2, 82.9, 83.6, 84.3, 84.9, 85.5,
86, 86.6, 87.1, 87.6, 88.1, 88.5, 88.9, 89.3, 89.7, 90.1,
90.5, 90.8, 91.1, 91.5, 91.8, 92.1, 92.4, 92.6, 92.9)),
.Names =c("pred.prob","variable", "value"), row.names = c(NA, -150L),
class = "data.frame")
The plot that i have created is from the following code
p1 <- ggplot(ppv_npv2,aes(x=pred.prob,y=value))+
geom_line(data=ppv_npv2[ppv_npv2$variable=="ppv_50",],
colour="red",linetype=2)+
geom_line(data=ppv_npv2[ ppv_npv2$variable=="ppv_2.5", ],
colour="blue",linetype=4)+
geom_line(data=ppv_npv2[ ppv_npv2$variable=="ppv_97.5", ],
colour="blue",linetype=4)+
theme_classic()+
ylab("Predicted positive predictive value (%) \n")+
xlab("\n Prevalence (%)")+
scale_x_continuous(limits=c(0,50),breaks=seq(0,50,2))+
scale_y_continuous(limits=c(0,100),breaks=seq(0,100,10), expand=c(0,0))+
theme(axis.text.x = element_text(size=12,hjust=.5,vjust=.8,face="plain"),
axis.text.y = element_text(size=12,hjust=.5,vjust=.8,face="plain"))+
theme(axis.title.x = element_text(size=14,face="bold"),
axis.title.y = element_text(size=14,face="bold"))
p1
The dput for the second data frame is
dat <- structure(list(PPV = c(57, 89, 19, 52, 52, 62, 63, 46, 31, 52,
54, 13, 17, 47, 48, 52, 96, 88, 64, 33, 62, 77, 75, 72), Prevalence = c(19,
35, 12, 16, 24, 6, 28, 13, 8, 19, 30, 6, 8, 20, 11, 25, 29, 55,
46, 13, 16, 22, 23, 20), total = c(939L, 323L, 306L, 703L, 137L,
833L, 360L, 317L, 440L, 2072L, 209L, 386L, 142L, 358L, 167L,
503L, 180L, 233L, 342L, 478L, 4870L, 1104L, 1813L, 1567L),
Author = structure(c(1L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 9L, 10L, 11L, 12L,
15L,18L, 19L, 8L, 14L, 16L, 17L, 21L, 20L, 20L, 13L, 10L),
.Label = c("Aldous",
"Bahrmann", "Body", "Christ ", "Collinson", "Eggers", "Freund",
"Giannitis", "Hammerer-Lercher", "Hoeller", "Inoue", "Invernizi",
"Keller", "Khan", "Lotze", "Melki ", "Normann", "Santalol", "Sebbane",
"Shah", "Thelin "), class = "factor"), Study.assay = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 1L, 1L), .Label = c("TnI", "TnT"), class = "factor")),
.Names = c("PPV", "Prevalence", "total", "Author", "Study.assay"),
class ="data.frame", row.names = c(NA, -24L))
And the plot from dataframe 2 is as follows
p2 <- ggplot(dat, aes(x=dat$Prevalence, y=dat$PPV, size=dat$total,
label=dat$Author),guide=F)+
geom_point(colour="white", fill="red", shape=21)+
scale_size_area(max_size = 10)+
scale_x_continuous(name="\n Prevalence", limits=c(0,100))+
scale_y_continuous(name="Predicted positive predictive value (%) \n",
limits=c(0,100))+
geom_text(size=2.5)+
theme_classic()+
ylab("Predicted positive predictive value (%) \n")+
xlab("\n Prevalence (%)")+
scale_x_continuous(limits=c(0,50),breaks=seq(0,50,2))+
scale_y_continuous(limits=c(0,100),breaks=seq(0,100,10), expand=c(0,0))+
theme(axis.text.x = element_text(size=12,hjust=.5,vjust=.8,face="plain"),
axis.text.y = element_text(size=12,hjust=.5,vjust=.8,face="plain"))+
theme(axis.title.x = element_text(size=14,face="bold"),
axis.title.y = element_text(size=14,face="bold"))+
theme(legend.position='none')
p2
As you can see both plots have the same axis and limits. I have two questions:
a) Can i overlay plot 2 onto plot 1?
b) Can i make the bubbles on plot 2 more transparent and choose colours by the factor dat$Study.assay (green and purple)?
Many thanks in advance - have spent a day researching this but no solution yet.
Here's a start using your data,
(plot2 <- ggplot() +
geom_line(data = ppv_npv2,aes(pred.prob, value,
group= variable, colour = variable)) +
geom_point(data = dat, aes(Prevalence, PPV, label=Author, size = total,
colour = Study.assay), alpha = I(0.4)) +
geom_text(data = dat, aes(Prevalence, PPV, label=Author,
size = total), size=3, hjust=-1, vjust=0)
)
It's not the orthodox ggplot2 way, but it's a start.

Resources