'termlabels' error in coxph() function of survival package - r

I am trying to analyse a reoccurring event dataset and I am struggling to fit the model.
A subset of my data:
outdat <- structure(list(yr = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), jday = c(141, 151, 156, 157, 157, 158, 164, 168, 168, 168, 168, 168, 168, 170, 171, 171, 177, 177, 177, 178, 179, 181, 182, 182, 182, 182, 182, 184, 186, 188, 188, 188, 189, 191, 197, 197, 197, 198, 198, 199, 209, 211, 217, 223, 230, 161, 187, 196, 196, 196, 197, 197, 201, 204, 204, 204, 208, 209, 211, 212, 215, 215, 219, 221, 222, 225, 229, 229, 245, 252, 256, 159, 160, 166, 172, 174, 174, 178, 178, 178, 178, 178, 179, 182, 185, 185, 186, 186, 187, 187, 187, 187, 187, 187, 188, 188, 192, 195, 195, 195, 195, 195, 196, 196, 196, 200, 200, 200, 200, 202, 203, 204, 207, 207, 207, 207, 207, 207, 207, 208, 212, 212, 226), out = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)), row.names = c(NA, -123L), class = c("tbl_df", "tbl", "data.frame"))
The data is showing an occurrence of crop disease in a country and I am trying to first the Cox PH model. The survival package manual recommends defining the term cluster() in the formula.
cm <- coxph(Surv(time =jday, event = out ) ~ cluster(yr), data = outdat)
Error in reformulate(temp[1 - tcl]) :
'termlabels' must be a character vector of length at least one
If I do not define covariate as a grouping variable the model is fitted.
cm <- coxph(Surv(time =jday, event = out ) ~ yr, data = outdat)
Note that I have tried changing the class of outdat$yr to character/factor.

A cluster() term adjusts the standard errors of the hazards, but since it's the only term on the right hand side of the formula, you have no covariates to adjust. You need at least one covariate. And I wouldn't include the cluster term unless you had a specific reason to.
See here for more info

Related

LASSO regression for categorical variables [migrated]

This question was migrated from Stack Overflow because it can be answered on Cross Validated.
Migrated 24 days ago.
Suppose there are several categorical variables included in the LASSO regression.
For a categorical variable with more than two factors, it is mandatory to create a dummy table.
For example, the categorical variable is vaccination status (Vacc_Stat), in which there are three categories,i.e., 1 = not vaccinated, 2 = partially vaccinated, and 3 = fully vaccinated.
Using the model. matrix function for the vaccination status variable will yield two dummy columns because the value 1= not vaccinated is the reference.
If the final result of the LASSO regression coefficient is as follows
Vacc_Stat1 .
Vacc_Stat2 .
Vacc_Stat3 -4.208877e-01
Do we use the Vacc_Stat3 only, or we used the Vacc_Stat variable as a whole?
I am planning to do a LASSO regression followed by a logistic regression of the remaining variables selected through LASSO regression.
Thank you in advance.
I am expecting that if one of the dummy variables is included in the LASSO regression, then we used the original categorical variable as a whole.
The following is the minimal reproducible dataset
structure(c(0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1,
1, 0, 1, 1, 1, 0, 0, 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, 1, 1, 1, 1, 1, 0, 0, 0,
0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0,
1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1,
1, 1, 1, 1, 1, 0, 1, 0, 0.34, 0.49, 38, 0.58, 0.2, 0.49, 0.65,
40.57, 2.08, 49.52, 50.77, 38.04, 76.55, 55.95, 53.23, 38.04,
99.72, 80.04, 92.41, 47, 66, 70, 52, 36, 39, 67, 42, 23, 66,
37.109375, 31.22945431, 26.2345679, 20.76124567, 35.3798127,
26.44628099, 23.87511478, 24.8015873, 21.49959688, 22.47120876,
110, 159, 127, 100, 120, 115, 100, 112, 130, 119, 72, 78, 80,
72, 80, 73, 76, 75, 80, 78, 84, 86, 88, 103, 90, 91, 90, 82,
88, 105, 36, 37, 36.5, 36, 38, 38, 36, 36.4, 37, 36, 20, 40,
24, 20, 22, 24, 18, 20, 22, 20, 90, 99, 98, 99, 96, 90, 98, 99,
99, 90, 7, 5, 0, 2, 7, 10, 3, 3, 2, 2, 11.7, 13.8, 13, 10.9,
11.6, 14.5, 15, 16.2, 12.3, 14.2, 3.9, 4.2, 3.6, 4.7, 4, 3.2,
4.4, 5.1, 3, 3.78, 15.7, 28.8, 6, 7.8, 37.6, 13.9, 26.6, 27.2,
33, 23, 138, 139, 121, 135, 139, 132, 133, 138, 137, 128, 75,
64.5, 87.4, 88.9, 47.1, 78, 61.8, 62.52, 56.3, 63.2, 753, 305,
250, 267, 315, 207, 285, 293, 366, 307, 8.7, 8.1, 11.2, 75.9,
13.7, 10.03, 42.2, 10, 9, 10.6, 11.07, 6.8, 1.18, 23.18, 4.33,
5.25, 8.73, 7.44, 8.01, 10.37), dim = c(10L, 76L), dimnames =
list(
c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10"),
c("TEST.Year2",
"TEST.Year3", "TEST.Gender2", "TEST.Vacc_Stat1",
"TEST.Vacc_Stat2",
"TEST.Vacc_Stat3", "TEST.Risk_AI2", "TEST.Risk_Obesity2",
"TEST.Risk_Smoking2",
"TEST.Risk_HT2", "TEST.Risk_DM2", "TEST.Risk_Asthma2",
"TEST.Risk_CHD2",
"TEST.Risk_CVD2", "TEST.Risk_COPD2", "TEST.Risk_TBC2",
"TEST.Risk_CKD2",
"TEST.Risk_CLD2", "TEST.Risk_Brain2", "TEST.Risk_HIV2",
"TEST.Risk_Cancer2",
"TEST.Symptom_Fever2", "TEST.Symptom_Cough2",
"TEST.Symptom_Sore_Throat2",
"TEST.Symptom_Rinnorrhea2", "TEST.Symptom_Anosmia2",
"TEST.Symptom_Myalgia2",
"TEST.Symptom_Headache2", "TEST.Symptom_Malaise2",
"TEST.Symptom_Anorexia2",
"TEST.Symptom_Diarrhea2", "TEST.Symptom_Nausea2",
"TEST.Symptom_Vomitting2",
"TEST.Symptom_Abd_Pain2", "TEST.Symptom_Dyspneu2",
"TEST.Symptom_Chest_Pain2",
"TEST.Symptom_LOC2", "TEST.Lab_RT_PCR2", "TEST.CXR_Proj2",
"TEST.CXR_Proj3",
"TEST.CXR_Pneumonia1", "TEST.CXR_Pneumonia2",
"TEST.CXR_Effusion2",
"TEST.Co_Septic2", "TEST.Co_Septic_Shock2", "TEST.Co_ARDS2",
"TEST.Co_Sx_Infection2", "TEST.Severity_Adm2",
"TEST.Severity_Adm3",
"TEST.Severity_Adm4", "TEST.Severity_Adm_Cat_12",
"TEST.Severity_Adm_Cat_22",
"TEST.Severity_Adm_Cat_32", "TEST.Severity_Worst2",
"TEST.Severity_Worst3",
"TEST.Severity_Worst4", "TEST.Progression2", "TEST.CXR_ALA_Num",
"TEST.CXR_Prob_Num", "TEST.Age", "TEST.BMI", "TEST.Vital_SBP",
"TEST.Vital_DBP", "TEST.Vital_PR", "TEST.Vital_Temp",
"TEST.Vital_RR",
"TEST.Vital_SpO2", "TEST.Symptom_Onset", "TEST.Lab_Hb",
"TEST.Lab_K",
"TEST.Lab_Lim", "TEST.Lab_Na", "TEST.Lab_Neu", "TEST.Lab_Tr",
"TEST.Lab_Ur", "TEST.Lab_WBC")))

