Emmeans continuous independant variable - r

I want to explan Type_f with Type_space of the experiment and the rate of Exhaustion_product and quantitative variable Age.
Here is my data :
res=structure(list(Type_space = 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, 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, 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,
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, 3L, 3L, 3L, 3L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, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("",
"29-v1", "29-v2", "88-v1", "88-v2"), class = "factor"), Id = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L,
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L,
81L, 82L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L,
13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L,
26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L,
52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L,
65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L,
78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L,
91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L,
103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L,
114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L,
125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L,
136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L,
147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L,
158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L,
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L,
81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L,
94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L,
17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L,
30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L,
43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L,
56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L,
69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L,
82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L,
95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L,
106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L,
117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L,
128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L,
139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L,
150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L,
161L, 162L, 163L, 164L), Age = c(3, 10, 1, 5, 4, 2, 1, 8, 2,
13, 1, 6, 3, 5, 2, 1, 3, 8, 3, 6, 1, 3, 7, 1, 2, 2, 2, 1, 2,
5, 4, 1, 6, 3, 6, 8, 2, 3, 4, 7, 3, 2, 6, 2, 3, 7, 1, 5, 4, 1,
4, 3, 2, 3, 5, 5, 2, 1, 1, 5, 8, 7, 2, 2, 4, 3, 4, 4, 2, 2, 10,
7, 5, 3, 3, 5, 7, 5, 3, 4, 5, 4, 1, 8, 6, 1, 12, 1, 6, 3, 4,
4, 13, 5, 2, 7, 7, 20, 1, 1, 1, 7, 1, 4, 3, 8, 2, 2, 4, 1, 1,
2, 3, 2, 2, 6, 11, 2, 5, 5, 9, 4, 4, 2, 7, 2, 7, 10, 6, 9, 2,
2, 5, 11, 1, 8, 8, 4, 1, 2, 14, 11, 13, 20, 3, 3, 4, 16, 2, 6,
11, 9, 11, 4, 5, 6, 19, 5, 2, 6, 1, 7, 11, 3, 9, 2, 3, 6, 20,
8, 6, 2, 11, 18, 9, 3, 7, 3, 2, 1, 8, 3, 5, 6, 2, 5, 8, 11, 4,
9, 7, 2, 12, 8, 2, 9, 5, 4, 15, 5, 13, 5, 10, 13, 7, 6, 1, 12,
12, 10, 4, 2, 16, 7, 17, 11, 18, 4, 3, 12, 1, 3, 7, 3, 6, 5,
11, 10, 12, 6, 14, 8, 6, 7, 8, 5, 10, 12, 6, 13, 3, 11, 14, 7,
9, 9, 4, 13, 4, 2, 1, 2, 2, 1, 7, 9, 3, 10, 3, 2, 1, 3, 1, 4,
2, 4, 5, 4, 2, 13, 4, 1, 3, 1, 11, 4, 1, 3, 3, 7, 5, 4, 5, 6,
1, 2, 1, 2, 1, 6, 1, 7, 6, 9, 5, 1, 6, 3, 2, 3, 3, 8, 8, 3, 2,
2, 4, 2, 5, 2, 6, 8, 11, 1, 6, 3, 3, 4, 5, 5, 7, 4, 2, 7, 3,
3, 1, 3, 9, 5, 2, 4, 12, 1, 4, 5, 2, 7, 6, 1, 2, 6, 4, 2, 7,
3, 5, 5, 3, 7, 1, 5, 2, 1, 15, 3, 5, 2, 5, 13, 6, 2, 3, 5, 2,
8, 4, 2, 6, 7, 2, 4, 1, 13, 8, 2, 1, 2, 1, 1, 5, 2, 1, 6, 11,
4, 1, 7, 7, 4, 3, 5, 1, 4, 10, 1, 2, 6, 1, 11, 3, 8, 9, 2, 6,
8, 11, 14, 16, 4, 1, 4, 2, 1, 10, 4, 9, 3, 12, 8, 11, 8, 8, 5,
1, 4, 13, 3, 8, 5, 14, 3, 5, 5, 12, 1, 3, 4, 5, 2, 7, 6, 9, 6,
10, 5, 2, 3, 2, 10, 10, 10, 10, 10, 1, 14, 3, 5, 9, 6, 2, 2,
2, 4, 4, 11, 14, 2, 2, 2, 8, 7, 2, 10, 12, 1, 6, 10, 2, 3, 5,
10, 6, 1, 8, 4, 11, 5, 4, 3, 6, 2, 4, 6, 9, 3, 9, 11, 7, 3, 15,
3, 7, 3, 5, 4, 6, 9, 13, 8, 5, 7, 8, 8, 5, 10), Type_product = c("f",
"s", "f", "f", "f", "f", "s", "c", "s", "f", "c", "f", "f", "f",
"s", "s", "f", "f", "c", "f", "s", "f", "f", "s", "f", "c", "f",
"f", "s", "f", "f", "c", "f", "c", "f", "f", "f", "f", "f", "c",
"c", "c", "f", "f", "c", "c", "f", "c", "c", "c", "c", "c", "s",
"f", "c", "c", "c", "s", "f", "c", "f", "f", "c", "c", "f", "c",
"c", "c", "f", "c", "c", "c", "c", "c", "c", "c", "f", "c", "c",
"c", "c", "f", "c", "f", "f", "s", "f", "c", "f", "f", "f", "c",
"f", "f", "f", "f", "f", "s", "c", "c", "f", "f", "c", "c", "f",
"f", "c", "c", "f", "f", "s", "f", "c", "c", "f", "f", "f", "c",
"f", "f", "f", "c", "f", "f", "f", "f", "f", "f", "c", "f", "f",
"f", "f", "c", "s", "f", "c", "f", "f", "c", "f", "f", "f", "c",
"f", "c", "c", "c", "f", "f", "f", "f", "c", "c", "c", "f", "f",
"c", "c", "f", "c", "f", "f", "c", "c", "c", "c", "f", "f", "f",
"c", "c", "c", "f", "c", "f", "c", "f", "f", "f", "c", "f", "c",
"c", "c", "c", "c", "f", "c", "c", "c", "c", "c", "c", "c", "f",
"f", "f", "c", "f", "c", "f", "f", "c", "c", "f", "f", "f", "c",
"c", "c", "f", "c", "c", "c", "c", "c", "f", "c", "f", "f", "c",
"c", "f", "c", "f", "c", "f", "c", "c", "c", "f", "c", "c", "c",
"c", "c", "c", "c", "f", "c", "c", "f", "c", "c", "f", "f", "c",
"f", "f", "s", "c", "s", "c", "f", "c", "c", "s", "c", "c", "s",
"c", "m", "c", "c", "f", "f", "f", "f", "f", "f", "s", "f", "f",
"c", "c", "f", "c", "f", "f", "f", "c", "f", "f", "f", "s", "f",
"f", "c", "f", "c", "f", "m", "c", "c", "c", "f", "s", "f", "f",
"f", "c", "s", "c", "m", "f", "c", "m", "c", "f", "c", "f", "f",
"f", "c", "m", "f", "c", "c", "f", "c", "f", "c", "c", "c", "c",
"c", "f", "f", "f", "c", "m", "f", "m", "m", "c", "c", "c", "c",
"m", "m", "c", "f", "m", "m", "m", "m", "m", "m", "m", "m", "m",
"c", "c", "f", "f", "f", "f", "c", "f", "m", "f", "f", "f", "c",
"f", "f", "f", "c", "f", "f", "c", "c", "f", "c", "f", "c", "m",
"f", "c", "f", "c", "f", "f", "f", "f", "c", "c", "f", "f", "c",
"c", "f", "f", "f", "f", "f", "f", "c", "f", "c", "c", "f", "c",
"f", "f", "f", "f", "f", "f", "f", "c", "f", "c", "f", "c", "f",
"c", "f", "c", "f", "f", "c", "c", "c", "c", "c", "f", "f", "f",
"c", "f", "c", "f", "f", "c", "c", "f", "f", "c", "f", "c", "f",
"c", "c", "c", "f", "f", "c", "f", "c", "c", "f", "c", "f", "c",
"f", "c", "f", "c", "m", "c", "c", "m", "c", "c", "f", "c", "c",
"f", "c", "c", "c", "f", "c", "c", "m", "c", "m", "m", "c", "c",
"f", "c", "c", "c", "c", "m", "c", "c", "c", "m", "m", "m", "c",
"c", "c", "c", "m", "m", "f", "m", "m", "m", "m", "m", "m", "m",
"m", "m", "m", "m", "m", "m", "m", "m"), Exhaustion_product = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 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, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L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, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L,
68L, 69L, 70L, 71L, 73L, 75L, 76L, 79L, 80L, 81L, 82L, 84L, 85L,
86L, 91L, 102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L,
111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L,
122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L,
133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L,
144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L,
155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L,
166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L,
177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L, 185L, 186L, 187L,
188L, 189L, 190L, 191L, 192L, 193L, 194L, 195L, 197L, 198L, 199L,
201L, 202L, 203L, 204L, 206L, 207L, 208L, 209L, 210L, 212L, 213L,
214L, 215L, 217L, 218L, 219L, 220L, 221L, 222L, 223L, 225L, 227L,
229L, 230L, 231L, 232L, 233L, 234L, 235L, 236L, 237L, 238L, 239L,
242L, 243L, 244L, 246L, 247L, 248L, 249L, 250L, 251L, 253L, 254L,
256L, 259L, 260L, 261L, 262L, 263L, 264L, 265L, 266L, 269L, 270L,
272L, 273L, 274L, 276L, 277L, 278L, 279L, 280L, 281L, 282L, 283L,
284L, 285L, 287L, 289L, 290L, 291L, 292L, 293L, 294L, 295L, 296L,
297L, 298L, 300L, 301L, 302L, 303L, 306L, 308L, 309L, 311L, 312L,
313L, 314L, 315L, 316L, 317L, 318L, 319L, 320L, 322L, 323L, 325L,
326L, 327L, 328L, 329L, 331L, 332L, 334L, 335L, 336L, 338L, 339L,
340L, 341L, 342L, 343L, 344L, 345L, 346L, 347L, 348L, 349L, 350L,
352L, 353L, 354L, 356L, 357L, 358L, 359L, 360L, 361L, 363L, 364L,
365L, 366L, 367L, 368L, 369L, 370L, 372L, 373L, 374L, 375L, 376L,
377L, 378L, 379L, 380L, 381L, 382L, 384L, 385L, 387L, 388L, 389L,
391L, 393L, 394L, 395L, 396L, 397L, 398L, 399L, 400L, 401L, 402L,
404L, 407L, 408L, 409L, 411L, 412L, 413L, 414L, 415L, 416L, 417L,
418L, 419L, 420L, 421L, 422L, 423L, 424L, 425L, 426L, 427L, 428L,
429L, 430L, 431L, 432L, 433L, 434L, 435L, 436L, 437L, 438L, 439L,
440L, 442L, 443L, 444L, 445L, 446L, 447L, 448L, 449L, 450L, 451L,
452L, 453L, 454L, 455L, 456L, 457L, 458L, 459L, 460L, 461L, 462L,
463L, 464L, 465L, 466L, 467L, 468L, 469L, 470L, 471L, 472L, 473L,
474L, 476L, 477L, 478L, 479L, 480L, 481L, 482L, 483L, 484L, 486L,
487L, 488L, 489L, 490L, 491L, 492L, 493L, 494L, 495L, 496L, 497L,
498L, 500L, 501L, 502L, 503L, 504L, 505L, 506L, 507L, 508L, 509L,
510L, 511L, 512L, 513L, 514L, 515L, 516L, 517L, 518L, 519L, 520L,
521L, 522L, 523L, 524L, 525L, 526L, 527L, 528L, 529L, 530L, 531L,
532L, 534L, 535L, 536L, 537L, 538L, 539L, 540L, 541L, 542L, 543L,
547L, 548L, 550L, 551L, 552L, 553L, 554L, 555L, 556L, 557L, 558L,
559L, 560L, 561L, 562L, 563L, 565L, 566L, 567L, 568L, 569L, 570L,
571L, 572L, 573L, 575L, 577L, 579L, 580L, 581L, 582L, 583L, 585L,
586L, 587L, 590L, 592L, 599L, 606L, 608L), class = "data.frame")
an=Anova(glm(Type_f ~ Type_space + Exhaustion_product + Age , family=binomial,data=res))
gl=glm(Type_f ~ Type_space + Exhaustion_product + Age , family=binomial,data=res)
library("emmeans")
emmp <- emmeans( gl, pairwise ~ Exhaustion_product + Age)
summary( emmp, infer=TRUE)
(1) In the case of categorical variable the results are clear. But in the case of Age which is significant in the GLM, what is the value generated in the emmeans ?5.455426.Is that is means ? How can I interpret this ?
(0,10] 5.455426 0.36901411 0.2935894 Inf -0.20641061 0.94443883 1.257 0.2088
(2)I want to generate graphic representationof the interaction age and Exhaustion_product. Also this do not make sens.
emmip(gl, Exhaustion_product ~ Age)
Edit 1
Contrast result
$contrasts
contrast estimate SE df asymp.LCL asymp.UCL z.ratio p.value
(0,10],5.45542635658915 - (10,20],5.45542635658915 0.33231353 0.4078967 Inf -0.95814279 1.6227698 0.815 0.9984
(0,10],5.45542635658915 - (20,30],5.45542635658915 -0.53694399 0.4194460 Inf -1.86393835 0.7900504 -1.280 0.9582
(0,10],5.45542635658915 - (30,40],5.45542635658915 -0.16100309 0.4139472 Inf -1.47060101 1.1485948 -0.389 1.0000
(0,10],5.45542635658915 - (40,50],5.45542635658915 0.40113723 0.4021403 Inf -0.87110757 1.6733820 0.998 0.9925
(0,10],5.45542635658915 - (50,60],5.45542635658915 0.60576562 0.4106536 Inf -0.69341247 1.9049437 1.475 0.9022
(0,10],5.45542635658915 - (60,70],5.45542635658915 1.38800301 0.4319258 Inf 0.02152631 2.7544797 3.214 0.0430
(0,10],5.45542635658915 - (70,80],5.45542635658915 1.01677522 0.4147441 Inf -0.29534399 2.3288944 2.452 0.2952
(0,10],5.45542635658915 - (80,90],5.45542635658915 1.99085692 0.4747929 Inf 0.48876247 3.4929514 4.193 0.0011
(0,10],5.45542635658915 - (90,100],5.45542635658915 2.03923289 0.4745872 Inf 0.53778910 3.5406767 4.297 0.0007

