[R] Running segmented on grouped data and collating model parameters in a data frame
Sumitrajit Dhar
@-dh@r @ending from northwe@tern@edu
Sun Jun 3 18:03:21 CEST 2018
Hi Folks,
I am trying to teach myself how to solve the problem described below but am
running out of time. Hence the plea for help. Thanks in advance.
Here is my data frame.
> t
# A tibble: 12 x 12
subject ageGrp ear hearingGrp sex freq L2 Ldp Phidp NF
SNR Ldp_p
<fct> <fct> <fct> <fct> <fct> <int> <int> <dbl> <dbl> <dbl>
<dbl> <dbl>
1 HALAF573 A L A F 2 0 -19.6 197. -28.5
8.88 2.10e-6
2 HALAF573 A L A F 2 2 -18.7 203. -22.0
3.25 2.31e-6
3 HALAF573 A L A F 2 4 -29.1 255. -27.4
-1.64 7.04e-7
4 HALAF573 A L A F 2 6 -12.4 174. -12.2
-0.206 4.78e-6
5 HALAF573 A L A F 4 0 -28.6 232. -26.7
-1.87 7.45e-7
6 HALAF573 A L A F 4 2 -27.2 351. -28.8
1.59 8.68e-7
7 HALAF573 A L A F 4 4 -20.4 26.2 -35.0
14.6 1.92e-6
8 HALAF573 A L A F 4 6 -20.0 85.1 -29.8
9.75 2.00e-6
9 HALAF573 A L A F 8 0 -22.8 39.2 -22.1
-0.689 1.45e-6
10 HALAF573 A L A F 8 2 -14.5 13.4 -20.7
6.23 3.76e-6
11 HALAF573 A L A F 8 4 -17.3 345. -21.6
4.30 2.73e-6
12 HALAF573 A L A F 8 6 -14.1 320. -21.7
7.59 3.94e-6
# Note that there are more levels of L2 (31 in total) and 344 other
subjects but I truncated the frame for posting.
# I want to do this: t %>% group_by(freq) %>% [run segmented] %>% [create
a data frame with [subject, freq, breakpoint1, breakpoint2, slope1, slope2,
slope3, L2 when Ldp_p == 0].
# Also note that ultimately I will be grouping by "subject, freq".
# I can run the models and get believable results. The following run on a
data frame with L2 between 0 and 60.
out.lm <- lm(Ldp_p ~ L2, data = t)
o <- segmented(out.lm, seg.Z = ~L2, psi = list(L2 = c(20,45)),
control = seg.control(display = FALSE)
o$psi
# Initial Est. St.Err
#psi1.L2 20 30.78256 0.5085192
#psi2.L2 45 53.16390 0.4671701
slope(o)
slope(o)
$L2
# Est. St.Err. t value CI(95%).l CI(95%).u
#slope1 1.2060e-06 1.6606e-07 7.2622 8.6397e-07 1.5480e-06
#slope2 1.0708e-05 2.9196e-07 36.6770 1.0107e-05 1.1309e-05
#slope3 -4.5791e-06 1.3694e-06 -3.3439 -7.3995e-06 -1.7588e-06
Thanks again for bailing me out.
Regards,
Sumit
[[alternative HTML version deleted]]
More information about the R-help
mailing list