ggplot barplot to boxplot

I have code to produce a good barplot and I'm trying to create a boxplot with the same data.
The barplot displays the count of "response" across all people (id). I'd like to create a boxplot for each type of "response" to replace the 3 bars. Boxplots should be calculated from the count of that specific "response" for each participant. So far no luck because I'm stuck on how to count the response for each participant.
current code:
df %>%
ggplot(position = dodge) +
labs(title= "question") +
geom_bar(aes(x = response), fill="red") +
labs(y = "count", x = "responses") +
scale_y_continuous(breaks=seq(0,100,20), limits = c(0,100))
output:
data sample:
structure(list(id = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5),
response = c(0, 1, 1, 0, 0, 0, 1, -1, 1, -1, 0, 1, -1, 1,
0, 0, 0, 0, 1, 1, 1, -1, 0, 1, 0, 1, 1, -1, 0, 1, 1, 1, 0,
1, 0, 0, 1, -1, 0, 1, 1, 1, -1, 1, 1, 1, 0, 0, -1, 1, 1,
-1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0,
1, 1, 0, 0, 0), iscorrect = c(0, 1, 1, 0, 0, 0, 1, 0, 1,
0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0,
0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1,
0, 1, 0, 0, 1, 1, 0, 0, 0), min = c(100, 150, 150,
50, 50, 50, 150, 100, 100, 100, 50, 100, 50, 150, 150, 150,
50, 100, 100, 100, 150, 150, 50, 50, 50, 150, 150, 100, 50,
100, 100, 150, 150, 50, 50, 50, 150, 100, 100, 100, 50, 100,
50, 150, 150, 150, 50, 100, 100, 100, 150, 150, 50, 50, 50,
150, 150, 100, 50, 100, 100, 150, 100, 50, 50, 50, 150, 100,
100, 50, 150, 100, 50, 150, 150), max = c(125.4, 180.8,
180.8, 62.4, 62.4, 62.4, 180.8, 125.4, 125.4, 125.4, 62.4,
125.4, 62.4, 180.8, 180.8, 180.8, 62.4, 125.4, 125.4, 125.4,
180.8, 180.8, 62.4, 62.4, 62.4, 180.8, 180.8, 125.4, 62.4,
125.4, 125.4, 180.8, 180.8, 62.4, 62.4, 62.4, 180.8, 125.4,
125.4, 125.4, 62.4, 125.4, 62.4, 180.8, 180.8, 180.8, 62.4,
125.4, 125.4, 125.4, 180.8, 180.8, 62.4, 62.4, 62.4, 180.8,
180.8, 125.4, 62.4, 125.4, 125.4, 180.8, 125.4, 62.4, 62.4,
62.4, 180.8, 125.4, 125.4, 62.4, 180.8, 125.4, 62.4, 180.8,
180.8), time = c(5, 7, 9, 5, 1, 7, 1, 1, 7, 3, 9, 9,
3, 5, 3, 1, 9, 5, 1, 7, 9, 3, 5, 7, 1, 5, 7, 3, 3, 9, 5,
7, 9, 5, 1, 7, 1, 1, 7, 3, 9, 9, 3, 5, 3, 1, 9, 5, 1, 7,
9, 3, 5, 7, 1, 5, 7, 3, 3, 9, 9, 7, 5, 7, 5, 9, 5, 3, 1,
1, 9, 7, 3, 3, 1)), row.names = c(NA, -75L), class = c("tbl_df",
"tbl", "data.frame"))
You can use this code:
data %>%
group_by(id) %>%
count(response) %>%
mutate(response = as.factor(response)) %>%
ggplot(aes(x = response, y = n)) +
geom_boxplot(fill = "red") +
labs(y = "count", x = "responses")
Output:
You can try:
library(dplyr)
library(ggplot2)
df %>%
group_by(id, response) %>%
count() %>%
mutate(id = factor(id), response = factor(response)) %>%
ggplot(aes(response, n)) +
geom_boxplot(fill = "red") +
scale_y_continuous(name = "Number of responses per participant")
Note that boxplots don't work well for discrete data like small counts (unless your actual data has a far higher number of participants with a far higher count per response)

How to calculate the conditional expectation Weibull model?

