R: Calculate moving maximum slope by week accounting for factors - r

I have a data.frame that includes heating degree day (HDD) below.
structure(list(WinterID = 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("2002", "2002_2003", "2003", "2003_2004",
"2004", "2004_2005", "2005", "2005_2006", "2006", "2006_2007",
"2007", "2007_2008", "2008"), class = "factor"), Date = structure(c(11968,
11969, 11970, 11971, 11972, 11973, 11974, 11975, 11976, 11977,
11978, 11979, 11980, 11981, 11982, 11983, 11984, 11985, 11986,
11987, 11988, 11989, 11990, 11991, 11992, 11993, 11994, 11995,
11996, 11997, 11998, 11999, 12000, 12001, 12002, 12003, 12004,
12005, 12006, 12007, 12008, 12009, 12010, 12011, 12012, 12013,
12014, 12015, 12016, 12017, 12018, 12019, 12020, 12021, 12022,
12023, 12024, 12025, 12026, 12027, 12028, 12029, 12030, 12031,
12032, 12033, 12034, 12035, 12036, 12037, 12038, 12039, 12040,
12041, 12042, 12043, 12044, 12045, 12046, 12047, 12048, 12049,
12050, 12051, 12052, 12053, 12054, 12055, 12056, 12057, 12058,
12059, 12060, 12061, 12062, 12063, 12064, 12065, 12066, 12067,
12068, 12069, 12070, 12071, 12072, 12073, 12074, 12075, 12076,
12077, 12078, 12079, 12080, 12081, 12082, 12083, 12084, 12085,
12086, 12087, 12088, 12089, 12090, 12091, 12092, 12093, 12094,
12095, 12096, 12097, 12098, 12099, 12100, 12101, 12102, 12103,
12104, 12105, 12106, 12107, 12108, 12109, 12110, 12111, 12112,
12113, 12114, 12115, 12116, 12117, 12118, 12119, 12120, 12121,
12122, 12123, 12124, 12125, 12126, 12127, 12128, 12129, 12130,
12131, 12132, 12133, 12134, 12135, 12136, 12137, 12138, 12139,
12140, 12141, 12142, 12010, 12011, 12014, 12015, 12017, 12023,
12024, 12025, 12026, 12027, 12028, 12029, 12030, 12042, 12070,
12071, 12075, 12076, 12077, 12078, 12079, 12080, 12082, 12083,
12084), class = "Date"), SiteID = 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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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, 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("B_A", "B_B", "B_E"), class = "factor"),
HDD = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0.246666666666667, 7.12666666666667, 10.6133333333333,
2.96666666666667, 0, 0.0933333333333337, 7.31333333333334,
10.7133333333333, 6.20000000000001, 2.70666666666667, 6.20000000000001,
3.88666666666667, 16.5866666666667, 28.3933333333333, 12.98,
21.6133333333333, 19.14, 12.6666666666667, 7.52, 3.33333333333334,
18.2933333333333, 4.14666666666667, 2.17333333333334, 26.08,
1.38, 7.48000000000001, 36.5733333333333, 53.4666666666667,
98.4533333333333, 109.093333333333, 104.14, 80.2466666666667,
47.0333333333333, 14.7133333333333, 15.7266666666667, 21.1066666666667,
5.07333333333334, 0.613333333333334, 6.18000000000001, 29.5666666666667,
45.5333333333333, 59.5666666666667, 91.44, 85.38, 51.1, 25.9666666666667,
14.8266666666667, 34.48, 79.16, 90.08, 66.3533333333333,
75.14, 97.1733333333333, 83.3066666666667, 50.0133333333333,
37.2733333333333, 88.9133333333334, 101.926666666667, 100.56,
99.2933333333334, 97.66, 89.6466666666667, 110.613333333333,
79.1466666666667, 92.6066666666667, 71.7133333333333, 31.32,
27.02, 39.02, 98.14, 62.5866666666667, 46.7933333333333,
47.5133333333333, 48.3666666666667, 25.5333333333333, 13.6,
17.9133333333333, 14.16, 7.98666666666667, 3.44, 1.86666666666667,
12.66, 0, 7.09333333333334, 21.3266666666667, 40.52, 18.8466666666667,
37.8466666666667, 33.42, 33.7133333333333, 15.6133333333333,
0.720000000000001, 2.31333333333334, 12.3066666666667, 8.48666666666667,
2.86, 0, 0, 0, 6.98666666666667, 6.67333333333334, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6.58000000000001, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13.42, 30.5266666666667,
1.12, 28.5066666666667, 6.82666666666667, 10.3933333333333,
3.18, 11.0466666666667, 0, 0, 0)), .Names = c("WinterID",
"Date", "SiteID", "SubstrateConcat", "HDD"), row.names = c(NA,
200L), class = "data.frame")
I'm trying to calculate the moving maximum slope over 7 days beginning on 4 November of each year without using a loop. This moving maximum slope needs to account for WinterID, SiteID, and SubstrateConcat.
For clarification, the calculation I'm trying to obtain is this:
Slope=(max-min)/7, where:
Max= (i-3)+(i-2)+(i-1)+i+(i+1)+(i+2)+(i+3)
Min= (i-3)
(((i-3)+(i-2)+(i-1)+i+(i+1)+(i+2)+(i+3)) - (i-3))/7
So, using a real example starting with 2002-11-19 as i:
(0+0.24+7.13+10.61+2.97+0+0.97) - 0)/7 = 3.13
I tried using zoo package rollmean, however, I could not figure out how to account for WinterID, SiteID, and SubstrateConcat. This gave me an "order.by" error where my Date values were not unique, since I have dates with different SubstrateConcat and WinterID criteria. As I enter more data into the database, there will eventually be dates with multiple SiteID criteria as well.
I thought maybe xts, TTR and ROC would be what I could use as in this question: Maximum slope for a given interval each day. But again, I don't understand how to specify the multiple group factors, as well as going three days forward and three days back as in align=center with rollmean.
Will someone please point me in the right direction here? Will one of the above functions combined with ddply work?
Thank you!
EDITED to include the answer after the answer supplied by #eddi.
dt <- data.table(df)
dt[, MaxSlope := if(length(HDD)<7) {rep(NA_real_, length(HDD))} else {filter(HDD, c(1,1,1,1,1,1,0)/7)}, by=list(Winter, Site, Substrate)]
This code works perfectly for dates that are continuous. Can anyone recommend how to tweak this code for data that has missing dates? For instance, I have:
Date Temp
Nov 21 14
Nov 23 10
Nov 24 12
Nov 27 11
Nov 28 7
Nov 29 9
Nov 30 10
Dec 01 12
Dec 02 8
Dec 03 7
I don't want the Max Slope calculated for Nov 21, Nov 23 and Nov 24 because there isn't consecutive data for the calculation. Instead, I want "NA" inserted. Can the existing code above, be modified to accommodate this?

Sounds like you need filter (or you could also use one of the rolling mean/sum functions). And the grouping part is easiest to do with data.table:
library(data.table)
dt = data.table(your_df)
dt[, filter(HDD, c(1,1,1,1,1,1,0))/7,
by = list(WinterID, SiteID, SubstrateConcat)]

I couldn't get working solution with ddply, though I didn't spend much time debugging. Here's a solution using base functions (assuming your object is named hdd).
# split your object into groups
shdd <- split(hdd, hdd[,c("WinterID","SiteID","SubstrateConcat")], drop=TRUE)
# create a function to apply to each group
f <- function(d) transform(d, MaxSlopeHDD=rollmax(c(NA,diff(d$HDD)),7,fill=NA))
# apply the function to each group and rbind the results together
shdd <- do.call(rbind, lapply(shdd, f))

Related

How to compare the slope and intercept of two total least squares regressions in R?