Because this question seems like a self-learning one, I am going to do a similar example, not the same data. But the structure is the same, with one factor and one covariate as predictors.
The example is the emmeans::fiber dataset. Its response variable is fiber strength, the continuous predictor is the diameter, and the factor is the machine it was made on.
Model:
> mod = glm(log(strength) ~ machine + diameter, data = fiber)
> summary(mod)
... (output has been abbreviated) ...
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3.124387 0.068374 45.695 6.74e-14
machineB 0.026025 0.023388 1.113 0.290
machineC -0.044593 0.025564 -1.744 0.109
diameter 0.023557 0.002633 8.946 2.22e-06
(Dispersion parameter for gaussian family taken to be 0.001356412)
Analysis with emmeans is based on the reference grid, which by default consists of all levels of the factor and the mean of the covariate:
> ref_grid(mod)
'emmGrid' object with variables:
machine = A, B, C
diameter = 24.133
Transformation: “log”
You can confirm in R that mean(fiber$diameter) is 24.133. I emphasize this is the mean of the diameter values, not of anything in the model.
> summary(.Last.value)
machine diameter prediction SE df
A 24.13333 3.692901 0.01670845 Inf
B 24.13333 3.718925 0.01718853 Inf
C 24.13333 3.648307 0.01819206 Inf
Results are given on the log (not the response) scale.
Those summary values are the predictions from mod at each combination of machine and diameter. Now look at EMMs for machine
> emmeans(mod, "machine")
machine emmean SE df asymp.LCL asymp.UCL
A 3.692901 0.01670845 Inf 3.660153 3.725649
B 3.718925 0.01718853 Inf 3.685237 3.752614
C 3.648307 0.01819206 Inf 3.612652 3.683963
Results are given on the log (not the response) scale.
Confidence level used: 0.95
... we get exactly the same three predictions. But if we look at diameter:
> emmeans(mod, "diameter")
diameter emmean SE df asymp.LCL asymp.UCL
24.13333 3.686711 0.009509334 Inf 3.668073 3.705349
Results are averaged over the levels of: machine
Results are given on the log (not the response) scale.
Confidence level used: 0.95
... we get the EMM is equal to the average of the three predicted values in the reference grid. And note that it says in the annotations that results were averaged over machine, so it is worth reading that.
To get a graphical representation of the model results, we can do
> emmip(mod, machine ~ diameter, cov.reduce = range)
The argument cov.reduce = range is added to cause the reference grid to use the min and max diameter, rather than its average. Without that, we'd have gotten three dots instead of three lines. This plot still shows the model predictions, just over a more detailed grid of values. Notice that all three lines have the same slope. That is vbecause the model was specified that way: the diameter effect is added to the machine effect. Each line thus has the common slope of 0.023557 (see the output from summary(mod).
There is no post hoc test needed for diameter, since its one effect is already tested in summary(mod).
One last thing. The model used log(strength) as the response. If we want the EMMs on the same scale as strength, just add type = "response":
> emmeans(mod, "machine", type = "response")
machine response SE df asymp.LCL asymp.UCL
A 40.16118 0.6710311 Inf 38.86728 41.49815
B 41.22008 0.7085126 Inf 39.85455 42.63239
C 38.40960 0.6987496 Inf 37.06421 39.80384
Confidence level used: 0.95
Intervals are back-transformed from the log scale
Again, the annotations below the results help explain the output.