I would like to calculate the conditional expectation of the Weibull model. In specific, I would like to estimate the remaining tenure of a client looking at random moments (time = t) in his total tenure.
To do so, I have calculated the total tenure for each client (currently active or inactive) and based on the random moment for each client, calculated his/her tenure at that moment.
The example below is a snapshot of my attempt. I use 2 variables STED and TemporalTenure to predict the dependent variable tenure which has either status 0 = active or 1 = inactive. I use the survival package for obtaining the survival object (km_surv).
df = structure(list(ID = c(16008, 21736, 18851, 20387, 30749,
42159), STED = c(2,
5, 1, 3, 2, 2), TemporalTenure = c(84, 98, 255, 392, 108, 278
), tenure = c(152, 166, 273, 460, 160, 289), status = c(0, 0,
1, 0, 1, 1)), row.names = c(NA,
6L), class = "data.frame")
km_surv <- Surv(time = df$tenure, event = df$status)
df <- data.frame(y = km_surv, df[,!(names(df) %in% c("tenure","status", "ID"))])
weibull_fit <- psm(y ~. , dist="weibull", data = df)
quantsurv <- Quantile(weibull_fit, df)
lp <- predict(weibull_fit, df, type="lp")
print(quantsurv(0.5, lp))
The output of these estimations are way too high. I assume this is caused by including the TemporalTenure, but I can't find out how the psm package calculates this and if there are other packages where it's possible to estimate the remaining tenure of client i at time t.
How can I obtain the predicted tenure conditioned over the time that a client is already active (random moment in time: TemporalTenure) where the dependent tenure can either be a client that is still active or one that is inactive?
EDIT
To clarify, whenever I add time conditional variables such as: TemporalTenure, number of received payments and number of complaints until time t, the predicted lifetime explodes in many cases. Therefore, I suspect that the psm is not the right way to go. Similar question is asked here, but the solution given doesn't work for the same reasons.
Below a slightly bigger dataset which already causes problems.
df = structure(list(ID= c(16008, 21736, 18851, 20387, 30749,
42159, 34108, 47511, 47917, 61116, 66600, 131380, 112668, 90799,
113615, 147562, 166247, 191603, 169698, 1020841, 1004077, 1026953,
1125673, 1129788, 22457, 1147883, 1163870, 1220268, 2004623,
1233924, 2009026, 2026688, 2031284, 2042982, 2046137, 2043214,
2033631, 2034252, 2068467, 2070284, 2070697, 2084859, 2090567,
2087133, 2087685, 2095100, 2095720, 2100482, 2105150, 2109353,
28852, 29040, 29592, 29191, 31172, 2126369, 2114207, 2111947,
2102678, 237687, 1093221, 2111607, 2031732, 2105275, 2020226,
1146777, 1028487, 1030165, 1098033, 1142093, 1186763, 2005605,
2007182, 2021092, 2027676, 2027525, 2070471, 2070621, 2072706,
2081862, 2085084, 2085353, 2094429, 2096216, 2109774, 2114526,
2115510, 2117329, 2122045, 2119764, 2122522, 2123080, 2128547,
2130005, 30025, 24166, 61529, 94568, 70809, 159214), STED = c(2,
5, 1, 3, 2, 2, 3, 1, 2, 2, 2, 2, 2, 1, 2, 2, 4, 1, 4, 3, 2, 4,
1, 1, 2, 1, 4, 1, 1, 1, 2, 4, 2, 5, 4, 1, 4, 2, 5, 3, 2, 1, 4,
2, 1, 5, 3, 1, 1, 5, 2, 2, 2, 2, 3, 4, 3, 5, 1, 1, 5, 2, 5, 1,
3, 5, 3, 1, 1, 1, 2, 2, 2, 2, 1, 2, 1, 3, 5, 2, 2, 1, 2, 1, 2,
3, 1, 1, 3, 5, 1, 2, 2, 2, 2, 1, 2, 1, 3, 1), TemporalTenure = c(84,
98, 255, 392, 108, 278, 120, 67, 209, 95, 224, 198, 204, 216,
204, 190, 36, 160, 184, 95, 140, 256, 142, 216, 56, 79, 194,
172, 155, 158, 78, 24, 140, 87, 134, 111, 15, 126, 41, 116, 66,
60, 0, 118, 22, 116, 110, 52, 66, 0, 325, 323, 53, 191, 60, 7,
45, 73, 42, 161, 30, 17, 30, 12, 87, 85, 251, 120, 7, 6, 38,
119, 156, 54, 11, 141, 50, 25, 33, 3, 48, 58, 13, 113, 25, 18,
23, 2, 102, 5, 90, 0, 101, 83, 44, 125, 226, 213, 216, 186),
tenure = c(152, 166, 273, 460, 160, 289, 188, 72, 233, 163,
266, 266, 216, 232, 247, 258, 65, 228, 252, 99, 208, 324,
201, 284, 124, 84, 262, 180, 223, 226, 146, 92, 208, 155,
202, 179, 80, 185, 64, 184, 120, 65, 6, 186, 45, 120, 170,
96, 123, 12, 393, 391, 64, 259, 73, 42, 69, 141, 47, 229,
37, 19, 37, 17, 155, 99, 319, 188, 75, 11, 49, 187, 180,
55, 52, 209, 115, 93, 88, 6, 53, 126, 31, 123, 26, 26, 24,
9, 114, 6, 111, 4, 168, 84, 112, 193, 294, 278, 284, 210),
status = c(0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1,
0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0,
1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 0, 0, 1, 0, 1), TotalValue = c(2579.35, 2472.85,
581.19, 2579.35, 2472.85, 0, 1829.18, 0, 936.79, 2098.2,
850.47, 2579.35, 463.68, 463.68, 2171.31, 3043.03, 561.16,
3043.03, 3043.03, -68.06, 2098.2, 2504.4, 1536.67, 2719.7,
3043.03, 109.91, 2579.35, 265.57, 3560.34, 2266.95, 3123.16,
3544.4, 1379.19, 2288.35, 2472.85, 2560.48, 1414.45, 3741.49,
202.2, 2856.23, 1457.75, 313.68, 191.32, 2266.95, 661.01,
0, 2050.81, 298.76, 1605.44, 373.86, 3043.03, 2579.35, 448.63,
3043.03, 463.68, 977.28, 818.06, 2620.06, 0, 3235.8, 280.99,
0, 0, 194.04, 3212.75, -23.22, 1833.46, 1829.18, 2786.7,
0, 0, 3250.38, 936.79, 0, 1045.21, 3043.03, 1988.36, 2472.85,
1197.94, 0, 313.68, 3212.75, 1419.33, 531.14, 0, 96.28, 0,
142.92, 174.79, 0, 936.79, 156.19, 2472.85, 463.68, 3520.69,
2579.35, 3328.87, 2567.88, 3043.03, 1081.14)), row.names = c(NA,
100L), class = "data.frame")
So here's what I have done: 1) added library call to load pkg:rms, removed the attempt to place a Surv object in a dataframe column, 3) built the Surv object inside formula as Therneau expects formulas to be built, and removed ID from the covariates where it most probably does not belong.
library(survival); library(rms)
#km_surv <- Surv(time = df$tenure, event = df$status)
#df <- data.frame(y = km_surv, df[,!(names(df) %in% c("tenure","status"))])
weibull_fit <- psm(Surv(time = tenure, event = status) ~TemporalTenure +STED , dist="weibull", data = df)
quantsurv <- Quantile(weibull_fit, df)
lp <- predict(weibull_fit, df, type="lp")
Results#
print(quantsurv(0.5, lp))
1 2 3 4 5 6
151.4129 176.0490 268.4644 466.8266 164.8640 301.2630