I have a dataset consisting of the two numerical, measured variables D and O, and a third variable that indicates sampling locations (East or West), in total 310 observation.
D and O are linearly correlated, I have used odregress() in R to create total least square regressions. Doing this for both sampling locations seperately leads to fairly similar regression lines.
I would like to statistically test if slope and/or intercept are different for these two locations. If I had used a simple linear regression I would use an ANOVA for this. But as far as I know this doesnt work for total least square approaches, is that correct? I have googled a lot but couldnt find any solution to this. Someone suggested bootstrapping both groups separately, getting 100 different slopes and intercepts and then to t.test these slope and intercept groups, but I feel like there should be an easier way of doing this?
I would appreciate any help!
EDIT: Here is the dataset
structure(list(O = c(0.7324, 0.78124, 0.78556, 0.78704, 0.81002,
0.87443, 0.8924, 0.91224, 0.92517, 0.92573, 0.92924, 0.93397,
0.95854, 0.96477, 0.98186, 0.99257, 0.9931, 0.99488, 0.99901,
1.0071, 1.0119, 1.0275, 1.0453, 1.0467, 1.0526, 1.0622, 1.0644,
1.0694, 1.0781, 1.0785, 1.0808, 1.0847, 1.0855, 1.0871, 1.0871,
1.0872, 1.0935, 1.1028, 1.1067, 1.1106, 1.1207, 1.1251, 1.131,
1.1359, 1.1359, 1.1387, 1.1419, 1.1496, 1.151, 1.1526, 1.1536,
1.1538, 1.1592, 1.1595, 1.1645, 1.1705, 1.1766, 1.1842, 1.1997,
1.2, 1.2011, 1.2077, 1.2085, 1.2175, 1.2183, 1.2287, 1.2301,
1.2362, 1.2449, 1.248, 1.2508, 1.254, 1.2638, 1.2721, 1.2745,
1.2858, 1.3039, 1.306, 1.3082, 1.3151, 1.3162, 1.3195, 1.3365,
1.3392, 1.3413, 1.355, 1.3614, 1.3675, 1.3826, 1.3882, 1.3926,
1.4075, 1.4094, 1.4109, 1.4155, 1.4414, 1.4487, 1.4554, 1.4642,
1.4771, 1.4782, 1.4845, 1.5, 1.5136, 1.6112, 1.6173, 1.6228,
1.6761, 1.7301, 1.7614, 1.8407, 1.893, 2.1033, 2.433, 0.13104,
0.18361, 0.25999, 0.26253, 0.26939, 0.36762, 0.37335, 0.38632,
0.39468, 0.48303, 0.50917, 0.5375, 0.54697, 0.55499, 0.55638,
0.55957, 0.56313, 0.58666, 0.58995, 0.61187, 0.63833, 0.63971,
0.65251, 0.66876, 0.67105, 0.67192, 0.67333, 0.67489, 0.69292,
0.69587, 0.71104, 0.71439, 0.715, 0.72289, 0.72526, 0.75811,
0.75894, 0.76593, 0.76717, 0.77036, 0.7803, 0.78315, 0.78472,
0.78818, 0.78862, 0.79408, 0.79605, 0.79814, 0.80054, 0.81471,
0.81491, 0.8154, 0.81859, 0.82125, 0.8259, 0.8261, 0.83919, 0.8415,
0.84694, 0.85396, 0.86645, 0.86752, 0.86833, 0.87828, 0.88269,
0.88637, 0.8927, 0.90286, 0.90783, 0.91026, 0.91418, 0.91427,
0.91478, 0.9166, 0.91863, 0.91865, 0.92676, 0.9295, 0.9334, 0.93429,
0.93487, 0.93529, 0.94348, 0.95148, 0.952, 0.95354, 0.95403,
0.9548, 0.96222, 0.96533, 0.96921, 0.97023, 0.97109, 0.97247,
0.97251, 0.98598, 0.98738, 0.99089, 0.99475, 0.99679, 0.99814,
1.0051, 1.0061, 1.0074, 1.0097, 1.0189, 1.0231, 1.0305, 1.0379,
1.0452, 1.0467, 1.0469, 1.0473, 1.0474, 1.0476, 1.0521, 1.06,
1.0652, 1.0673, 1.0737, 1.0739, 1.0796, 1.0832, 1.0864, 1.0922,
1.0923, 1.0928, 1.0987, 1.1098, 1.1108, 1.112, 1.119, 1.1319,
1.135, 1.1355, 1.1368, 1.1406, 1.15, 1.1532, 1.1548, 1.1561,
1.1563, 1.161, 1.1628, 1.1657, 1.1695, 1.1726, 1.1762, 1.1847,
1.1851, 1.186, 1.1861, 1.1864, 1.1892, 1.1944, 1.2014, 1.2034,
1.2035, 1.2088, 1.209, 1.2093, 1.21, 1.226, 1.234, 1.2457, 1.258,
1.2597, 1.2601, 1.261, 1.2619, 1.2648, 1.2724, 1.2785, 1.3138,
1.3148, 1.3392, 1.3452, 1.3537, 1.3553, 1.3613, 1.3634, 1.374,
1.3804, 1.39, 1.3964, 1.4017, 1.4033, 1.4124, 1.454, 1.4909,
1.4933, 1.5095, 1.6554, 2.2905, 2.2935), D = c(6.7335, 5.9842,
7.2607, 6.6692, 6.7883, 6.3851, 6.9412, 6.0943, 6.617, 6.3907,
7.8515, 7.5698, 7.0378, 7.4205, 7.499, 6.8394, 7.0227, 7.5943,
7.6416, 6.2831, 8.1137, 6.5366, 8.5699, 7.9497, 6.6017, 6.6698,
7.4538, 8.0064, 7.9721, 8.618, 8.0632, 7.9414, 7.1566, 6.7663,
8.7359, 6.8296, 7.0741, 7.7438, 6.97, 8.4848, 7.6823, 7.8706,
7.2941, 7.6861, 7.8883, 7.2856, 7.7869, 7.6533, 8.2157, 8.0779,
7.4342, 6.9185, 8.6697, 8.1597, 6.869, 7.8173, 8.0279, 7.3248,
8.1733, 8.8169, 8.1503, 8.6909, 8.7486, 6.9067, 8.4934, 7.891,
8.5693, 8.9372, 8.5297, 8.1161, 8.1002, 7.764, 7.4613, 8.3119,
8.1478, 8.0479, 8.0527, 8.6343, 7.8803, 8.032, 7.2934, 7.7577,
10.055, 6.9696, 8.2901, 9.4509, 8.6809, 8.3964, 9.8273, 8.0222,
8.933, 8.6679, 9.4189, 9.7793, 9.468, 9.4953, 8.9932, 9.9725,
9.2968, 9.9642, 9.3904, 8.9943, 8.8995, 9.4839, 9.9091, 9.3051,
9.8542, 8.5494, 8.5515, 8.287, 10.293, 8.591, 7.8362, 11.147,
2.8316, 3.8897, 4.5759, 3.7706, 4.225, 4.8624, 3.444, 4.0051,
4.3401, 2.938, 4.8966, 3.5711, 4.4281, 5.0723, 5.4119, 5.472,
4.9745, 5.52, 5.5544, 6.9943, 4.8439, 5.0446, 4.5001, 5.1235,
6.3628, 4.7419, 5.8969, 5.5802, 5.6402, 5.5854, 4.9522, 4.7576,
4.1654, 5.571, 5.6993, 4.6309, 5.115, 5.5524, 5.6906, 5.7571,
5.6431, 5.1011, 5.0844, 5.6666, 4.5314, 6.456, 5.1582, 5.3766,
4.7862, 5.4651, 5.914, 5.345, 5.494, 6.9188, 5.7707, 4.9756,
5.1671, 6.1472, 5.2446, 6.1542, 5.9616, 5.9502, 5.4772, 6.1042,
5.7241, 5.592, 4.9474, 6.8122, 7.1245, 6.4829, 5.0722, 6.7933,
6.2212, 6.2546, 6.434, 6.9884, 6.4172, 5.7726, 5.5066, 5.8896,
6.9811, 5.0706, 6.2065, 6.6623, 6.0453, 6.6251, 5.7937, 6.889,
6.4776, 6.3118, 6.2194, 4.9703, 6.4035, 7.5873, 6.4064, 7.1442,
6.461, 5.4649, 5.3957, 6.7057, 6.4148, 6.52, 5.878, 6.8155, 7.4694,
6.4654, 6.081, 8.053, 6.5501, 6.6835, 6.8489, 6.2467, 7.4948,
7.1152, 7.1818, 6.4375, 7.3438, 7.2218, 5.4177, 7.0612, 6.8986,
6.9722, 6.5899, 6.876, 6.817, 6.9595, 7.674, 6.3334, 6.9009,
6.236, 7.1216, 6.4549, 6.8034, 6.379, 6.6753, 6.9686, 7.4758,
7.2485, 6.9605, 6.7682, 6.7202, 7.3145, 7.266, 6.1579, 7.5649,
7.1079, 7.0922, 6.886, 6.9401, 6.8369, 6.8474, 7.2315, 6.3774,
5.6486, 7.1576, 7.5174, 7.3115, 7.998, 8.4278, 7.5225, 7.5302,
7.196, 7.5353, 6.5144, 7.8133, 6.4237, 7.8706, 7.9279, 6.9488,
7.5694, 7.078, 6.9277, 8.1135, 7.7531, 6.6827, 6.9672, 7.6023,
8.957, 7.3327, 7.6301, 8.0807, 6.824, 9.4569, 8.8401, 7.1244,
8.6603, 7.8307, 7.8158, 6.5451, 8.2186, 8.4406, 8.3064, 6.3104,
7.3834, 7.3139), Location = 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, 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, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label =
c("East",
"West"), class = "factor")), class = "data.frame", row.names = c(NA,
-309L))