Related

Plotting Piecewise growth curves

I am trying to plot a piecewise growth curve similar to this first plot. I used the separate slopes coding scheme and placed a breakpoint at time 2
| time | 0 | 1 | 2 | 5 | 10 | 15 | 20|
| time1 | 0 | 1 | 2 | 2 | 2 | 2 | 2 |
| time2 | 0 | 0 | 0 | 1 | 2 | 3 | 4 |
I used the following code to create my growth model
m1 <- lmer(sdmtwr ~ time1 + time2 + (time1 | id) + (0 + time2 | id), data = SDMT, REML = FALSE)
I'm also exploring an interaction with a 2-level categorical predictor with the following code
m2 <- lmer(sdmtwr ~ (time1 + time2)*edu + (time1 | id) + (0 + time2 | id), data = SDMT, REML = FALSE)
I've attempted to create the plots with the ggplot2, sjPlot, and effects packages to no avail, and I am at a loss due to limited programming experience. I have only ever been able to plot segments separately for both the baseline and interaction models.
If anyone could provide assistance on the appropriate code, I would appreciate it!
Edit: Here is the dput summary (edited for length to show edu, time1, and time2)
> dput(sdmt)
structure(list(id = c(3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 6L,
6L, 6L, 28L, 28L, 28L, 28L, 28L, 28L, 28L, 62L, 62L, 62L, 62L,
108L, 108L, 108L, 108L, 119L, 119L, 120L, 120L, 120L, 120L, 132L,
132L, 132L, 132L, 132L, 148L, 148L, 148L, 148L, 148L, 148L, 175L,
175L, 175L, 178L, 178L, 178L, 178L, 201L, 201L, 201L, 201L, 201L,
201L, 201L, 253L, 253L, 253L, 253L, 327L, 327L, 327L, 327L, 336L,
336L, 336L, 336L, 336L, 336L, 343L, 343L, 360L, 360L, 360L, 366L,
366L, 366L), time = c(0L, 2L, 10L, 15L, 20L, 5L, 10L, 15L, 2L,
2L, 15L, 20L, 0L, 1L, 2L, 5L, 10L, 15L, 20L, 5L, 10L, 15L, 20L,
0L, 2L, 15L, 20L, 0L, 2L, 0L, 10L, 15L, 20L, 0L, 1L, 5L, 10L,
20L, 1L, 2L, 5L, 10L, 15L, 20L, 0L, 1L, 2L, 0L, 1L, 2L, 5L, 0L,
1L, 2L, 5L, 10L, 15L, 20L, 0L, 1L, 5L, 15L, 0L, 1L, 10L, 20L,
0L, 1L, 5L, 10L, 15L, 20L, 0L, 10L, 1L, 5L, 10L, 0L, 10L, 15L
), sdmtwr = c(20L, 24L, 18L, 19L, 9L, 17L, 24L, 17L, 41L, 33L,
27L, 29L, 31L, 29L, 26L, 29L, 32L, 20L, 19L, 40L, 42L, 46L, 38L,
14L, 25L, 24L, 29L, 46L, 45L, 29L, 26L, 34L, 38L, 30L, 33L, 71L,
52L, 51L, 29L, 33L, 50L, 55L, 40L, 39L, 32L, 34L, 35L, 28L, 37L,
37L, 36L, 37L, 29L, 52L, 51L, 50L, 44L, 42L, 30L, 43L, 43L, 41L,
33L, 46L, 49L, 38L, 52L, 50L, 48L, 49L, 49L, 50L, 40L, 39L, 18L,
NA, 3L, 31L, 43L, 47L), time_seg1 = c(0, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 0, 2,
0, 2, 2, 2, 0, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 0, 1, 2, 0, 1, 2,
2, 0, 1, 2, 2, 2, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2, 2,
2, 0, 2, 1, 2, 2, 0, 2, 2), time_seg2 = c(0, 0, 2, 3, 4, 1, 2,
3, 0, 0, 3, 4, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 0, 0, 3, 4, 0,
0, 0, 2, 3, 4, 0, 0, 1, 2, 4, 0, 0, 1, 2, 3, 4, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 1, 2, 3, 4, 0, 0, 1, 3, 0, 0, 2, 4, 0, 0, 1, 2,
3, 4, 0, 2, 0, 1, 2, 0, 2, 3), ed_dich = structure(c(2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, NA, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 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, 1L, 1L, 1L), .Label = c("< HS",
">= HS"), class = "factor")), row.names = c(NA, -80L), class = "data.frame")
What I think you want is a piecewise linear spline. You can do this with a truncated power basis function. In your model, you would include time and a function that is time-2 if time is greater than 2 and 0 otherwise. This makes a piecewise linear function that meet each other at time=2. You can do this in the model as follows:
library(lme4)
mod <- lmer(sdmtwr ~ time + I(ifelse(time > 2, time-2, 0)) +
(1 |id), data=tmp, REML=TRUE)
Then, you could use the ggpredict() function from the ggeffects package to produce the plot:
library(ggeffects)
g <- ggpredict(mod, "time")
plot(g)
Note: I couldn't get it to run with random effects on the time variables, but with more data perhaps you'll be able to get it to work.