error with extracting data from a list table

I am trying to extract the median values from the following data
df<-structure(list(n = 26L, time = c(64, 77, 142, 148, 167, 175,
181, 218, 286, 294, 323, 362, 375, 414, 427, 442, 455, 460, 505,
543, 544, 548, 598, 604, 771, 951), n.risk = c(26, 25, 24, 23,
22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7,
6, 5, 4, 3, 2, 1), n.event = c(1, 0, 1, 1, 0, 1, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0), n.censor = c(0,
1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0,
0, 1, 0, 1), surv = c(0.961538461538462, 0.961538461538462, 0.921474358974359,
0.881410256410256, 0.881410256410256, 0.839438339438339, 0.839438339438339,
0.839438339438339, 0.839438339438339, 0.839438339438339, 0.786973443223443,
0.734508547008547, 0.682043650793651, 0.629578754578755, 0.577113858363858,
0.524648962148962, 0.524648962148962, 0.4663546330213, 0.408060303893637,
0.349765974765975, 0.349765974765975, 0.27981277981278, 0.209859584859585,
0.209859584859585, 0.104929792429792, 0.104929792429792), type = "right",
std.err = c(0.0392232270276368, 0.0392232270276368, 0.0578796660439579,
0.0729817807835649, 0.0729817807835649, 0.0877911880959172,
0.0877911880959172, 0.0877911880959172, 0.0877911880959172,
0.0877911880959172, 0.108967698764172, 0.128980092013706,
0.148762796526449, 0.168939711260041, 0.190043109889266,
0.212620066567793, 0.212620066567793, 0.24309706208875, 0.277404622263805,
0.317431643449181, 0.317431643449181, 0.388281918537096,
0.483834870173886, 0.483834870173886, 0.856794130229766,
0.856794130229766), upper = c(1, 1, 1, 1, 1, 0.997049673308717,
0.997049673308717, 0.997049673308717, 0.997049673308717,
0.997049673308717, 0.974346771572688, 0.945768634864856,
0.912933812389795, 0.876701615980298, 0.837580372384821,
0.795886882462859, 0.795886882462859, 0.751001648029994,
0.70283210436471, 0.651592180391947, 0.651592180391947, 0.598926755204663,
0.541713673163476, 0.541713673163476, 0.56260462703826, 0.56260462703826
), lower = c(0.890389006776242, 0.890389006776242, 0.822651689473135,
0.763934098528765, 0.763934098528765, 0.706741845048289,
0.706741845048289, 0.706741845048289, 0.706741845048289,
0.706741845048289, 0.635633245173389, 0.570438462156972,
0.509547937949868, 0.45211438075625, 0.397645905392106, 0.345848812876783,
0.345848812876783, 0.289595428067216, 0.236917480831754,
0.187749701094333, 0.187749701094333, 0.130725820922461,
0.0812994900059442, 0.0812994900059442, 0.019570157816371,
0.019570157816371), conf.type = "log", conf.int = 0.95, call = survfit(formula = Surv(as.numeric(as.character(all_clin$new_death))[ind_clin],
all_clin$death_event[ind_clin]) ~ event_rna[ind_gene,
ind_tum])), .Names = c("n", "time", "n.risk", "n.event",
"n.censor", "surv", "type", "std.err", "upper", "lower", "conf.type",
"conf.int", "call"), class = "survfit")
I try to get it like below
x1 <- ifelse (is.na(as.numeric(summary(s)$table[,'median'][1])),'NA',as.numeric(summary(s)$table[,'median'][1]))
x2 <- as.numeric(summary(s)$table[,'median'][2])
if(x1 != 'NA' & x2 != 'NA'){
lines(c(0,x1),c(0.5,0.5),col='blue')
lines(c(x1,x1),c(0,0.5),col='black')
lines(c(x2,x2),c(0,0.5),col='red')
}
I get the following error for both comments
Error in summary(s)$table[, "median"] : incorrect number of dimensions

How to set the ranges of the values taken by ggplot2 stat_smooth() to fits lines?