Add a column with repeating values taken from another data.frame

everybody! I have the following data:
> dput(test)
structure(list(CNTRYID = 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, 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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,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Czech Republic",
"Denmark", "France", "Greece", "Hungary", "Italy", "Lithuania",
"Netherlands", "Poland", "Slovak Republic", "Slovenia", "Spain",
"United Kingdom"), class = "factor"), Gender = structure(c(2L,
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L,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 1L, 2L, 1L,
1L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L,
1L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 1L, 2L), .Label = c("Male", "Female"), class = "factor"),
PVLIT1 = c(341.73556, 327.75647, 333.99133, 299.23201, 274.26806,
330.20199, 290.3974, 245.60594, 305.04734, 322.12211, 264.19379,
283.34121, 286.62649, 301.27084, 204.67276, 339.7475, 350.74884,
300.41595, 338.76949, 340.92763, 323.86579, 320.03587, 276.43592,
307.12426, 290.60296, 322.23858, 299.0517, 328.35244, 267.82705,
245.1432, 322.43248, 352.37208, 275.75408, 317.75532, 332.75897,
321.23265, 315.338, 303.61999, 309.63271, 315.13702, 349.31912,
330.28415, 291.53027, 238.86247, 336.98836, 312.80759, 312.32254,
238.765, 268.14566, 326.59843, 298.99782, 372.8563, 314.59135,
267.68085, 358.00259, 293.74775, 257.99369, 352.46286, 277.74005,
340.21297, 343.90215, 365.2711, 298.95923, 347.24665, 288.41685,
267.28314, 299.62821, 288.50113, 346.33, 348.62584, 304.37408,
288.28595, 209.42411, 308.3979, 317.11152, 339.05234, 338.79844,
273.4862, 317.05362, 321.9683, 298.49502, 315.31917, 285.24075,
260.5942, 292.17127, 278.49578, 351.55043, 291.29336, 345.61643,
252.87387, 318.81766, 359.97134, 334.8103, 282.96817, 347.12409,
266.44512, 284.23384, 303.45264, 228.6119, 248.62507, 445.14543,
262.78094, 330.89695, 276.94543, 340.09174, 370.30567, 254.49489,
267.31821, 235.36262, 268.87887, 293.34609, 323.89632, 332.1124,
270.03193, 302.92495, 290.86264, 330.11303, 225.08377, 306.51173,
393.41635, 296.14069, 290.8399, 249.71853, 273.45073, 391.61765,
229.62298, 368.49384, 352.84141, 276.27584, 271.89748, 345.33152,
279.98931, 322.87181, 262.99515, 251.44921, 291.01078, 302.7094,
288.9321, 335.60061, 353.84158, 225.1918, 267.17191, 261.80763,
291.8125, 354.75112, 263.19101, 300.22572, 309.91597, 323.37047,
371.69579, 288.89204, 344.10129, 211.20385, 268.50285, 347.76109,
223.8669, 280.59382, 341.03251, 330.07897, 297.91223, 382.22934,
346.63016, 279.97218, 296.75031, 279.52446, 239.17915, 307.47267,
310.66321, 339.9095, 288.07157, 313.38334, 294.49606, 301.16477,
330.97285, 306.29201, 258.9723, 305.16945, 220.56537, 293.92923,
316.1579, 294.9318, 231.12861, 327.08462, 295.45707, 212.56843,
343.58854, 321.65331, 347.51206, 327.05139, 270.26912, 245.49499,
284.13733, 346.38385, 262.40319, 305.82358, 268.60343, 314.58164,
247.77339, 346.95623, 326.82632, 296.7529, 252.09381, 307.30177,
262.73407, 261.99723, 265.99006, 297.02533, 221.36089, 342.78459,
259.3387, 308.40893, 345.39162, 252.01223, 249.46163, 232.65253,
295.11341, 269.32542, 270.33917, 311.52152, 295.45142, 299.57599,
264.36488, 253.78787, 277.27982, 184.86342, 343.89483, 304.73262,
315.47804, 277.46603, 294.02264, 278.49927, 288.03195, 261.12088,
258.48768, 286.84427, 232.02021, 239.09253, 249.53217, 341.65779,
199.08805, 348.26952, 292.84918, 233.47102, 280.34833, 266.45888,
300.40607, 287.81728, 293.79174, 257.01624, 375.42354, 212.49236,
326.45861, 266.21237, 282.09158, 273.21422, 226.8083, 320.00889,
300.76454, 261.39701, 263.16525, 325.74916, 285.175, 297.7344,
266.98718, 287.35748, 295.33917, 310.84685, 235.88418, 335.12738,
338.0637, 263.95686, 341.24316, 344.17478, 277.19368, 292.86124,
306.71616, 302.24125, 292.9568, 327.03543, 273.10249, 246.17394,
329.32818, 289.45179, 329.58524, 316.57873, 254.53541, 307.42027,
340.46333, 260.72247, 177.1923, 298.05925, 229.80109, 299.93516,
298.97657, 296.32594, 331.81735, 335.73543, 348.6484, 224.40826,
339.58894, 230.45043, 297.69392, 250.64399, 216.37015, 248.48309,
315.3377, 259.91751, 343.96253, 238.19071, 268.7662, 195.30995,
295.58828, 282.90717, 257.23455, 312.86206, 255.33727, 293.08026,
321.61584, 352.64202, 331.90932, 291.72882, 301.87908, 299.83755,
276.29271, 288.31371, 173.19914, 235.98186, 267.68055, 273.57641,
259.38568, 282.82247, 255.2055, 259.92553, 324.37237, 323.27084,
297.1294, 293.29913, 319.26816, 296.19763, 287.20711, 120.7633,
299.14635, 324.02645, 272.24852, 280.46404, 363.56726, 213.18303,
318.84395, 307.23832, 337.49138, 352.61447, 295.99838, 245.2863,
303.07131, 200.14136, 314.2698, 290.86413, 289.6412, 232.13515,
328.96702, 250.57396, 251.36983, 259.23267, 277.98361, 306.61626,
298.90864, 336.54659, 270.34128, 295.23666, 289.28454, 314.25576,
305.90498, 223.59499, 297.3472, 302.97199, 306.82586, 293.8622,
277.52691, 274.99887, 309.28188, 243.9094, 287.04775, 332.49858,
316.85995, 227.84818, 338.34644, 231.03199, 280.42949, 326.23743,
287.90662, 308.44719, 224.65553, 255.51269, 320.01916, 284.76909,
310.07218, 305.64163, 267.90448, 291.91266, 311.95645, 234.77883,
288.47911, 337.66548, 235.67435, 328.26341, 217.05384, 271.18711,
256.92855, 306.35037, 298.57583, 220.25599, 305.70531, 397.23308,
243.34537, 318.81272, 287.84564, 291.19244, 260.50231, 309.29317,
288.32053, 285.65509, 270.71887, 349.55511, 266.04791, 305.22095,
259.29735, 283.99375, 281.30021, 368.59316, 300.66732, 253.21196,
277.61171, 313.79851, 310.08222, 288.8469, 329.40954, 324.01478,
283.84872, 293.73975, 175.58543, 287.87392, 304.40044, 294.23277,
268.17933, 309.02819, 253.19897, 265.68756, 290.41261, 333.8446,
248.33467, 298.56631, 196.18183, 249.01214, 239.07566, 272.0436,
266.38371, 244.71529, 230.62195, 280.98915, 327.92842, 291.64174,
290.58153, 278.05094, 242.61752, 290.2778, 349.53173, 206.13951,
293.64674, 201.95774, 353.00234, 301.41944, 266.58184, 313.45438,
290.98948, 287.79774, 334.35567, 276.27736, 271.85967, 233.80236,
275.925, 292.44499, 275.67027, 306.44843, 293.76468, 300.68248,
277.37136, 258.36308, 215.93238, 304.53151, 282.98405, 284.24563,
267.54523, 278.81698, 293.47491, 274.20848, 325.56625, 338.29286,
237.04901, 321.03634, 284.59905), PVLIT2 = c(329.96437, 309.7071,
323.76923, 311.6131, 296.0494, 320.40846, 316.35148, 284.25026,
305.10127, 317.73826, 279.00676, 311.32111, 263.20577, 295.35872,
247.77471, 312.83461, 338.78073, 306.69291, 323.277, 324.9266,
286.49722, 317.47413, 294.87445, 332.89844, 285.07891, 294.75732,
312.38012, 307.31727, 281.80926, 299.38988, 307.64408, 359.26622,
269.06506, 311.84518, 335.81958, 320.7753, 261.46395, 254.00522,
322.33784, 304.60692, 330.96759, 348.84054, 308.38673, 246.73035,
330.95077, 351.57083, 307.36752, 255.48044, 269.63236, 347.14345,
315.88441, 400.94726, 296.03271, 276.19477, 363.93767, 309.51741,
249.73923, 359.06376, 268.23778, 340.45045, 347.93015, 331.94389,
265.17793, 339.92872, 302.63463, 271.15349, 287.00579, 245.92875,
333.74443, 395.13695, 319.19868, 280.50088, 274.27643, 298.57925,
301.34704, 343.32807, 322.24002, 260.04914, 305.97704, 299.22391,
311.31108, 314.82843, 284.82475, 263.54816, 305.83341, 275.05002,
291.21003, 295.85667, 325.4418, 297.35548, 328.91424, 348.46255,
374.91294, 278.72346, 336.15884, 284.42308, 301.46666, 280.60555,
221.60585, 240.95952, 352.39105, 244.17594, 331.78841, 288.21081,
311.62633, 360.55586, 252.44882, 275.11625, 224.22946, 263.59549,
275.29046, 321.90792, 332.09749, 282.92641, 282.89338, 292.94236,
329.33191, 242.67135, 284.59736, 372.01791, 284.58945, 304.33581,
262.18477, 263.41397, 397.0077, 233.13107, 354.51546, 330.58595,
267.95011, 272.97652, 320.41706, 269.92821, 311.25004, 248.15808,
260.19179, 278.38501, 314.36786, 296.30594, 295.8064, 362.28497,
236.54304, 280.39167, 268.16006, 282.61124, 328.43024, 266.80519,
272.71165, 317.02057, 302.61842, 313.83703, 299.44275, 347.81048,
248.96742, 244.98621, 314.78989, 228.19594, 277.08832, 321.66255,
307.59243, 285.20515, 329.42832, 327.38645, 266.39655, 284.52323,
253.67779, 239.9784, 315.34247, 324.64586, 339.56843, 334.15503,
324.9195, 289.38614, 279.53691, 346.34106, 299.6327, 264.50415,
329.80362, 257.43317, 310.62546, 325.04705, 266.05947, 252.10474,
377.03047, 284.46253, 252.06975, 307.29189, 325.21219, 318.36119,
313.92387, 266.27629, 236.96554, 308.785, 327.27422, 260.62932,
298.31588, 266.69611, 343.64082, 249.20663, 368.61185, 314.38577,
296.44633, 301.93329, 305.82456, 234.26794, 285.09993, 264.74247,
291.16644, 261.86653, 326.92465, 271.35729, 298.4598, 315.83039,
251.59546, 248.09457, 250.90401, 313.28557, 281.10513, 289.41966,
322.99908, 308.48391, 303.46002, 259.34959, 285.41517, 276.54708,
215.90873, 320.46273, 307.92637, 329.45411, 280.43586, 319.99516,
295.35201, 270.81816, 260.6502, 277.0142, 285.64532, 217.5607,
245.98812, 226.29258, 309.23674, 220.03202, 387.01331, 268.84682,
248.05849, 279.63382, 285.34802, 284.09757, 273.3005, 259.08111,
264.10347, 386.52038, 210.39955, 320.20292, 245.43105, 301.72215,
262.78873, 207.51776, 329.13312, 321.57565, 258.19365, 291.23977,
310.97158, 272.66014, 259.28731, 246.29506, 319.95987, 294.63771,
311.32836, 312.20071, 350.12412, 308.50961, 237.55051, 343.6281,
340.66271, 297.44784, 331.91189, 276.27969, 301.48287, 321.79642,
285.85259, 256.98003, 267.14263, 334.84721, 348.23387, 322.91747,
268.42791, 269.33266, 334.05385, 319.43236, 270.38822, 168.24661,
327.46855, 248.39576, 294.05205, 318.50606, 312.98079, 287.35827,
361.43941, 342.54578, 247.997, 316.69669, 232.59609, 285.6277,
261.74517, 230.28962, 254.88764, 302.1326, 267.05801, 385.75184,
267.35567, 313.20042, 203.28131, 282.90967, 290.01701, 282.52713,
339.58608, 266.53576, 297.53657, 278.7528, 348.01977, 328.51913,
289.18677, 319.62583, 311.31862, 271.67348, 279.98333, 227.36405,
276.27979, 290.28081, 280.66062, 255.47229, 291.26037, 264.89911,
304.19635, 330.39094, 334.60314, 272.9507, 294.44286, 346.35474,
332.11805, 313.19078, 104.71963, 293.40003, 329.98007, 247.70109,
259.13664, 325.72004, 239.1354, 294.74629, 291.78074, 328.38065,
334.70175, 312.16118, 215.75044, 297.27003, 202.36576, 271.84798,
291.59486, 303.22928, 221.18819, 315.37282, 238.00923, 233.12403,
269.9957, 259.90847, 311.6859, 272.51505, 346.11677, 260.70023,
275.42651, 290.61255, 307.04942, 323.60677, 219.96542, 305.55666,
283.99912, 267.52644, 285.16624, 301.02858, 260.05581, 283.41617,
244.09153, 290.04512, 353.63745, 317.37715, 241.34487, 321.14259,
258.36885, 261.37509, 348.62232, 300.73864, 327.22453, 228.55662,
268.91226, 314.09536, 260.56452, 283.06898, 319.64052, 275.96927,
296.46456, 295.56963, 276.68877, 289.44363, 296.45405, 240.71778,
310.15017, 237.00255, 240.61887, 244.14616, 316.83501, 253.25562,
230.76127, 304.57009, 364.11619, 267.0067, 308.55546, 275.38505,
293.96026, 243.73364, 321.01363, 316.79925, 277.67496, 289.51552,
307.85001, 279.01546, 317.36872, 242.91248, 279.70631, 226.23475,
386.36518, 326.81052, 235.98733, 288.59577, 305.13604, 353.10836,
274.67248, 327.51103, 322.45477, 315.17773, 316.73504, 189.05388,
267.47862, 316.6865, 272.59329, 281.07099, 327.16369, 280.24953,
253.59041, 314.05915, 341.81589, 286.40082, 323.2246, 218.71734,
256.56632, 239.05101, 292.73646, 262.55946, 280.23444, 219.2759,
293.2041, 342.50186, 251.37571, 321.82169, 290.32648, 233.07586,
293.06438, 333.05446, 222.39499, 298.17628, 201.93622, 354.07415,
294.31312, 301.73295, 328.2708, 278.76013, 289.11145, 313.0081,
275.55262, 287.71408, 272.14237, 300.88391, 308.66827, 284.93954,
312.92511, 335.31426, 276.17927, 320.79382, 273.71349, 255.89581,
300.24576, 292.25961, 288.96434, 303.44888, 253.07684, 248.08721,
281.36322, 326.35893, 335.85343, 253.90892, 328.49694, 272.70062
), PVLIT3 = c(347.03202, 297.99638, 308.9164, 300.68665,
302.42232, 318.3244, 315.16283, 273.26457, 306.98393, 342.15998,
271.74761, 330.01561, 239.3113, 292.38404, 220.32939, 320.98693,
328.77846, 312.25874, 320.16997, 315.01401, 278.54675, 297.93796,
289.05429, 306.41476, 292.88945, 287.35494, 289.7173, 327.61128,
256.59233, 270.40046, 299.88817, 347.61574, 271.24062, 300.58625,
389.47902, 323.26406, 303.8882, 287.85783, 320.78071, 260.5962,
329.16759, 322.44865, 288.50371, 230.97053, 341.00067, 317.60536,
334.04519, 242.21659, 273.88791, 319.75284, 303.14072, 369.25668,
315.38962, 238.82989, 389.86658, 291.40597, 256.2491, 328.59089,
270.08341, 346.88612, 351.05896, 313.98005, 276.49836, 341.01902,
304.44641, 279.94384, 297.38058, 260.15889, 301.88881, 385.34273,
281.3716, 305.96796, 235.56287, 313.06217, 242.71572, 354.28779,
305.7085, 256.87025, 288.9405, 291.11581, 307.62043, 321.73213,
283.82863, 248.89211, 291.42861, 273.85537, 307.50928, 316.5518,
321.39642, 289.61352, 319.72759, 339.1263, 364.61416, 277.72976,
295.74977, 274.09616, 272.9924, 272.49014, 251.05889, 251.01214,
386.34787, 245.74296, 303.75875, 284.2808, 350.74965, 354.9783,
229.63546, 282.53014, 233.23552, 252.86831, 264.47331, 320.17187,
357.86769, 281.28431, 291.52807, 309.28932, 335.37317, 250.08053,
279.77425, 370.20707, 313.44683, 293.07481, 250.40199, 270.51998,
391.40329, 234.55338, 376.94785, 332.07567, 246.3079, 299.29627,
313.21714, 285.05673, 285.967, 272.23878, 254.95618, 291.76139,
311.07348, 291.55214, 330.95787, 368.524, 230.85077, 259.26676,
269.73751, 284.4568, 331.74769, 267.65832, 297.71321, 327.10192,
281.02533, 317.82234, 277.43442, 347.61242, 242.73154, 273.46411,
326.03792, 228.83492, 292.92737, 304.29623, 275.46119, 298.80275,
343.8432, 343.49854, 280.61476, 290.29079, 251.7561, 249.89728,
297.3227, 298.18224, 355.51642, 310.46884, 337.82139, 307.61772,
313.36828, 334.1722, 305.46306, 278.88311, 278.72036, 267.1024,
320.97308, 343.18164, 263.62036, 256.22528, 309.85014, 288.59665,
235.3395, 314.77392, 321.25776, 300.35306, 371.13057, 259.11446,
258.48696, 306.43744, 323.78058, 280.04595, 297.34385, 263.67336,
329.24681, 233.97186, 346.51321, 286.97003, 306.86708, 297.15846,
337.82102, 262.12682, 291.70902, 281.91251, 297.9659, 217.23621,
359.04224, 265.45122, 324.9777, 334.42103, 237.77726, 228.74243,
252.22045, 290.07852, 283.7953, 270.72377, 295.45991, 302.80527,
304.19675, 268.4273, 244.94332, 263.11038, 207.48832, 326.08576,
312.77998, 318.79277, 244.6571, 332.0414, 259.87963, 296.28998,
239.8572, 248.63712, 312.60629, 247.72389, 242.67078, 271.98961,
322.73757, 197.99485, 342.45941, 246.40288, 246.54248, 270.31797,
279.19136, 283.27782, 256.21676, 298.22446, 245.34207, 363.90223,
206.35297, 297.69943, 250.76662, 299.09484, 296.26682, 217.5044,
323.62796, 290.34726, 246.24697, 290.60326, 313.243, 254.3545,
273.66298, 265.30058, 304.26679, 309.53028, 310.93268, 318.41836,
318.59416, 344.05085, 247.21871, 319.13724, 314.71633, 262.51803,
277.58658, 240.33522, 283.6671, 315.50305, 284.70172, 260.21957,
247.34008, 347.11825, 315.84204, 310.46298, 278.06065, 264.27623,
336.07573, 326.01364, 245.67, 149.19113, 298.48698, 222.68479,
303.39236, 305.14083, 303.49802, 261.84537, 341.96969, 334.29589,
221.43909, 307.37339, 258.84784, 319.13495, 241.58692, 239.09068,
242.53418, 330.78695, 280.48138, 310.41672, 263.1502, 267.58253,
187.53822, 262.62308, 295.44728, 266.01227, 313.69862, 253.52788,
256.41986, 323.6009, 337.53842, 333.99872, 303.51809, 312.28169,
294.92838, 300.70826, 300.24336, 187.73782, 276.88791, 286.49432,
292.1261, 254.85939, 277.5201, 243.95193, 251.41231, 300.05949,
307.55581, 308.63016, 292.26692, 322.59732, 314.09203, 279.01204,
104.22403, 282.82249, 310.19937, 265.4532, 298.59688, 327.82834,
216.9004, 304.42974, 308.75751, 353.56875, 323.77259, 301.02944,
243.21455, 299.93867, 239.32609, 288.85644, 321.70498, 300.7652,
214.00534, 330.58371, 218.42779, 226.38351, 269.54573, 281.0985,
313.38796, 315.12442, 338.55295, 287.76669, 276.50012, 283.05571,
293.54309, 313.50773, 251.84096, 306.30878, 298.6039, 282.81368,
280.57491, 246.21204, 228.88241, 278.85273, 250.53517, 308.06586,
337.51522, 322.46427, 249.82595, 326.33555, 223.46005, 292.20952,
326.24258, 284.81759, 361.99513, 225.74007, 265.54327, 301.95898,
289.31911, 306.88864, 312.2168, 227.28386, 288.40244, 274.2207,
281.3788, 295.98457, 291.69465, 233.11237, 327.41621, 238.52888,
246.48451, 272.98087, 300.54158, 265.74598, 205.12706, 296.53577,
350.87861, 247.59947, 315.72462, 285.2722, 269.48033, 243.84168,
283.6546, 275.47676, 290.5681, 252.84893, 341.91242, 268.79573,
304.94915, 251.56578, 263.95641, 243.55776, 338.59661, 303.00255,
212.9379, 292.68837, 294.13636, 388.07221, 303.7387, 313.72581,
355.13609, 317.09882, 331.7978, 218.43844, 262.96991, 306.87193,
268.31627, 265.33588, 320.67174, 247.42967, 276.86054, 273.34922,
348.14517, 267.12308, 306.68901, 177.57379, 242.70165, 216.52883,
255.14138, 224.94714, 252.34893, 235.99393, 311.64778, 336.18343,
287.39269, 336.26041, 289.42778, 192.66316, 276.65478, 306.98647,
198.9643, 302.39132, 182.62885, 346.2712, 309.66977, 264.90717,
302.69623, 264.56688, 277.51619, 339.16197, 280.54847, 277.54282,
283.37459, 292.15455, 292.34224, 252.94767, 301.60766, 301.46605,
289.27154, 286.94408, 288.05704, 233.64855, 304.36141, 278.99481,
260.61019, 257.88718, 279.99577, 269.7465, 293.24509, 323.32979,
315.02618, 246.47989, 327.3044, 274.86892)), row.names = c(29L,
47L, 59L, 116L, 125L, 155L, 238L, 245L, 335L, 354L, 393L, 410L,
420L, 443L, 478L, 514L, 584L, 665L, 676L, 720L, 737L, 766L, 782L,
839L, 840L, 842L, 857L, 866L, 885L, 887L, 1017L, 1041L, 1083L,
1086L, 1153L, 1207L, 1219L, 1231L, 1276L, 1283L, 1389L, 1405L,
1423L, 1469L, 1517L, 1525L, 1544L, 1686L, 1766L, 1769L, 1773L,
1841L, 1854L, 1885L, 1925L, 1932L, 1956L, 1992L, 2067L, 2094L,
2105L, 2169L, 2262L, 2291L, 2293L, 2310L, 2349L, 2353L, 2358L,
2385L, 2441L, 2456L, 2490L, 2516L, 2545L, 2552L, 2557L, 2676L,
2683L, 2702L, 2733L, 2761L, 2801L, 2851L, 2876L, 2881L, 2882L,
2913L, 2927L, 2928L, 2991L, 3001L, 3018L, 3042L, 3071L, 3209L,
3239L, 3296L, 3298L, 3371L, 3502L, 3546L, 3559L, 3560L, 3561L,
3566L, 3589L, 3659L, 3669L, 3714L, 3717L, 3738L, 3744L, 3765L,
3768L, 3778L, 3782L, 3826L, 3840L, 3842L, 3848L, 3894L, 3968L,
3972L, 3984L, 4007L, 4064L, 4103L, 4160L, 4209L, 4212L, 4337L,
4377L, 4432L, 4671L, 4674L, 4675L, 4686L, 4714L, 4724L, 4761L,
4773L, 4853L, 4871L, 4908L, 4931L, 4993L, 5000L, 5039L, 5055L,
5081L, 5100L, 5173L, 5260L, 5265L, 5281L, 5331L, 5368L, 5412L,
5419L, 5435L, 5476L, 5482L, 5485L, 5500L, 5561L, 5591L, 5618L,
5629L, 5641L, 5745L, 5767L, 5783L, 5833L, 5876L, 5884L, 5933L,
5938L, 5943L, 5960L, 5997L, 6000L, 6016L, 6021L, 6109L, 6116L,
6124L, 6135L, 6157L, 6166L, 6172L, 6173L, 6176L, 6181L, 6214L,
6216L, 6226L, 6229L, 6264L, 6280L, 6282L, 6315L, 6330L, 6358L,
6366L, 6368L, 6375L, 6397L, 6463L, 6541L, 6543L, 6598L, 6604L,
6644L, 6649L, 6669L, 6690L, 6692L, 6702L, 6711L, 6716L, 6718L,
6725L, 6740L, 6769L, 6775L, 6781L, 6829L, 6841L, 6853L, 6860L,
6872L, 6890L, 6891L, 6902L, 6905L, 6913L, 6914L, 6927L, 6934L,
6937L, 6947L, 6965L, 6980L, 6981L, 7021L, 7072L, 7094L, 7104L,
7121L, 7136L, 7138L, 7148L, 7149L, 7168L, 7169L, 7222L, 7258L,
7263L, 7266L, 7292L, 7347L, 7350L, 7360L, 7382L, 7390L, 7419L,
7423L, 7455L, 7463L, 7471L, 7482L, 7509L, 7528L, 7543L, 7577L,
7583L, 7587L, 7593L, 7615L, 7616L, 7617L, 7633L, 7650L, 7654L,
7674L, 7688L, 7706L, 7718L, 7723L, 7748L, 7766L, 7797L, 7811L,
7813L, 7820L, 7839L, 7893L, 7899L, 7910L, 7926L, 7944L, 7945L,
7947L, 7954L, 7955L, 7959L, 7982L, 7990L, 8025L, 8031L, 8041L,
8044L, 8049L, 8062L, 8069L, 8072L, 8085L, 8090L, 8110L, 8122L,
8134L, 8141L, 8193L, 8196L, 8201L, 8203L, 8208L, 8233L, 8234L,
8236L, 8271L, 8273L, 8276L, 8284L, 8307L, 8328L, 8333L, 8354L,
8367L, 8386L, 8412L, 8421L, 8441L, 8452L, 8478L, 8488L, 8504L,
8509L, 8518L, 8534L, 8548L, 8558L, 8574L, 8578L, 8585L, 8620L,
8654L, 8660L, 8662L, 8663L, 8704L, 8707L, 8733L, 8751L, 8757L,
8817L, 8822L, 8824L, 8831L, 8840L, 8841L, 8851L, 8858L, 8877L,
8904L, 8919L, 8944L, 8945L, 8951L, 8954L, 8959L, 8966L, 8986L,
9049L, 9058L, 9075L, 9088L, 9111L, 9124L, 9132L, 9138L, 9148L,
9171L, 9183L, 9188L, 9207L, 9257L, 9280L, 9284L, 9288L, 9292L,
9316L, 9335L, 9341L, 9354L, 9392L, 9428L, 9439L, 9451L, 9483L,
9484L, 9493L, 9498L, 9532L, 9542L, 9566L, 9569L, 9582L, 9592L,
9613L, 9628L, 9634L, 9643L, 9647L, 9659L, 9665L, 9687L, 9697L,
9708L, 9713L, 9762L, 9773L, 9815L, 9825L, 9829L, 9830L, 9839L,
9842L, 9848L, 9854L, 9865L, 9868L, 9891L, 9896L, 9925L, 9945L,
10012L, 10023L, 10035L, 10046L, 10059L, 10067L, 10096L, 10105L,
10106L, 10137L, 10146L, 10154L, 10173L, 10178L, 10195L, 10201L,
10202L, 10233L, 10243L, 10259L, 10273L, 10277L, 10287L, 10288L,
10315L, 10316L, 10342L, 10345L, 10348L, 10354L, 10361L, 10372L,
10417L, 10420L, 10430L, 10454L, 10476L, 10477L, 10490L, 10498L,
10499L, 10532L, 10533L, 10550L, 10553L, 10557L, 10560L, 10572L,
10616L, 10663L, 10667L, 10687L, 10712L), class = "data.frame")
and I want to add another column here with values taken from another data. The other data is this one:
> dput(new)
structure(list(CNTRYID = structure(1:2, .Label = c("Czech Republic",
"Denmark", "France", "Greece", "Hungary", "Italy", "Lithuania",
"Netherlands", "Poland", "Slovak Republic", "Slovenia", "Spain",
"United Kingdom"), class = "factor"), Freq = c(184, 518), Mean = c(299.71,
294.09), s.e. = c(4.39, 2.28), SD = c(36.73, 44.8), s.e = c(3.4,
2.12)), row.names = 1:2, class = c("intsvy.mean", "data.frame"
))
What I want to do is to take the value under Mean from the second data.frame that is associated with, e.g. the Czech Republic, and put it in the first data.frame so that everywhere where CNTRYID is Czech Republic the value 299.71 to appear in a new column, say, test$MEAN. Then 294.09 to appear in every row where the CNTRYID is Denmark and so on. You might require package "intsvy" since one of the classes here is "intsvy.mean". I have extracted this from a large dataset I am working with, so there might be some additional labels etc. but, overall, you should be able to reproduce it. Thank you in advance!
You are looking for merge.
result <- merge(test,new,by="CNTRYID")