change data type by indexing

I want to change some columns to integers (from the 4 column of my table to the last one). How can I do that?
My data
I already tried
databianca %>%
mutate_if(is.factor, ~as.integer(as.character(.)))
and
databianca %>%
mutate_at(vars(columbia:escrita.POS), ~as.integer(as.character(.)))
and with both of them I get these warnings:
Warning messages:
1: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
2: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
3: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
4: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
5: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
6: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
7: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
8: In (function (..., .x = ..1, .y = ..2, . = ..1) :
NAs introduced by coercion
Isn't there an option to treat all columns from columbia to the last column (in matlab I would use 4:end to give the instructions from the 4th column until the last element) as integrals?
my data via dpasta:
databianca <- data.frame(
SEXO = as.factor(c("F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "F", "F", "F", "F",
"F", "F", "F", "F", "F", "M", "M", "M", "M",
"M", "M", "M", "M", "M", "M", "M", "M", "M", "M",
"M", "M", "M", "M", "M", "M", "M", "M", "M",
"M", "M", "M", "M")),
GRUPOS = as.factor(c("C", "E", "E", "C", "C", "C", "E", "E", "E",
"C", "E", "E", "E", "C", "C", "C", "E", "C",
"E", "E", "C", "E", "C", "C", "C", "E", "E",
"E", "C", "E", "C", "C", "C", "E", "C", "E", "C",
"E", "E", "C", "C", "C", "E", "E", "E", "E",
"C", "C", "C", "C")),
SALAS = as.factor(c("B", "A", "C", "A", "A", "C", "A", "B", "C",
"C", "C", "B", "A", "C", "A", "C", "A", "C",
"C", "C", "B", "B", "B", "A", "B", "C", "A",
"B", "A", "A", "C", "B", "B", "A", "A", "B", "B",
"B", "C", "B", "B", "B", "B", "A", "A", "B",
"C", "C", "A", "B")),
token = c(100L, 95L, 106L, 112L, 84L, 125L, 108L, 114L, 112L,
117L, 84L, 96L, 124L, 118L, 99L, 84L, 95L, 113L, 123L,
135L, 100L, 92L, 101L, 129L, 93L, 106L, 101L, 84L, 84L,
128L, 113L, 95L, 109L, 127L, 112L, 108L, 108L, 118L, 97L,
112L, 116L, 88L, 92L, 111L, 123L, 112L, 116L, 104L, 90L,
89L),
hab.visuo = c(3L, 3L, 2L, 0L, 0L, 1L, 3L, 3L, 2L, 1L, 1L, 1L, 0L, 2L,
1L, 1L, 2L, 2L, 0L, 7L, 3L, 2L, 0L, 2L, 0L, 3L, 1L, 0L,
1L, 1L, 1L, 1L, 0L, 2L, 2L, 1L, 1L, 0L, 0L, 0L, 1L, 2L,
1L, 1L, 0L, 2L, 0L, 0L, 3L, 3L),
tamanho = c(7L, 4L, 8L, 7L, 7L, 8L, 8L, 8L, 8L, 7L, 7L, 7L, 8L, 8L,
8L, 6L, 7L, 7L, 8L, 8L, 8L, 7L, 7L, 8L, 8L, 8L, 7L, 7L,
7L, 7L, 8L, 8L, 7L, 8L, 7L, 8L, 8L, 7L, 7L, 7L, 8L, 8L,
8L, 8L, 8L, 7L, 8L, 8L, 7L, 7L),
forma = c(6L, 6L, 7L, 4L, 6L, 6L, 6L, 7L, 6L, 6L, 6L, 6L, 6L, 5L,
6L, 7L, 5L, 6L, 7L, 7L, 6L, 7L, 0L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 2L, 2L, 6L, 7L, 7L, 6L, 7L, 7L,
6L, 7L, 6L, 6L, 6L, 6L, 6L, 7L),
nomeou.cor = c(67L, 82L, 85L, 83L, 68L, 72L, 74L, 74L, 80L, 80L, 63L,
70L, 80L, 81L, 68L, 72L, 81L, 76L, 78L, 81L, 81L, 80L,
65L, 78L, 74L, 75L, 61L, 63L, 69L, 79L, 70L, 75L, 77L,
79L, 74L, 78L, 70L, 79L, 71L, 70L, 78L, 53L, 67L, 75L, 83L,
82L, 84L, 71L, 53L, 69L),
nomeou.incor = c(23L, 8L, 5L, 7L, 22L, 18L, 16L, 16L, 10L, 10L, 27L,
20L, 10L, 9L, 22L, 18L, 9L, 14L, 12L, 9L, 8L, 10L, 25L,
12L, 16L, 15L, 29L, 27L, 21L, 11L, 20L, 15L, 13L, 11L,
16L, 12L, 20L, 11L, 19L, 20L, 12L, 37L, 23L, 15L, 15L, 8L,
6L, 19L, 38L, 21L),
token.POS = c(120L, 120L, 136L, 117L, 122L, 118L, 135L, 122L, 124L,
112L, 98L, 127L, 130L, 119L, 116L, 94L, 122L, 125L,
130L, 123L, 113L, 116L, 105L, 109L, 113L, 120L, 99L, 87L,
0L, 123L, 0L, 94L, 136L, 125L, 118L, 124L, 123L, 128L,
101L, 124L, 131L, 87L, 116L, 120L, 117L, 119L, 123L, 127L,
91L, 105L),
hab.visuo.POS = c(1L, 2L, 2L, 1L, 2L, 5L, 2L, 1L, 3L, 0L, 2L, 2L, 1L, 2L,
5L, 1L, 0L, 0L, 0L, 5L, 0L, 2L, 0L, 1L, 0L, 2L, 3L, 1L,
0L, 1L, 0L, 1L, 0L, 3L, 1L, 4L, 1L, 0L, 2L, 0L, 1L, 0L,
0L, 2L, 3L, 2L, 7L, 1L, 2L, 3L),
tamanho.POS = c(8L, 8L, 8L, 7L, 8L, 7L, 7L, 8L, 8L, 8L, 7L, 8L, 8L, 8L,
8L, 7L, 8L, 8L, 8L, 7L, 8L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
0L, 7L, 0L, 7L, 8L, 8L, 7L, 7L, 7L, 8L, 7L, 8L, 7L, 7L,
8L, 6L, 8L, 7L, 8L, 7L, 8L, 7L),
forma.POS = c(6L, 7L, 7L, 7L, 6L, 7L, 7L, 7L, 6L, 5L, 7L, 6L, 6L, 7L,
5L, 5L, 7L, 5L, 7L, 8L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 6L,
0L, 7L, 0L, 5L, 7L, 7L, 7L, 7L, 2L, 7L, 5L, 5L, 5L, 7L,
6L, 7L, 7L, 6L, 6L, 7L, 2L, 6L),
nomeou.cor.POS = c(78L, 78L, 85L, 73L, 70L, 76L, 77L, 78L, 82L, 74L, 56L,
70L, 79L, 78L, 65L, 67L, 87L, 78L, 82L, 82L, 79L, 84L,
74L, 78L, 71L, 79L, 61L, 70L, 0L, 75L, 0L, 74L, 83L, 77L,
76L, 77L, 75L, 77L, 68L, 79L, 79L, 61L, 67L, 76L, 79L,
78L, 81L, 68L, 53L, 71L),
nomeou.incor.POS = c(12L, 12L, 5L, 17L, 20L, 14L, 13L, 12L, 8L, 16L, 34L,
20L, 11L, 12L, 25L, 23L, 3L, 12L, 8L, 8L, 11L, 6L, 16L,
12L, 19L, 11L, 29L, 20L, 0L, 15L, 0L, 16L, 7L, 13L, 14L,
13L, 15L, 13L, 22L, 11L, 11L, 29L, 23L, 14L, 11L, 12L,
9L, 22L, 37L, 19L),
columbia = as.factor(c("49,3", "55,38", "67,69", "53,33", "38,46",
"53,84", "63,07", "61,53", "63,07", "35,38",
"43,33", "38,33", "55,38", "67,69", "47,69",
"29,23", "60", "53,84", "61,66", "73,33", "47,
69", "46,66", "41,66", "56,66", "47,69", "61,66",
"33,33", "40", "60", "36,66", "51,66", "43,33",
"58,46", "60", "56,92", "38,33", "40", "60",
"53,33", "56,92", "61,53", "53,84", "44,61",
"47,69", "44,61", "66,15", "56,66", "58,33", "65",
"51,66")),
escrita = as.factor(c("2", "2", "2", "2", "2", "1,5", "2,25", "2",
"2", "2", "2", "2", "2", "2,75", "2", "2,5",
"1,75", "2", "3", "4,5", "1,75", "1,75", "2",
"2", "2", "2", "1", "1,5", "2", "2", "2", "2,5",
"2", "2", "2", "1,75", "1,75", "2", "2", "2,
25", "2", "2", "1,75", "1", "2", "0", "2,5", "2",
"1,25", "1,5")),
columbia.POS = as.factor(c("53,8", "66,7", "76,7", "31,7", "43,1", "58,
5", "56,7", "55", "70", "35,4", "28,3", "43,
3", "61,7", "68,3", "55,4", "28,3", "55", "56,7",
"68,3", "66,7", "41,5", "56,7", "50", "65",
"58,5", "75", "48,3", "31,7", "0", "53,3", "0",
"50", "65", "66,7", "73,8", "51,7", "45", "66,
7", "48,3", "69,2", "73,8", "43,3", "56,7", "47,
7", "51,7", "73,3", "66,7", "38,3", "43,3", "33,
3")),
escrita.POS = as.factor(c("2", "3", "3", "1,5", "1,5", "2", "3", "3",
"4", "2", "1,5", "3", "2", "4", "1,5", "3",
"2", "3", "4", "4", "1,5", "2", "3", "1,5", "2",
"2", "1,5", "1,5", "0", "1,5", "0", "2", "3",
"1,5", "1,5", "1,5", "1,5", "2", "1,5", "3",
"2", "2", "2", "1,5", "1,5", "1,5", "5", "3", "1,
5", "1,5"))
)
This happens when you use as.integer on non-integer variables.
It seems your numbers have "," in them (ex: 51,66 ) and also new line character which is creating problem so first make your factors "clean" with gsub() and then run as.integer()
databianca[,4:length(names(databianca))] <- apply(databianca[, 4:length(names(databianca))], 2, function(x) as.integer(gsub("[\n,]", "", x)))
This should also work
databianca[,4:length(names(databianca))] <- as.integer(gsub("[\n,]", "", as.matrix(databianca[,4:length(names(databianca))])))

Inconsistent predictions from predict.gbm() 2.1.4 vs 2.1.3

This question is related to my earlier post here.
I have tracked down the problem and it seems to be related to which version of gbm I use. The latest version, 2.1.4 exhibits the problem on my system (R 3.4.4 and also 3.5; both on Ubuntu 18.04) whereas version 2.1.3 works as expected:
mydata <- structure(list(Count = c(1L, 3L, 1L, 4L, 1L, 0L, 1L, 2L, 0L, 0L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 0L, 2L, 3L, 1L, 4L, 3L, 0L, 4L, 1L, 2L, 1L, 1L, 0L, 2L, 1L, 4L, 1L, 5L, 3L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 2L, 0L, 0L, 1L, 1L, 1L, 0L, 3L, 1L, 1L, 0L, 3L, 1L, 1L, 1L, 1L, 2L, 3L, 2L, 2L, 0L, 0L, 3L, 5L, 1L, 2L, 1L, 1L, 0L, 0L, 1L, 2L, 1L, 3L, 1L, 1L, 0L, 2L, 2L, 1L, 3L, 3L, 2L, 0L, 0L, 1L, 2L, 1L, 0L, 2L, 0L, 0L, 4L, 4L, 2L), Treat1 = structure(c(10L, 14L, 8L, 2L, 3L, 12L, 1L, 10L, 6L, 2L, 11L, 11L, 15L, 1L, 8L, 3L, 13L, 9L, 9L, 11L, 1L, 8L, 14L, 5L, 10L, 8L, 15L, 11L, 7L, 6L, 13L, 11L, 7L, 1L, 1L, 2L, 7L, 12L, 5L, 1L, 8L, 1L, 9L, 8L,12L, 14L, 12L, 7L, 8L, 14L, 3L, 3L, 5L, 1L, 1L, 11L, 6L, 5L, 5L, 13L, 9L, 3L, 8L, 9L, 13L, 9L, 7L, 9L, 2L, 6L, 10L, 3L, 11L, 4L, 3L, 15L, 12L, 6L, 4L, 3L, 8L, 8L, 11L, 1L, 11L, 2L, 11L, 5L, 12L, 6L, 8L, 14L, 1L, 9L, 9L, 10L, 10L, 5L, 14L, 3L), .Label = c("D", "U", "R", "E", "C", "Y", "L", "O", "G", "T", "N", "J", "V", "X", "A"), class = "factor"), Treat2 = structure(c(15L, 13L, 7L, 8L, 2L, 5L, 15L, 4L, 2L, 7L, 6L, 2L, 3L, 14L, 10L, 7L, 7L, 14L, 11L, 7L, 6L, 1L, 5L, 13L, 11L, 6L, 10L, 5L, 3L, 1L, 7L, 9L, 6L, 10L, 5L, 11L, 15L, 9L, 7L, 11L, 10L, 2L, 3L, 3L, 5L, 11L, 8L, 6L,4L, 5L, 15L, 8L, 8L, 2L, 2L, 10L, 4L, 1L, 10L, 11L, 10L, 8L, 7L, 7L, 8L, 14L, 16L, 11L, 10L, 9L, 3L, 15L, 13L, 1L, 11L, 11L, 9L, 7L, 10L, 9L, 3L, 7L, 5L, 13L, 3L, 14L, 10L, 10L, 15L, 13L, 15L, 12L, 14L, 11L, 5L, 4L, 2L, 3L, 11L, 10L), .Label = c("B", "X", "R", "H", "L", "D", "U", "Q", "K", "C", "T", "V", "J", "E", "F", "A"), class = "factor"), Near = c(0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0), Co1 = c(2, 5, 1, 1, 0, 1, 1, 2, 1, 2, 5, 2, 1, 0, 1, 2, 6, 3, 3, 1, 2, 2, 3, 0, 1, 0, 1, 0, 2, 1, 0, 1, 2, 3, 1, 2, 2, 0, 0, 2, 3, 3, 1, 1, NA, 2, 0, 2, 1, NA, 1, 1, 0, 1, 2, 0, 2, 1, 1, 1, 2, 3, 1, 0, 4, 0, 0, 0, 2, 2, 1, 1,2, 0, 1, 2, 1, 0, 0, 0, 0, 2, 1, 2, 2, 2, 2, 1, 0, 1, 1, 1, 1, 1, 0, 2, 0, 0, 5, 1), Co2 = c(1, 1, 2, 2, 4, 1, 3, 0, 5, 2, 2, 4, 1, 1, 2, 1, 2, 3, 0, 2, 3, 3, 0, 3, 1, 0, 1, 1, 1, 2, 0, 1, 1, 1, 2, 3, 2, 2, 3, 0, 0, 0, 1, 2, NA, 1, 1, 1, 0, 2, 1, 1, 2, 5, 0, 2, 1, 4, 1, 1, 3, 0, 1, 1, 1, 1, NA, 0, 2, 1, 1, 3, 2, 1, 2, 1, 3, 1, 2, 0, 1, 5, 2, 2, 1, 2, 3, 4, 3, 1, 1, 0, 5, 1, 1, 0, 1, 1, 2, 0)), .Names = c("Count", "Treat1", "Treat2", "Near", "Co1", "Co2"), row.names = c(1759L, 959L, 1265L, 1504L, 630L, 1905L, 1885L, 1140L, 1187L, 1792L, 1258L, 1125L, 756L, 778L, 1718L, 1797L, 388L, 715L, 63L, 311L, 1492L, 1128L, 629L, 536L, 503L, 651L, 1684L, 1893L, 721L, 1440L, 1872L, 1444L, 1593L, 143L, 1278L, 1558L, 1851L, 1168L, 1829L, 386L, 365L, 849L, 429L, 155L, 11L, 1644L, 101L, 985L, 72L, 459L, 1716L, 844L, 1313L, 77L, 1870L, 744L, 219L, 513L, 644L, 831L, 338L, 284L, 211L, 1096L,243L, 1717L, 1881L, 1784L, 1017L, 992L, 45L, 707L, 489L, 1267L, 1152L, 1819L, 995L, 510L, 1350L, 1700L, 56L, 1754L, 725L, 1625L, 319L, 1818L, 1287L, 1634L, 953L, 1351L, 1787L, 923L, 917L, 484L, 886L, 390L, 1531L, 679L, 1811L, 1736L), class = "data.frame")
detach("package:gbm", unload = TRUE )
remove.packages("gbm")
require(devtools)
install_version("gbm", version = "2.1.3")
set.seed(12345)
require(gbm)
n.trees <- 10000
m1.gbm <- gbm(Count ~ Treat1 + Treat2 + Near + Co1 + Co2, data = mydata, distribution = "poisson", n.trees = n.trees)
head(predict(m1.gbm, newdata = mydata, n.trees = n.trees, type = "response"))
[1] 0.8620154 2.8210216 0.8800267 3.7808341 0.4749737 0.3716022
predict(m1.gbm, newdata = head(mydata), n.trees = n.trees, type = "response")
[1] 0.8620154 2.8210216 0.8800267 3.7808341 0.4749737 0.3716022
...as expected. However,
detach("package:gbm", unload = TRUE )
remove.packages("gbm")
install.packages("gbm", dependencies = TRUE)
# I had to restart R after this, otherwise the following line failed with:
# Loading required package: gbm
# Error: package or namespace load failed for ‘gbm’ in get(method, envir = home):
# lazy-load database '/home/container/R/x86_64-pc-linux-gnu-library/3.5/gbm/R/gbm.rdb' is corrupt
require(gbm)
m1.gbm <- gbm(Count ~ Treat1 + Treat2 + Near + Co1 + Co2, data = mydata, distribution = "poisson", n.trees = n.trees)
head(predict(m1.gbm, newdata = mydata, n.trees = n.trees, type = "response"))
[1] 0.7524109 2.8789957 0.7843470 4.1724821 0.4525449 0.2036923
predict(m1.gbm, newdata = head(mydata), n.trees = n.trees, type = "response")
[1] 2.2216079 1.2806235 0.9109426 2.2842149 2.4828922 0.6124778
...which exhibits the problem in my earlier post.
I find this quite surprising since gbm is a well-known package, although I see that the vignette was update last month, so perhaps the latest version was only recently released. I was unable to find the exact date from here. What is the best way to proceed here ?

ddply function based on two variables

How can I modify the below code to calculate the average of time per ID in each level of Type.
df <- structure(list(ID = structure(c(1L, 2L, 2L, 3L, 3L, 4L, 4L, 4L,
4L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 5L, 6L), .Label = c("R1", "R2",
"R3", "R4", "R5", "R6"), class = "factor"), cost = c(123L, 12L,
14L, 15L, 16L, 17L, 18L, 9L, 6L, 6L, 7L, 8L, 1L, 111L, 121L,
141L, 181L, 1611L), Time = c(123L, -12L, NA, -15L, NA, 17L, 18L,
-9L, 88L, 666L, 7L, 78L, 188L, 111L, 121L, 141L, 189L, 161L),
number = c(14L, 12L, 15L, 17L, NA, 17L, 22L, 95L, NA, 67L,
7L, 8L, 7L, 118L, NA, 140L, 180L, 1611L),type = c("A", "A", "B", "B", "B", "C", "C",
"M", "A", "M", "D", "D", "C", "A", "D", "B", "B", "M")), .Names = c("ID",
"cost", "Time", "number","type"), row.names = c(NA, -18L), class = "data.frame")
library(dplyr)
df %>%
group_by(id) %>%
summarise(N.Time = sum(!is.na(Time)),
Time_Average = round(mean(Time[Time >=0 & !is.na(Time)]), 2))

Counting then sum particular values?

I have a data as below ( just a part of my data)
month NumberOfMonths
Jan 4
Jan 3
Feb 2
May 1
Jan 4
Feb 1
May 2
Mar 12
Feb 2
May 1
So I want to create a data frame as below
Month NumberOfMonths
1 2 3 4 5 6 7 8 9 10 11 12
Jan 0 0 1 2 0 0 0 0 0 0 0 0
Feb 1 2 0 0 0 0 0 0 0 0 0 0
Mar 0 0 0 0 0 0 0 0 0 0 0 1
Apr 0 0 0 0 0 0 0 0 0 0 0 0
May 2 1 0 0 0 0 0 0 0 0 0 0
As you see above, the function will count the same number of month and will assign to corresponding month. For example, if i have two 4 in NumberOfMonths, in my data frame january for NumberOfMonths 4 will be 2.
By the way class of Month is factor not date.
can anyone help me please?
I tried every function all you gave. However, I could not get the same result. I pasted my data output. If you wouldn't mind my can you help again?
structure(list(Month = structure(c(4L, 5L, 5L, 12L, 5L, 5L, 2L,
12L, 11L, 10L, 7L, 9L, 7L, 4L, 8L, 7L, 5L, 12L, 12L, 5L, 5L,
5L, 5L, 5L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, 2L, 5L, 9L, 4L, 6L, 5L, 5L,
8L, 11L, 5L, 8L, 7L, 12L, 7L, 6L, 8L, 9L, 9L, 6L, 11L, 12L, 5L,
4L, 4L, 1L, 1L, 5L, 5L, 2L, 4L, 9L, 4L, 1L, 1L, 8L, 1L, 1L, 5L,
2L, 8L, 6L, 1L, 9L, 5L, 10L, 6L, 2L, 12L, 5L, 5L, 10L, 5L, 8L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 9L, 7L, 5L, 12L, 11L, 7L,
5L, 5L, 5L, 12L, 11L, 11L, 10L, 5L, 5L, 5L, 12L, 12L), .Label = c("Apr",
"Aug", "Dec", "Feb", "Jan", "Jul", "Jun", "Mar", "May", "Nov",
"Oct", "Sep"), class = "factor"), NumberOfMonth = c(1, 12.0000000000009,
1, 1, 12.0000000000009, 12.0000000000009, 1, 1.99999999999909,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3.00000000000091,
3.00000000000091, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 10.9999999999991,
1, 1, 1, 1, 1.99999999999909, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1,
3.00000000000091, 1, 4, 12.0000000000009, 1, 1, 12.0000000000009,
12.0000000000009, 12.0000000000009, 4, 1, 12.0000000000009, 12.0000000000009,
1, 12.0000000000009, 1, 7.99999999999909, 4, 12.0000000000009,
1, 1, 4, 4, 12.0000000000009, 1, 1.99999999999909, 1, 1, 1, 1.99999999999909,
1, 1, 1, 1, 1, 1, 1, 1.99999999999909, 1, 1, 1, 1, 1, 1, 1, 12.0000000000009,
1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 4, 7, 12.0000000000009, 1, 1,
1, 12.0000000000009, 1, 12.0000000000009, 12.0000000000009, 1,
12.0000000000009, 4.99999999999909, 6.00000000000091, 1, 10.9999999999991,
1, 1.99999999999909, 1, 2.99999999999818, 1, 1, 1, 1, 4, 1.99999999999909,
12.0000000000009, 12.0000000000009, 12.0000000000009, 1.99999999999909,
4, 4, 12.0000000000009, 1, 2.99999999999818, 1, 1, 2.00000000000182,
4, 1, 7, 12.0000000000009, 1, 1, 6.00000000000091, 1, 7, 4, 3.00000000000091,
1, 1, 1, 1, 1, 1, 1.99999999999909, 1, 1, 16, 12.0000000000009,
1, 12.0000000000009, 12.0000000000009, 1, 12.0000000000009, 1,
1, 12.0000000000009, 1, 12.0000000000009, 7, 3.00000000000091,
1, 1, 1.99999999999909, 4.99999999999909, 12.0000000000009, 1.99999999999909,
1.99999999999909, 6.00000000000091, 1, 7, 1, 1, 1.99999999999909,
1, 1, 4, 3.00000000000091, 1.99999999999909, 1, 1, 3.00000000000091,
1, 1, 1, 12.0000000000009, 1, 1, 1, 12.0000000000009, 10.9999999999991,
1, 1, 2.00000000000182, 1, 1, 1, 12.0000000000009, 1, 1, 12.0000000000009,
12.0000000000009, 24.0000000000009, 1, 1, 12.0000000000009, 1,
1, 1, 10, 12.0000000000009, 1, 4, 12.0000000000009, 1.99999999999909,
1, 7.99999999999909, 12.0000000000009, 12.0000000000009, 12.0000000000009,
12.0000000000009, 12.0000000000009, 12.0000000000009, 6.00000000000091,
12.0000000000009, 12.0000000000009, 4, 1, 1, 1, 12.0000000000009,
1, 1, 2.00000000000182, 12.0000000000009, 12.0000000000009, 1,
6.00000000000091, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2.99999999999818,
1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 2.00000000000182, 12.0000000000009,
12.0000000000009, 1, 1, 12.0000000000009, 1, 4.99999999999909,
12.0000000000009, 7.99999999999909, 1, 1, 1, 21.0000000000009,
7, 9.00000000000091, 2.00000000000182, 4, 4, 3.00000000000182,
12.0000000000009, 1, 1, 12.0000000000009, 1, 1, 4.99999999999909,
1, 1, 1.99999999999909, 1, 4.99999999999909, 4.99999999999909,
4.99999999999909, 4.99999999999909, 4.99999999999909, 1, 1, 1,
1, 1, 1, 1, 1, 1, 4, 1, 3.00000000000091, 12.0000000000009, 1,
1, 12.0000000000009, 12.0000000000009, 6.00000000000091, 3.00000000000091,
4, 1, 12.0000000000009, 7, 3.00000000000091, 1.99999999999909,
1, 1, 1, 1, 12.0000000000009, 1, 24.0000000000009, 12.0000000000009,
1, 12.0000000000009, 12.0000000000009, 12.0000000000009, 1, 1,
1, 1, 10.9999999999991, 1, 1.99999999999909, 1, 10.9999999999991,
3.00000000000091, 6.00000000000091, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 7, 1, 1, 4, 12.0000000000009, 4, 4, 12.0000000000009, 12.0000000000009,
12.0000000000009, 12.0000000000009, 6.00000000000091, 1.99999999999909,
1, 1, 1, 4.99999999999909, 12.0000000000009, 1, 1, 1, 12.0000000000009,
12.0000000000009, 12.0000000000009, 1, 1, 1, 1, 10.9999999999991,
12.0000000000009, 1, 1, 1, 1, 1, 7.99999999999909, 4, 2.99999999999818,
1.99999999999909, 1, 1, 4, 4, 1, 12.0000000000009, 12.0000000000009,
1.99999999999909, 1, 1, 1, 1, 12.0000000000009, 1.99999999999909,
4.99999999999909, 4.99999999999909, 3.00000000000091, 4, 1, 1,
1, 1, 2.00000000000182, 1, 1.99999999999909, 1, 3.00000000000091,
12.0000000000009, 4, 1.99999999999909, 9.00000000000091, 10,
1, 1, 1, 1, 12.0000000000009, 4, 2.00000000000182, 3.00000000000091,
4, 1, 1, 1, 1, 1, 4.99999999999909, 1.99999999999909, 1.99999999999909,
7, 1, 4, 1, 7, 7.99999999999909, 12.0000000000009, 1, 10, 1,
12.0000000000009, 7.99999999999909, 1, 1, 12.0000000000009, 4,
7.99999999999909, 7, 2.99999999999818, 1, 3.00000000000091, 4.99999999999909,
1, 1, 4, 4.99999999999909, 1, 6.00000000000091, 5.99999999999818,
1, 9.00000000000091, 1, 7.99999999999909, 1, 1, 1, 4.99999999999909,
1, 1, 1, 1, 1, 4.99999999999909, 1, 1, 1, 1, 1, 10.9999999999991,
1, 1, 1, 1, 2.00000000000182, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 4.99999999999909, 1, 1, 1, 1, 9.00000000000091, 1.99999999999909,
1, 7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 7, 1, 1, 1.99999999999909,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 7.99999999999909, 1, 1.99999999999909,
1, 1, 1, 1, 1, 1, 1, 1, 12.0000000000009, 12.0000000000009, 1,
12.0000000000009, 1, 1.99999999999909, 1, 1, 12.0000000000009,
10, 12.0000000000009, 1, 1, 1, 1, 10, 12.0000000000009, 1, 1,
1, 12.0000000000009, 12.0000000000009, 12.0000000000009, 1, 1,
1, 1, 12.0000000000009, 12.0000000000009, 12.0000000000009, 1,
1)), .Names = c("Month", "NumberOfMonth"), row.names = c(1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L,
42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L,
55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L,
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L,
81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L,
94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L,
106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L,
117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L,
128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L,
139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L,
150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L,
161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L,
172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L,
183L, 184L, 185L, 186L, 187L, 188L, 189L, 190L, 191L, 192L, 193L,
194L, 195L, 196L, 197L, 198L, 199L, 200L, 201L, 202L, 203L, 204L,
205L, 206L, 207L, 208L, 209L, 210L, 211L, 212L, 213L, 214L, 215L,
216L, 217L, 218L, 219L, 220L, 221L, 222L, 223L, 224L, 225L, 226L,
227L, 228L, 229L, 230L, 231L, 232L, 233L, 234L, 235L, 236L, 237L,
238L, 239L, 240L, 241L, 242L, 243L, 244L, 245L, 246L, 247L, 248L,
249L, 250L, 251L, 252L, 253L, 254L, 255L, 256L, 257L, 258L, 259L,
260L, 261L, 262L, 263L, 264L, 265L, 266L, 267L, 268L, 269L, 270L,
271L, 272L, 273L, 274L, 275L, 276L, 277L, 278L, 279L, 280L, 281L,
282L, 283L, 284L, 285L, 286L, 287L, 288L, 289L, 290L, 291L, 292L,
293L, 294L, 295L, 296L, 297L, 298L, 299L, 300L, 301L, 302L, 303L,
304L, 305L, 306L, 307L, 308L, 309L, 310L, 311L, 312L, 313L, 314L,
315L, 316L, 317L, 318L, 319L, 320L, 321L, 322L, 323L, 324L, 325L,
326L, 327L, 328L, 329L, 330L, 331L, 332L, 333L, 334L, 335L, 336L,
337L, 338L, 339L, 340L, 341L, 342L, 343L, 344L, 345L, 346L, 347L,
348L, 350L, 351L, 352L, 353L, 354L, 355L, 356L, 357L, 358L, 359L,
360L, 361L, 362L, 363L, 364L, 365L, 366L, 367L, 368L, 369L, 370L,
371L, 372L, 373L, 374L, 375L, 376L, 377L, 378L, 379L, 380L, 381L,
382L, 383L, 384L, 385L, 386L, 387L, 388L, 389L, 390L, 391L, 392L,
393L, 394L, 395L, 396L, 397L, 398L, 399L, 400L, 401L, 402L, 403L,
404L, 405L, 406L, 407L, 408L, 409L, 410L, 411L, 412L, 413L, 414L,
415L, 416L, 417L, 418L, 419L, 420L, 421L, 422L, 423L, 424L, 425L,
426L, 427L, 428L, 429L, 430L, 431L, 432L, 433L, 434L, 435L, 436L,
437L, 438L, 439L, 440L, 441L, 444L, 445L, 446L, 447L, 448L, 449L,
450L, 451L, 452L, 453L, 454L, 455L, 456L, 457L, 458L, 459L, 460L,
461L, 462L, 463L, 464L, 465L, 466L, 467L, 468L, 469L, 470L, 471L,
472L, 473L, 474L, 475L, 476L, 477L, 478L, 479L, 480L, 481L, 482L,
483L, 484L, 485L, 486L, 487L, 488L, 489L, 490L, 491L, 492L, 493L,
494L, 495L, 496L, 497L, 498L, 499L, 500L, 501L, 502L, 503L, 504L,
505L, 506L, 507L, 508L, 509L, 510L, 511L, 512L, 513L, 514L, 515L,
516L, 517L, 518L, 519L, 520L, 521L, 522L, 523L, 524L, 525L, 526L,
527L, 528L, 529L, 530L, 531L, 532L, 533L, 534L, 535L, 536L, 537L,
538L, 539L, 540L, 541L, 542L, 543L, 544L, 545L, 546L, 547L, 548L,
549L, 550L, 551L, 552L, 553L, 554L, 555L, 556L, 557L, 558L, 559L,
560L, 561L, 562L, 563L, 564L, 565L, 566L, 567L, 568L, 569L, 570L,
571L, 572L, 573L, 574L, 575L, 576L, 577L, 578L, 579L, 580L, 581L,
582L, 583L, 584L, 585L, 586L, 587L, 588L, 589L, 590L, 591L, 592L,
593L, 594L, 595L, 596L, 597L, 598L, 599L, 600L, 601L, 602L, 603L,
604L, 605L, 606L, 607L, 608L, 609L, 610L, 611L, 612L, 613L, 614L,
615L, 616L, 617L, 618L, 619L), class = "data.frame", na.action = structure(c(349L,
442L, 443L), .Names = c("349", "442", "443"), class = "omit"))
Read in the data
dd = read.table(textConnection("month NumberOfMonths
Jan 4
Jan 3
Feb 2
May 1
Jan 4
Feb 1
May 2
Mar 12
Feb 2
May 1"), header=TRUE)
Set up a factor with correct levels
dd$month = factor(dd$month, levels=month.abb)
## I've made No. of months a factor to influence the table output
dd$NumberOfMonths = factor(dd$NumberOfMonths, levels=1:12)
Now tabulate
table(dd$month, dd$NumberOfMonths)
## Drop unused months
table(droplevels(dd$month), droplevels(dd$NumberOfMonths))
you also have xtabs :
tab$NumberOfMonths <- factor(tab$NumberOfMonths, levels=1:12)
tab$month <- factor(tab$month, levels=c("Jan", "Feb", "Mar", "Apr", "May"))
xtabs(~month+NumberOfMonths, data=tab)
I also like using the dcast function within the reshape2 package.
dat <- ...
dcast(dat, month ~ NumberOfMonths)
# month 1 2 3 4 12
#1 Feb 1 2 0 0 0
#2 Jan 0 0 1 2 0
#3 Mar 0 0 0 0 1
#4 May 2 1 0 0 0

Resources