I would like to make stat_smooth to fit a line using only the data from specified ranges, in other words, I want stat_smooth to fit a line between x(0,99), x(100,199) and x(199,299).
I managed to get fairly close using facet_grid as follows:
ggplot(avtimes3, aes(x=trial, y=ttime, colour=factor(id))) +
facet_grid(. ~ brk, scales = "free") +
geom_vline(xintercept = short_bks, colour="red") +
geom_vline(xintercept = long_bks, colour="green") +
geom_point() +
geom_point(shape=21, fill="white")+
opts(title=titl) +
ylab("Time taken (ms)") +
xlab("Trial No.")+
stat_smooth(method="loess")
But it plots lots of empty space on each grid. I haven't been able to find a way of removing it.
The other partial solution I took from another post included creating a group for the ranges, but that doesn't seem to respect the factors passed to qqplot.
ggplot(avtimes3, aes(x=trial, y=ttime, colour=factor(id))) +
geom_vline(xintercept = short_bks, colour="red") +
geom_vline(xintercept = long_bks, colour="green") +
geom_point() +
geom_point(shape=21, fill="white")+
opts(title=titl) +
ylab("Time taken (ms)") +
xlab("Trial No.")+
stat_smooth(method="loess", aes(group=brk))
Any help on how to get this to work?
Thanks!
The data:
> dput(avtimes3)
structure(list(trial = c(0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14,
15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
23, 23, 24, 24, 25, 25, 26, 26, 27, 27, 28, 28, 29, 29, 30, 30,
31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 37, 37, 38, 38,
39, 39, 40, 40, 41, 41, 42, 42, 43, 43, 44, 44, 45, 45, 46, 46,
47, 47, 48, 48, 49, 49, 50, 50, 51, 51, 52, 52, 53, 53, 54, 54,
55, 55, 56, 56, 57, 57, 58, 58, 59, 59, 60, 60, 61, 61, 62, 62,
63, 63, 64, 64, 65, 65, 66, 66, 67, 67, 68, 68, 69, 69, 70, 70,
71, 71, 72, 72, 73, 73, 74, 74, 75, 75, 76, 76, 77, 77, 78, 78,
79, 79, 80, 80, 81, 81, 82, 82, 83, 83, 84, 84, 85, 85, 86, 86,
87, 87, 88, 88, 89, 89, 90, 90, 91, 91, 92, 92, 93, 93, 94, 94,
95, 95, 96, 96, 97, 97, 98, 98, 99, 99, 100, 100, 101, 101, 102,
102, 103, 103, 104, 104, 105, 105, 106, 106, 107, 107, 108, 108,
109, 109, 110, 110, 111, 111, 112, 112, 113, 113, 114, 114, 115,
115, 116, 116, 117, 117, 118, 118, 119, 119, 120, 120, 121, 121,
122, 122, 123, 123, 124, 124, 125, 125, 126, 126, 127, 127, 128,
128, 129, 129, 130, 130, 131, 131, 132, 132, 133, 133, 134, 134,
135, 135, 136, 136, 137, 137, 138, 138, 139, 139, 140, 140, 141,
141, 142, 142, 143, 143, 144, 144, 145, 145, 146, 146, 147, 147,
148, 148, 149, 149, 150, 150, 151, 151, 152, 152, 153, 153, 154,
154, 155, 155, 156, 156, 157, 157, 158, 158, 159, 159, 160, 160,
161, 161, 162, 162, 163, 163, 164, 164, 165, 165, 166, 166, 167,
167, 168, 168, 169, 169, 170, 170, 171, 171, 172, 172, 173, 173,
174, 174, 175, 175, 176, 176, 177, 177, 178, 178, 179, 179, 180,
180, 181, 181, 182, 182, 183, 183, 184, 184, 185, 185, 186, 186,
187, 187, 188, 188, 189, 189, 190, 190, 191, 191, 192, 192, 193,
193, 194, 194, 195, 195, 196, 196, 197, 197, 198, 198, 199, 199,
200, 200, 201, 201, 202, 202, 203, 203, 204, 204, 205, 205, 206,
206, 207, 207, 208, 208, 209, 209, 210, 210, 211, 211, 212, 212,
213, 213, 214, 214, 215, 215, 216, 216, 217, 217, 218, 218, 219,
219, 220, 220, 221, 221, 222, 222, 223, 223, 224, 224, 225, 225,
226, 226, 227, 227, 228, 228, 229, 229, 230, 230, 231, 231, 232,
232, 233, 233, 234, 234, 235, 235, 236, 236, 237, 237, 238, 238,
239, 239, 240, 240, 241, 241, 242, 242, 243, 243, 244, 244, 245,
245, 246, 246, 247, 247, 248, 248, 249, 249, 250, 250, 251, 251,
252, 252, 253, 253, 254, 254, 255, 255, 256, 256, 257, 257, 258,
258, 259, 259, 260, 260, 261, 261, 262, 262, 263, 263, 264, 264,
265, 265, 266, 266, 267, 267, 268, 268, 269, 269, 270, 270, 271,
271, 272, 272, 273, 273, 274, 274, 275, 275, 276, 276, 277, 277,
278, 278, 279, 279, 280, 280, 281, 281, 282, 282, 283, 283, 284,
284, 285, 285, 286, 286, 287, 287, 288, 288, 289, 289, 290, 290,
291, 291, 292, 292, 293, 293, 294, 294, 295, 295, 296, 296, 297,
297, 298, 298, 299, 299), id = c(1.5, 2, 1.5, 2, 1.5, 2, 1.5,
2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5,
2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5,
2, 1.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5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5,
2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5,
2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5, 2, 1.5,
2), ttime = c(2564, 2050.5, 2204.75, 2060.625, 2266.125, 1880.125,
2224.25, 2067, 2215.375, 2199.25, 2333.375, 2434.625, 2278.875,
2102, 2104.5, 2049, 2336.125, 2159.5, 2110.625, 2082.875, 2197.5,
2136.5, 2107.375, 2025, 2342, 2289.875, 2106.25, 2146.75, 2187.25,
2104.875, 2151.375, 2246.375, 2143.75, 2091.625, 2129.625, 2163.875,
2132.5, 2265.25, 1959, 2054.25, 2205.5, 2033, 1992.875, 1965.625,
1983.875, 1973.75, 1953.625, 1936.375, 2012.25, 2106.75, 2115,
2195.375, 2210.875, 2097.625, 2016.25, 2084.375, 2005.25, 2250.875,
2352.875, 2217.625, 2141.75, 2154.375, 2181.625, 2083.5, 2103.5,
1959.5, 2020.75, 2255.25, 2039.125, 2144.125, 2216.125, 2216.125,
2014.125, 2234.625, 1975.625, 2289, 2035.5, 2058.25, 2039.5,
2222.5, 2013.125, 2156.375, 1896.375, 1990.875, 2014.125, 2030.625,
1928.875, 2021, 2013, 2001.5, 1940.625, 2029.375, 2075.5, 2179.375,
1991, 2292.25, 2020.375, 2065.375, 2007.375, 2192.5, 1954.25,
2128.5, 1949.875, 2240.25, 2119.375, 2011.625, 1984.875, 2182,
1955.375, 2010.5, 1924.5, 2043.25, 2033, 2183.375, 2075.75, 2011.375,
1969.625, 2002.375, 1921.5, 2111.875, 1951, 2103, 1780.375, 1935.75,
1930.75, 2044.375, 1892.25, 2128.75, 1973.875, 1992.625, 1920.625,
2093, 2047.875, 2285.625, 1968, 2208.375, 2021.25, 2079.375,
1988.75, 2147.875, 1989, 2026.25, 1873.25, 2084.75, 2010.25,
2163.25, 1960.125, 1987.125, 2043.75, 2154.875, 2188.25, 2402.625,
2101.625, 2019.625, 2447.5, 1996.125, 2009.875, 2037.625, 2195.5,
2123.875, 1986.625, 2093, 1793.125, 1858.125, 1862.625, 1895.5,
1806, 1951.5, 1784.625, 2041.625, 1868.625, 2095.25, 1946.125,
1930.75, 2010.5, 2179.375, 1903.875, 2049.25, 1958.125, 2053.75,
2016.75, 2057.875, 1898.875, 2196.875, 1939.75, 2265.875, 2090.875,
2187.875, 2082.75, 2089.125, 1904, 2303.25, 1982.125, 2094.375,
2205.625, 2259.25, 2018.5, 1882.25, 1978.125, 1901.625, 1834.5,
2019.375, 1802.125, 1929.875, 1700.75, 2019, 1691.125, 1959.375,
1774.375, 2138.375, 1751.25, 2036.125, 1808.5, 2138, 1848, 2109.75,
1823.125, 2001, 1923, 2057.125, 1827.5, 1877.375, 1823.125, 1860.25,
1742.875, 2047.625, 2018.125, 2016.125, 1799, 2095.25, 1735.25,
1951, 1776.625, 2057.125, 1726.125, 2068.5, 1806, 2107.875, 1774.375,
1956, 1920.75, 1943.25, 1714.125, 1756.125, 1789.25, 1941.125,
1734.125, 2195.375, 1788.375, 2005.375, 1832.5, 1861, 1830.25,
2263.625, 1903.75, 2090.375, 2015.875, 2019.25, 1807.375, 2143.375,
1882.5, 1901.75, 1897.125, 1866.75, 1943.125, 1943.875, 1818.75,
2089.5, 1818, 1992.875, 1910.25, 1855.125, 1825.5, 1850.25, 1881.125,
1953.625, 1840.25, 2119.25, 1745.25, 2045.5, 1767.625, 2019.875,
1759.5, 1876.125, 1667.875, 1948, 1767.125, 2046.25, 1721, 1984.5,
1842.875, 2300.125, 1726.625, 2252.875, 1718.75, 1983.625, 1842.375,
2165.125, 1932, 2154.375, 1746.125, 1936.75, 1920.625, 2056.25,
1854.375, 2022.125, 1973.625, 2064.25, 1883.5, 2077.375, 2117.25,
2089, 1759, 2082.875, 1910.75, 1955.75, 1847.375, 1998.25, 1796.375,
2028.5, 1804.5, 1920.625, 1793.25, 1841.125, 1778.625, 1929.25,
1742.25, 1988.875, 1805.375, 1982.75, 1797.125, 1963.75, 1682.875,
2028.25, 1664.375, 2109.5, 1747.625, 1992, 1767.125, 2064.125,
1740.125, 2083.875, 1952.375, 2007.125, 1763.125, 2050, 1780.25,
2181.25, 1810.5, 1990.25, 1706.875, 2029, 1715.125, 2211.375,
1876.375, 2003.625, 1773.5, 1986.75, 1824, 2198.375, 1777.375,
2075, 1761.25, 1873, 1725.125, 1917.75, 1773.875, 1860.25, 1788,
1908.625, 1955.375, 1943.875, 1828.5, 1860.5, 1836.25, 2134.5,
1829.75, 1949.375, 1762, 2058.75, 1736.75, 1908.625, 1816.375,
2113.625, 1868.125, 2042.625, 1796.375, 2280.5, 1773, 1968.125,
1885.75, 2136.375, 1784.5, 2103.875, 1785.875, 2296.875, 1782.625,
1969.125, 1864.75, 2062.5, 1800.75, 2289.75, 1639.5, 2081.75,
1735.125, 1947.375, 1617.875, 1871.125, 1676.375, 1998, 1685,
1929.625, 1762.625, 2073.625, 1698.75, 1834.5, 1669, 1858.875,
1705.625, 2164.625, 1750.125, 1835.875, 1836.25, 1992, 1741.625,
1874.375, 1810.375, 2140.875, 1819.5, 2277.625, 1779.5, 2054.875,
1703.375, 1978.125, 1754.625, 1964.875, 1749.875, 1959.625, 1932.875,
1952.125, 1709.625, 2038.125, 1680.5, 1893, 1664.5, 1961.125,
1700.25, 1871, 1753, 1889.75, 1724, 1878.875, 1827.375, 1849.25,
1850.75, 1977.125, 1783.625, 1977.25, 1895.125, 2097.5, 1716.375,
1985.5, 1675.375, 1845.25, 1764.5, 2089.375, 1766.75, 1848.125,
1730.125, 1991.5, 1865.375, 2030.875, 1864.625, 1960.875, 1739.25,
2204.375, 1758.125, 2175.375, 1871.5, 1994.25, 1739.25, 2028.875,
1646.125, 1958.375, 1709.25, 1914.5, 1638.375, 1965.375, 1691.5,
2154.375, 1833.75, 1892.375, 1891.625, 1999.25, 1752.75, 1952.5,
1702.625, 1907.75, 1671.25, 1947.375, 1734.875, 2125.375, 1745.25,
2145.25, 1756.125, 2095.25, 1707.5, 1939.375, 1738.25, 2102.875,
1862.375, 2152.875, 1719, 2091.25, 1969.25, 2088.125, 1702.875,
2231.625, 2007.5, 2087, 1732.625, 1866.625, 1675.875, 1971.625,
1663.875, 2016.5, 1834, 1927.75, 1777.25, 1995, 2029.125, 2009.5,
1778.5, 1868, 1711.5, 1820.375, 1706.625, 1911.375, 1850, 2001.625,
1753.375, 2105.625, 1812.125, 2028.125, 1820.5, 2205.75, 1812.625,
2238, 1812.125, 2236.25, 1713.375, 2482, 1844.25, 1951.25, 1995.125,
2108.5, 1873.125, 1977, 1849, 2064.25, 1709.75, 1962, 1844, 1828.5,
1735.75, 1974.125, 1675, 2147, 1789.75, 2099.875, 1790.5, 2083,
1713.5, 2250.125, 1930.25, 2321.75, 1742.5, 2189.875, 1840, 2070,
1823.125, 2027.25, 1782.875, 2344, 1788.875, 2106.25, 1824.75,
2251.625, 1902.5, 1961.375, 1873.5, 2158.75, 1855.125, 1933.75,
1825.75, 2001.875, 1887.75, 2002.75, 1945.375, 2230.875), se = c(259.899705930686,
137.567905310172, 228.087917729734, 172.114158284968, 234.074502600897,
109.939827292024, 271.704918063696, 195.301781134443, 227.085803284951,
169.989784356926, 257.363066052332, 353.735067201228, 231.204411529526,
151.548082893092, 230.22527787241, 188.563933833745, 289.179037319443,
130.584537478874, 250.616449800715, 90.5208021672367, 192.562883977454,
178.968373103821, 225.736567192507, 157.087555204096, 222.259179466547,
189.375330032716, 195.44269803266, 156.734278090568, 222.104443738912,
155.254449844211, 229.124571350234, 234.785535687055, 214.213407443538,
136.267454648675, 218.906693878922, 219.006069509826, 181.324098154185,
198.187568984535, 160.006249877934, 152.706271505603, 280.670689090054,
147.26276321111, 197.418648480474, 131.188242273133, 184.134989060821,
117.833531184585, 177.697770857873, 95.0770246957697, 179.782482660098,
149.204886697071, 143.619164061466, 171.956798717003, 225.56939362264,
150.268680500248, 233.734415064864, 134.100833264163, 177.509833731623,
179.588959004802, 302.633231433458, 256.399655776179, 261.114900132927,
194.266379848099, 233.925960318034, 180.624314136117, 204.312523215923,
147.377552080169, 190.48347791119, 143.664757334567, 184.82012853235,
203.166050796949, 199.973965939498, 181.057847652148, 226.143478278409,
219.701090567226, 192.715458040753, 161.770339856053, 161.292214850651,
121.259130731316, 209.589053694537, 141.678080772675, 203.257616049542,
158.180608516702, 146.132393189483, 151.949173539331, 171.350459324492,
194.983967976491, 127.888265615509, 167.346091336829, 133.506152881217,
141.716391833428, 110.950912167112, 128.343141825231, 135.5180536207,
175.427125437561, 133.723194910767, 194.496120967562, 173.14114158892,
142.659927487414, 199.71273454461, 219.278280469622, 135.405970273523,
223.876480612477, 155.919786783828, 219.316531687487, 142.123178151409,
132.259071725048, 131.867823095163, 192.080526268096, 97.3238220544472,
154.800909004345, 143.230957348114, 172.257049409969, 113.881987789365,
301.963629402852, 147.572228078321, 145.94690050788, 116.268306930258,
103.901556687775, 123.388903182464, 217.304134920557, 106.773123959169,
137.984730004032, 172.231876249516, 166.958479654588, 130.019607037884,
182.871142914114, 142.971994010815, 203.262387603525, 68.1171358501557,
215.99462108878, 142.521168540476, 236.726032125143, 124.067728839073,
132.166282913933, 131.916369611313, 236.530271205249, 124.9966785273,
159.275585240084, 89.4242995898925, 208.220816639794, 86.0348766489497,
106.74431232757, 117.217708011326, 186.83259791283, 92.4279159283446,
153.374745313562, 121.694646774505, 171.894115820093, 107.795731097559,
127.720179284358, 272.201064734981, 364.814039590231, 155.784528103495,
143.156624703255, 489.334059134015, 173.340544006382, 83.3367603462002,
166.637431215627, 219.917679403648, 209.582530411224, 228.411747375843,
189.848059548381, 139.321242034884, 107.117331780889, 112.04876768303,
153.452994198782, 110.70181957467, 212.323287868826, 88.1538706507791,
223.663885064493, 109.954525908928, 174.492810944831, 91.7725132947146,
157.710715054042, 91.0863013065866, 274.394589277923, 98.7716409379303,
171.341946119448, 134.708208136274, 186.973140737227, 120.51344767879,
161.766303826141, 86.3732426945488, 199.796095834514, 104.603801829297,
242.194169263011, 161.9110687891, 212.630332930518, 146.398032529715,
205.110722494322, 115.358911972281, 240.223909687846, 120.004380500404,
183.283731791138, 236.191503528992, 204.437359424767, 129.357340064534,
146.524589598957, 121.586893898385, 142.473924619309, 127.924224222221,
188.175823699539, 152.876518506651, 145.462002586341, 87.0048746581806,
133.546273414338, 98.8791537866877, 174.15909572859, 106.353174695714,
186.087147163058, 126.108053158506, 209.25995773371, 98.74281167327,
160.608886073325, 96.4942633379977, 180.464554295692, 109.188473603477,
150.810548134311, 167.763332448678, 179.1967631145, 107.239551605606,
88.4372128970605, 102.662838856827, 97.9408987239608, 106.765670897264,
250.209122357108, 133.851255063757, 171.96443127868, 79.5145988392787,
134.235367651642, 91.3258979854956, 180.483774498271, 100.323216494773,
155.563886451378, 109.931543227722, 157.58716408932, 101.749692874229,
194.113501177238, 76.3331367615477, 161.404881321123, 150.197697100474,
146.905551533727, 100.562780231342, 141.682925362535, 145.424839448326,
138.486517845601, 108.039251713043, 131.377990315295, 112.028525943172,
96.5617747314418, 125.319448952325, 154.683432486205, 95.5564063038909,
200.194721057489, 93.4435945217373, 122.469812242504, 203.591190300212,
211.888510947216, 101.610414521769, 196.474547379189, 103.015775490941,
119.9206210672, 145.295442547246, 128.463967988804, 150.725085301969,
165.399455593766, 104.018499384896, 215.797110400355, 121.178587217379,
119.188841385065, 143.195265035445, 174.310318264787, 134.187901306872,
127.609721696384, 138.859697626777, 122.751482024804, 115.77236747786,
181.758252907537, 103.840494096887, 162.714583594359, 124.458819566382,
205.661861654721, 129.944493644677, 188.241243156222, 100.24442227091,
158.948217084325, 111.493984062562, 148.115875246376, 115.076216979382,
139.682931772538, 136.821630169147, 191.964509136083, 79.2329155762571,
165.804471495365, 81.7984784175633, 230.305537547407, 124.336946798953,
170.654667750402, 154.3081332918, 162.600182030736, 81.7580134779285,
245.344491771992, 129.797636108235, 219.264496995882, 75.4214202000996,
235.570692870072, 162.076253823845, 207.486811112693, 67.3856173717465,
167.51779622945, 132.19219638736, 189.748047232564, 104.87049837095,
158.695737351611, 122.825187912391, 148.869519810374, 78.1129990782584,
197.096672255738, 78.5845626197847, 133.345550035559, 103.367582649771,
175.46753235489, 104.227215735623, 124.734781135816, 156.960975302325,
160.870483619588, 113.155984312427, 166.304737229581, 79.420431317856,
206.602599036342, 114.308392002262, 208.96263763786, 94.5465564644515,
168.996380984074, 108.669048277656, 137.826236357855, 109.913104882903,
151.036892371178, 120.993718476268, 173.60406898416, 89.1802823939078,
119.126199214471, 112.281523969886, 173.941845526026, 109.368550830274,
183.11822878754, 122.79741592209, 194.953634597709, 113.22401941032,
205.555584244957, 113.743200346218, 159.083199256597, 92.3830220757967,
192.970014093604, 106.811405473787, 128.241393125287, 95.926683312979,
236.008152855544, 137.267595801569, 215.379145553337, 126.974538716007,
158.024523413298, 116.186080737509, 152.477633020153, 99.9564972339753,
192.186456152798, 119.916702860777, 201.098123703686, 97.9207040707648,
159.467074243736, 175.587985423263, 175.71460601742, 74.9249624624531,
183.511385400003, 104.460476121012, 195.090363677963, 99.0178104469811,
190.719121962489, 105.451241271567, 249.549934167207, 101.510511419121,
163.787529777802, 122.226799945838, 260.121464587769, 81.4819080488774,
207.000080874725, 100.300074330552, 318.607292984415, 92.1772361114329,
164.345502513542, 89.5695559726789, 220.035868626328, 121.088220495873,
215.86027536845, 110.382361022946, 266.694209422113, 97.2656409566826,
199.965929017563, 114.208416940259, 155.021887855508, 84.6120875356639,
199.733013760155, 111.110948412579, 199.452800539018, 123.88609940183,
133.35290685942, 111.102500534416, 187.941616836947, 98.1187213641864,
234.046011959798, 123.069956877031, 120.168919427731, 127.988132471614,
232.385450852365, 98.7424047712025, 146.951765653127, 105.750717660517,
145.651300506675, 79.970069735942, 258.883932728549, 87.6153194163473,
115.796841416953, 106.095938726634, 166.706649965912, 101.298698606081,
146.769977649674, 108.152494439895, 195.816006705639, 174.924881837074,
249.006880104317, 86.1696831009276, 159.538333671611, 80.2393601090779,
174.212353742289, 63.3733093603744, 186.199723559331, 107.657764218047,
160.516124857554, 126.626480741589, 168.920145705005, 109.61880989202,
105.320503617021, 129.726085051763, 115.527671391502, 106.433077565201,
190.60022087193, 77.0231296429845, 112.304592197165, 119.858845552353,
206.728492576271, 107.217902289816, 139.122047844433, 119.25886272846,
122.684227359743, 103.792504202237, 210.152362882267, 120.954970591184,
104.686393917398, 214.792876769027, 191.32610530565, 79.7480267333126,
222.855494499398, 91.1213746094421, 140.845628513733, 111.331069979845,
177.763178886566, 99.5254364471716, 140.033980251223, 91.6609508499355,
130.849559200087, 135.593791669404, 217.92957885355, 135.812625320434,
250.986371108359, 85.4232633921898, 196.743119093691, 78.9173250714225,
225.198796106589, 134.913331968554, 156.164833932428, 107.713865336429,
166.808641166629, 97.9876882754738, 152.573043104419, 95.9305459322376,
171.318833924186, 111.86454460322, 182.048554894802, 97.4309279145268,
152.878913044185, 133.997168147049, 205.307168608753, 211.624746856654,
175.627827887756, 100.98775562272, 214.86931244031, 81.6376877034831,
119.379609110721, 92.9686487094286, 209.254282896944, 93.4634071823987,
161.94630255339, 119.06507074465, 223.306623758199, 95.2261288985329,
196.493161458612, 131.601943103327, 154.470748213283, 96.1410329374805,
199.033866240683, 123.609976698023, 240.952161634687, 120.094010793925,
249.304657999003, 98.3497312800454, 210.566280496529, 93.4448724993971,
208.160904238936, 144.170162357839, 238.586686851671, 117.022424209453,
141.61642579013, 106.825030089261, 227.534764808432, 76.6747391583434,
202.614782284018, 145.926106545157, 164.683001768332, 104.052141805374,
186.763888372458, 290.37355482083, 247.792756149166, 121.924889056454,
139.544795675081, 90.5098652870819, 128.81228555815, 97.8303811816291,
158.302258072064, 92.8779921648365, 203.965152617164, 89.5191395137694,
128.750026005545, 119.657761598295, 179.974247909845, 133.466823057803,
238.607474287195, 116.294720660791, 201.864700091069, 112.108435629464,
271.062377507466, 83.4109742479969, 239.645348081583, 130.262886667144,
152.792196650035, 176.884388948181, 162.02645286672, 98.7427099479103,
137.316891063804, 122.648306726416, 125.046669859126, 100.13272442114,
180.137447522718, 217.650850807303, 149.685622937828, 100.501021672135,
139.485974397326, 97.2504131463864, 168.918196600772, 149.435096996274,
196.609242398287, 118.348909102341, 97.2902505760102, 79.5321139091445,
234.000338636812, 162.735625873905, 258.4290279748, 82.6042631207027,
234.984151251283, 134.304026107081, 246.673584201586, 132.418632361268,
137.06069432398, 103.267931251672, 181.858657832316, 108.533312446982,
177.572294130749, 130.488060482833, 183.539693789732, 143.39418697124,
155.657584081672, 144.60809105994, 205.197357091028, 124.319639509842,
153.618817439234, 120.377345697365, 157.628795128211, 158.939875199937,
149.213503553992, 103.583978611559, 143.635257000402), brk = 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, 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, 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, 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, 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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2)), .Names = c("trial", "id",
"ttime", "se", "brk"), row.names = c(NA, -600L), class = "data.frame")
If you are talking about the space between facets, you can try this (also note that we do not have short_brks and long_brks so I removed those from your example code). grid is required for the unit function.
require(ggplot2)
require(grid)
avtimes3$id <- factor(avtimes3$id)
## create and store plot
p <- ggplot(avtimes3, aes(x=trial, y=ttime, colour=id)) +
facet_grid(. ~ brk, scales = "free") +
geom_point(shape=21, fill="white") +
## remove space between panels
opts(panel.margin = unit(0, "lines")) +
ylab("Time taken (ms)") +
xlab("Trial No.")
## as you had
p + stat_smooth(method="loess")
## only one line per facet
p + stat_smooth(method="loess", aes(colour=NULL))
Edit: Rereading what you wrote and tried, I think you may want this:
## setup plot
p <- ggplot(avtimes3, aes(x=trial, y=ttime, colour=id)) +
geom_point(shape=21, fill="white") +
ylab("Time taken (ms)") +
xlab("Trial No.")
p + stat_smooth(aes(group = brk), method="loess")
The here that differs from what you had before is that the first argument to stat_smooth is mapping, the fifth is method. In R you have two options (maybe more). Either give the arguments in order or explicitly name the arguments. In your second example, you gave the arguments out of order and did not name explicitly like:
p + stat_smooth(method = "loess", mapping = aes(group = brk))
To get separate lines for each id and grouping by brk is a bit trickier (conceptually). We need the interaction of brk and id. Colouring alone does not work, as it would with most geoms.
p + stat_smooth(method = "loess", mapping = aes(group = interaction(brk, id)))

Resources