How to transform data for bar graph based on multi choice?

So I'm learning to use R/GGplot, it was simple enough to create a single bar chart but I'm struggling to understand how to properly manipulate the data to get the chart I want.
So I have a basic example data file that looks like this in RStudio:
Basically, I wanted to make a bar for each option, which counts the "Yes" options. The Y axis would then be equal to the total number of records, with the scale measured in %.
Here is where I think I went completely wrong:
data_Q1 <- data.frame(Q1 = c("Red", "Blue", "Green", "Yellow", "Pink"))
I believe here I might need to remove the "No" level, then rename the "Yes" level to the colour name before I can work with it, but I seem to be greatly misunderstanding how I do this.
I've tried using droplevels() and raw_data$Q1_1[grepl("Yes", raw_data$Q1, ignore.case=T)] <- "Red" but neither seem to achieve the goal I want.
Here is the code I'm using for the graph:
ggplot(
data_Q1,
aes(
x=Q1,
y=sum(
complete.cases(raw_data)
)
)
)
+geom_bar(
aes(
fill=Q1
),
colour="black",
stat="identity"
)
+labs(
title="Colours respondents liked",
subtitle="Q1. Select all the colours you like",
caption="source: example data"
)
+ylab("Total completes")
+scale_y_continuous(labels = scales::percent)
structure(list(id = 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, 100, 101, 102, 103, 104, 105, 106,
107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132,
133, 134, 135, 136, 137, 138, 139), Q1_1 = structure(c(2L, 1L,
2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
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1L, 2L,
2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L,
2L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L,
1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L,
2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L,
1L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L,
2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L), .Label = c("No", "Yes"
), class = "factor")), row.names = c(NA, -139L), variable.labels = c(id = "id",
Q1_1 = "[Red] Q1. Select all the colours you like", Q1_2 = "[Blue] Q1. Select all the colours you like",
Q1_3 = "[Green] Q1. Select all the colours you like", Q1_4 = "[Yellow] Q1. Select all the colours you like",
Q1_5 = "[Pink] Q1. Select all the colours you like"), codepage = 65001L, class = "data.frame")
Okay, I am assuming this is from some survey, so your data is (probably) a bit messy.
An approach with tidyverse and transposing (t()) would look something like this:
library(tidyverse)
df <- raw_data
df2 <- data.frame(t( df %>% summarise(Q1 = sum(Q1_1=="Yes") / length(complete.cases(df)) ,
Q2 = sum(Q1_2=="Yes") / length(complete.cases(df)),
Q3 = sum(Q1_3=="Yes") / length(complete.cases(df)),
Q4 = sum(Q1_4=="Yes") / length(complete.cases(df)),
Q5 = sum(Q1_5=="Yes") / length(complete.cases(df)),) ))
names(df2) <- ("sum_yes")
df2$q <- rownames(df2)
Output of df2:
> df2
sum_yes q
Q1 0.8417266 Q1
Q2 0.7338129 Q2
Q3 0.7122302 Q3
Q4 0.4820144 Q4
Q5 0.7122302 Q5
>
Then, we plot the results:
library(ggplot2)
ggplot(
df2,
aes(
x=q,
y = sum_yes,
fill = q
)
) +
geom_bar(
colour="black",
stat="identity"
) + labs(
title="Colours respondents liked",
subtitle="Q1. Select all the colours you like",
caption="source: example data"
) + ylab("Total completes")+scale_y_continuous(labels = scales::percent) +
scale_fill_manual(values= c("Red", "Blue", "Green", "Yellow", "Pink") )
with scale_fill_manual, you can just specify what the colors of your results should be.
If I understood the data correctly, you would need to replace the names (Q1 to Q5 ) with the respective colors.

How to determine similar values around a particular row in R?

Goal
I want to find the duration of lane changes of vehicles as shown in the plot, using lateral position data.
Data
Following is the data for only one vehicle:
> dput(a)
structure(list(Frame.ID = 526:1058, xcoord = c(14.346, 14.367,
14.388, 14.419, 14.458, 14.503, 14.55, 14.6, 14.65, 14.702, 14.754,
14.807, 14.86, 14.913, 14.966, 15.02, 15.072, 15.125, 15.178,
15.23, 15.282, 15.333, 15.384, 15.434, 15.482, 15.529, 15.574,
15.617, 15.657, 15.694, 15.727, 15.755, 15.78, 15.802, 15.823,
15.841, 15.858, 15.874, 15.889, 15.903, 15.917, 15.93, 15.942,
15.955, 15.967, 15.978, 15.989, 16, 16.011, 16.022, 16.033, 16.044,
16.055, 16.065, 16.075, 16.085, 16.095, 16.104, 16.112, 16.12,
16.129, 16.139, 16.151, 16.164, 16.178, 16.195, 16.212, 16.231,
16.25, 16.27, 16.291, 16.312, 16.333, 16.356, 16.379, 16.403,
16.428, 16.455, 16.482, 16.511, 16.542, 16.574, 16.609, 16.646,
16.687, 16.732, 16.783, 16.839, 16.902, 16.967, 17.033, 17.1,
17.168, 17.232, 17.294, 17.354, 17.41, 17.464, 17.513, 17.559,
17.6, 17.636, 17.665, 17.685, 17.694, 17.7, 17.708, 17.725, 17.751,
17.782, 17.817, 17.856, 17.897, 17.939, 17.982, 18.025, 18.067,
18.108, 18.145, 18.178, 18.207, 18.232, 18.255, 18.274, 18.292,
18.308, 18.323, 18.336, 18.349, 18.361, 18.372, 18.383, 18.393,
18.403, 18.413, 18.422, 18.432, 18.441, 18.451, 18.46, 18.469,
18.479, 18.488, 18.496, 18.505, 18.513, 18.521, 18.529, 18.537,
18.544, 18.55, 18.556, 18.562, 18.567, 18.574, 18.58, 18.588,
18.597, 18.609, 18.623, 18.64, 18.662, 18.69, 18.722, 18.76,
18.802, 18.849, 18.899, 18.953, 19.012, 19.076, 19.144, 19.218,
19.299, 19.386, 19.479, 19.574, 19.669, 19.763, 19.855, 19.945,
20.031, 20.112, 20.187, 20.254, 20.31, 20.352, 20.385, 20.412,
20.435, 20.45, 20.455, 20.449, 20.436, 20.416, 20.39, 20.361,
20.328, 20.293, 20.256, 20.217, 20.178, 20.139, 20.1, 20.063,
20.026, 19.99, 19.957, 19.925, 19.895, 19.867, 19.842, 19.819,
19.796, 19.774, 19.751, 19.729, 19.707, 19.685, 19.662, 19.64,
19.617, 19.594, 19.571, 19.547, 19.523, 19.499, 19.473, 19.449,
19.426, 19.404, 19.382, 19.359, 19.336, 19.312, 19.288, 19.263,
19.237, 19.211, 19.184, 19.156, 19.127, 19.097, 19.066, 19.033,
18.998, 18.961, 18.921, 18.878, 18.831, 18.781, 18.727, 18.67,
18.612, 18.554, 18.498, 18.446, 18.397, 18.349, 18.304, 18.264,
18.233, 18.21, 18.194, 18.182, 18.175, 18.171, 18.17, 18.172,
18.177, 18.183, 18.192, 18.202, 18.213, 18.226, 18.241, 18.258,
18.277, 18.298, 18.321, 18.346, 18.371, 18.396, 18.422, 18.447,
18.471, 18.495, 18.518, 18.54, 18.559, 18.577, 18.591, 18.601,
18.606, 18.605, 18.6, 18.593, 18.584, 18.579, 18.58, 18.59, 18.607,
18.629, 18.655, 18.682, 18.711, 18.739, 18.766, 18.792, 18.818,
18.842, 18.864, 18.885, 18.905, 18.924, 18.943, 18.961, 18.98,
19, 19.02, 19.038, 19.054, 19.068, 19.081, 19.092, 19.103, 19.112,
19.121, 19.129, 19.137, 19.144, 19.15, 19.156, 19.161, 19.166,
19.169, 19.172, 19.173, 19.173, 19.171, 19.168, 19.163, 19.156,
19.147, 19.136, 19.123, 19.109, 19.093, 19.078, 19.061, 19.041,
19.017, 18.988, 18.954, 18.918, 18.878, 18.836, 18.795, 18.756,
18.722, 18.693, 18.671, 18.655, 18.642, 18.633, 18.625, 18.619,
18.613, 18.608, 18.602, 18.593, 18.58, 18.562, 18.537, 18.504,
18.46, 18.403, 18.33, 18.234, 18.115, 17.972, 17.806, 17.623,
17.427, 17.223, 17.013, 16.802, 16.592, 16.389, 16.191, 15.998,
15.806, 15.604, 15.386, 15.149, 14.891, 14.617, 14.328, 14.029,
13.722, 13.412, 13.097, 12.773, 12.436, 12.084, 11.723, 11.361,
11.006, 10.663, 10.334, 10.02, 9.723, 9.453, 9.219, 9.027, 8.874,
8.753, 8.657, 8.583, 8.525, 8.481, 8.448, 8.421, 8.4, 8.384,
8.371, 8.36, 8.351, 8.345, 8.338, 8.33, 8.319, 8.304, 8.284,
8.258, 8.224, 8.183, 8.136, 8.084, 8.029, 7.971, 7.912, 7.853,
7.794, 7.736, 7.681, 7.629, 7.581, 7.54, 7.506, 7.482, 7.468,
7.46, 7.459, 7.462, 7.468, 7.477, 7.489, 7.501, 7.514, 7.526,
7.539, 7.55, 7.562, 7.573, 7.584, 7.595, 7.607, 7.62, 7.636,
7.654, 7.675, 7.702, 7.734, 7.773, 7.823, 7.885, 7.96, 8.046,
8.134, 8.213, 8.278, 8.322, 8.342, 8.338, 8.308, 8.258, 8.192,
8.112, 8.023, 7.927, 7.827, 7.725, 7.623, 7.522, 7.424, 7.334,
7.252, 7.183, 7.128, 7.093, 7.078, 7.085, 7.117, 7.177, 7.267,
7.385, 7.525, 7.679, 7.839, 8, 8.155, 8.296, 8.418, 8.519, 8.606,
8.682, 8.749, 8.82, 8.891, 8.956, 9.012, 9.057, 9.09, 9.126,
9.162, 9.197, 9.227, 9.249, 9.257, 9.254, 9.251, 9.247), Lane = 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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lane change duration, I want to start from the row where the target lane number is reported first time. In the example the target lane is 1 and the number 1 is reported first time when the vehicle touches the pavement marking, as shown in the plot. This point can be added in the data as:
a$rows <- as.numeric(row.names(a)) # Row numbers
a$lch <- a$xcoord[match(head(which(a$Lane==1),1), a$rows)]
For this point I want to find the difference from every other point.
a$difference <- abs(a$lch - a$xcoord)
Then, comparing the differences in both directions, there will be some rows where the differences will be quite similar indicating that lateral position was almost similar. The first instances in both directions will be treated as the limits of lane change maneuver.
How can I achieve this comparison part in R? I don't know how to 'move' from Frame.ID 929 (this example) upwards and downwards to compare where the differences become similar.
Not sure if it's the best/fastest solution, but the idea is to calculate the differences between two consecutive "difference" columns you created and pick a small value as a threshold to flag that they started becoming similar. Then get the row closest to the time point of the change for each lane.
Note that I renamed your dataset as dt. Run the process step by step to see how the process works. If you are happy with it you can make the script shorter by combining some commands.
dt$rows <- as.numeric(row.names(dt)) # Row numbers
dt$lch <- dt$xcoord[match(head(which(dt$Lane==1),1), dt$rows)]
dt$difference <- abs(dt$lch - dt$xcoord)
library(dplyr)
dt %>%
mutate(frameIDchange = Frame.ID[difference==0], # spot the frame ID of the change
diff_diff = difference - lag(difference, default=difference[1]), # find the difference of two consequtive differences
flag = ifelse(abs(diff_diff) <= 0.01,1,0)) %>% # flag if the difference of two consequtive differences is equal or less to our threshold (0.01)
filter(flag==1) %>% # keep only rows with threshold difference
mutate(frameIDdiff = abs(Frame.ID-frameIDchange)) %>% # calculate distance of frame IDs
group_by(Lane) %>% # for each lane
filter(frameIDdiff== min(abs(frameIDdiff))) # return the frame id closest to the change frame id with a difference equal or less to our threshold
# Frame.ID xcoord Lane rows lch difference frameIDchange diff_diff flag frameIDdiff
# 1 896 18.593 2 371 12.436 6.157 929 -0.009 1 33
# 2 953 8.351 1 428 12.436 4.085 929 0.009 1 24
This tells you that the frame ids where the differences started to become similar (0.01 threshold) are 896 and 953 for lanes 2 and 1 respectively. Obviously the frame id of the change (929) belongs between those frames as expected.
You can also experiment with slightly higher/less threshold values to see how the results change.

How to extract random effects and variance components from lme4 wrapped in dlply

This post How can I extract elements from lists of lists in R? answers some of my questions but that still doesn't quite work for me and what I need to do goes beyond my R knowledge.
I have data from field trials in 2 environments (=trials), 2 years and 5 traits of interest (defined by trait_id). GID is the unique line identifier. My model in lme4 is:
mods <- dlply(data,.(trial,trait_id),
function(d)
lmer(phenotype_value ~(1|GID)+(1|year)+(1|year:GID)+(1|year:rep),
na.action = na.omit,data=d))
Running this returns a large list of 10 elements and I would like to store the random effects for GID for all traits per trial in a data frame. I tried several things:
blup=lapply(mods,ranef, drop = FALSE)
blup1=blup[[1]]
blup2=blup1$GID
will give me a df with the random effects for one trait per trial, I was hoping for something more streamlined that will preserve some of info like $irrigation.GRYLD in the column names.
Here is a reproducible example with only two traits (GRYLD, PTHT), 2 years (11OBR, 12OBR), and two reps:
structure(list(GID = structure(c(1L, 2L, 3L, 4L, 5L, 5L, 1L,
2L, 4L, 3L, 1L, 2L, 3L, 4L, 5L, 5L, 1L, 2L, 4L, 3L, 1L, 2L, 3L,
4L, 5L, 5L, 2L, 1L, 4L, 3L, 1L, 2L, 3L, 4L, 5L, 5L, 2L, 1L, 4L,
3L, 1L, 2L, 3L, 4L, 5L, 5L, 1L, 2L, 4L, 3L, 1L, 2L, 3L, 4L, 5L,
5L, 1L, 2L, 4L, 3L, 1L, 2L, 3L, 4L, 5L, 5L, 2L, 1L, 4L, 3L, 1L,
2L, 3L, 4L, 5L, 5L, 2L, 1L, 4L, 3L), .Label = c("A", "B", "C",
"D", "E"), class = "factor"), year = 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, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L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2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("GRYLD",
"PTHT"), class = "factor"), phenotype_value = c(3.93, 3.38, 1.65,
4.33, 2.45, 2.48, 3.98, 3.3, 4.96, 1.53, 87.5, 69.5, 65.5, 84.5,
77, 81, 94.5, 84.5, 89, 81, 6.56, 4.3, 5.76, 7.3, 5.73, 4.14,
5.93, 6.96, 8.43, 5.81, 114.5, 100, 104.5, 110, 110, 106, 99,
97.5, 105, 100, 0.119, 0.131, 0.681, 0.963, 0.738, 1.144, 0.194,
0.731, 0.895, 0.648, 35, 50, 45, 50, 45, 50, 55, 45, 50, 55,
2.79, 3.73, 3.96, 4.64, 5.03, 2.94, 3.78, 4.14, 3.89, 3.21, 90,
95, 105, 100, 105, 85, 95, 100, 100, 95)), .Names = c("GID",
"year", "trial", "rep", "trait_id", "phenotype_value"), class = "data.frame", row.names = c(NA,
-80L))
I'm not quite sure what you want as an output format, but how about:
all_ranef <- function(object) {
rr <- ranef(object)
ldply(rr,function(x) data.frame(group=rownames(x),x,check.names=FALSE))
}
ldply(mods,all_ranef)
## trial trait_id .id group (Intercept)
## 1 heat GRYLD year:GID 11OBR:A 7.935352e-01
## 2 heat GRYLD year:GID 11OBR:B 1.960487e-01
## 3 heat GRYLD year:GID 11OBR:C -1.504116e+00
## ...
## 82 irrigation PTHT year:rep 12OBR:2 -1.595022e+00
## 83 irrigation PTHT year 11OBR 2.915033e+00
## 84 irrigation PTHT year 12OBR -2.915033e+00
this works reasonably well because all of your random effects are intercept-only. If you had some random-slopes terms in the models you might either want to reshape2:::melt() the individual random effects, or use rbind.fill() to combine data frames with different random-effects columns.
library("ggplot2"); theme_set(theme_bw())
ggplot(vals, aes(y=group,x=`(Intercept)`))+
geom_point(aes(colour=interaction(trial,trait_id)))+
facet_wrap(~.id,scale="free")
By the way, it's usually inadvisable to use a factor with only 2 levels (YEAR) as a grouping variable ...

Resources