GGPLOT boxplot: independently specify facet scales - r

I have plenty of measurements (N ~ 500.000) of two laboratory tests that I wanted to plot using geom_boxplot. These measurements were created using three different analyzers.
However, many of these measurements are extreme outliers which leads to extremely wide scales, which again results in boxes that are too small to really read or compare.
I created a dummy data set sampling only 250 of these measurements to showcase the issue. The data as follows:
dummy <- structure(list(result = c(3.93, 2.708, 2.08, 1.8422, 0.897, 1.68,
3.56, 2.8954, 0.972, 2.99, 0.567, 2.01, 2.5629, 0.7958, 4.81,
3.539, 2.24, 4.79, 2.07, 5.56, 3.4761, 1.74, 1.5691, 0.964, 1.8171,
0.005, 0.663, 2.16, 2.37, 0.0164, 1.25, 3.086, 0.769, 1.9573,
1.05, 2.17, 1.6331, 8.2358, 0.58, 1.43, 0.9328, 1.94, 2.59, 5.06,
0.0574, 1.61, 1.01, 8.21, 2.77, 0.9938, 20.38, 3.71, 1.6731,
0.0259, 0.9701, 0.0114, 0.499, 38.8, 4.8689, 3.02, 1.176, 2.86,
1.96, 5.03, 0.7564, 0.903, 2.0017, 0.4928, 1.3993, 4.02, 1.97,
10.6109, 1.18, 1.68, 230, 1.9764, 3.81, 3.3518, 0.985, 3.4, 16.1,
1.5889, 3.13, 2.0168, 1.82, 4.75, 2.61, 2.0133, 1.1971, 0.4736,
74.1, 0.737, 5.21, 1.6495, 1.4, 3.7408, 0.68, 2.26, 2.5, 2.16,
0.459, 0.0281, 5.34, 1.3, 4.11, 1.9344, 3.9611, 2.79, 1.72, 8.9041,
1.47, 2.61, 3.02, 1.91, 3.49, 1.0161, 1.9, 1.63, 1.31, 1.81,
2.556, 0.972, 4.9, 8.313, 1.55, 0.875, 1.4379, 3.68, 0.716, 2.76,
2.1897, 0.3121, 1.4376, 2.56, 0.89, 3.0298, 0.6003, 1.2542, 1.61,
0.491, 3.08, 1.45, 1.94, 2.1503, 2.6605, 23.5, 1.54, 3.54, 4.22,
2.31, 1.082, 1.45, 1.77, 0.423, 11.9, 2.77, 4.8894, 0.8142, 0.158,
9.2012, 1.96, 0.467, 0.4081, 1.06, 2, 3.05, 2.81, 0.2151, 2.21,
0.95, 2.3647, 0.357, 1.7284, 1.31, 0.9586, 1.4548, 4.51, 0.022,
2.2629, 39.9014, 1.3403, 6.64, 4.62, 1.27, 1.18, 1.1, 0.565,
0.939, 1.9141, 3.855, 0.455, 3.14, 1.09, 1.0475, 0.971, 2.33,
1.16, 4.6919, 100, 0.2412, 2.53, 1.84, 1.04, 2.35, 4.89, 1.6384,
3.33, 1.82, 0.8973, 0.6061, 0.98, 6.18, 0.4258, 2.5555, 1.67,
5.37, 2.29, 2.93, 5.0596, 2.2328, 2.84, 7.73, 1.8, 2.3978, 3.02,
2.71, 0.618, 0.0035, 3.97, 0.9827, 2.3385, 5.07, 0.306, 3.13,
2.62, 2.81, 4.4749, 1.0362, 1.4896, 2.3907, 1.45, 2.2823, 2.3726,
2.1746, 4.08, 2.98, 2.57, 0.947, 2.16, 1.46), testName = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L,
1L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 1L,
1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 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, 1L, 3L, 3L, 1L, 2L, 1L,
3L, 1L, 1L, 1L, 3L, 2L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 2L, 1L, 1L,
3L, 2L, 2L, 3L, 3L, 1L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 3L, 1L, 2L,
3L, 1L, 3L, 3L, 2L, 3L, 1L, 3L, 3L, 1L, 1L, 1L, 3L, 1L, 1L, 3L,
2L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 3L, 3L, 3L,
1L, 3L, 2L, 3L, 1L, 1L, 1L, 3L, 3L, 3L, 2L, 3L, 1L, 3L, 2L, 1L,
1L, 3L, 3L, 1L, 1L, 2L, 3L, 3L, 3L, 1L, 1L, 2L, 3L, 3L, 1L, 2L,
3L, 3L, 1L, 3L, 1L, 1L, 3L, 3L, 2L, 3L, 3L, 1L, 1L, 1L, 1L, 3L,
1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("X", "Y", "Z"
), class = "factor")), .Names = c("result", "testName", "accountName",
"moduleCode"), class = "data.frame", row.names = c(NA, -250L))
Using this code
library(ggplot2)
boxplot <- ggplot(data=dummy, aes(x=accountName, y=result, fill=moduleCode)) +
geom_boxplot(position=position_dodge(width=0.85), outlier.alpha=0.2) +
facet_wrap(~testName, scales="free_y", nrow=2) +
labs(fill="Module") +
theme_bw() +
theme(axis.ticks.x=element_line(color="black")) +
theme(axis.ticks.y=element_line(color="black")) +
theme(axis.text.x=element_text(size=10, angle=55, hjust=1, vjust=0.975, color="black")) +
theme(axis.text.y=element_text(color="black")) +
theme(panel.background=element_blank()) +
theme(axis.title.x=element_blank()) +
theme(axis.title.y=element_blank()) +
theme(axis.line=element_line(colour="black", size=0.5, linetype ="solid")) +
theme(panel.grid.major.y=element_line(color="#bdbdbd", linetype="dotted")) +
theme(panel.grid.minor.y=element_line(color="#bdbdbd", linetype="dotted")) +
theme(plot.margin=unit(c(0.5, 0.5, 0.5, 0.7), "cm"))
print(boxplot)
I created this plot
NOW, the question is:
How can I scale the y-axis of the facets independently, and manually - best case, so that it works generically, since I wanted to write a script that can handle comparison of two random tests?
In general, I want the range of the scales to be much smaller than the complete data's range, i.e., I want to focus on the range of the lower and upper extremes of the boxplot whiskers only for each of the facets independently.
Those can be obtained using
boxplot.stats(dummy$result)$stats[c(1, 5)]
I'm not looking for the scales='free_y' argument. Also, scale_y_continuous and coord_cartesian won't do the job because, from what I understand, they set scales globally for all facets representing the single tests, which can vary greatly. I tried adding empty layers using geom_blank with dummy data that is limited in range to the lower and upper whiskers extremes. The problem is, depending on the tests, there always might be extreme outliers in the real data still causing the scales to get really wide and thus ignoring the range of the dummy data plugged into geom_blank.
I also tried extracting and manipulating the scales after creating the graph object using function ggplot_build:
graph_object <- ggplot_build(boxplot)
graph_object$layout$panel_scales$y[[1]]$range$range <- boxplot.stats(dummy[with(dummy, testName=="Test A"),]$result)$stats[c(1, 5)]
graph_object$layout$panel_scales$y[[2]]$range$range <- boxplot.stats(dummy[with(dummy, testName=="Test B"),]$result)$stats[c(1, 5)]
I then tried plotting the manipulated object using print(graph_object) and graph_object$plot, however, the scales still are the 'original' wider ones including the extreme values. Am I missing anyhting?
Sorry for the very long post, I might have gotten lost somewhere on the way trying to solve this and overlooked the solution. Therefore, I would be really happy to see some ideas or even a proper solution to this.
Thanks a lot!

Related

getting Error in svd(X) : infinite or missing values in 'x' when using summary of regression model

I am getting an error
Error in svd(X) : infinite or missing values in 'x'
while doing summary of ordinal regression model. This is my code ..
library(MASS)
a <- dget('dput.txt')
lep <- polr(bmicat2 ~ Leptin, data = a,Hess = TRUE)
summary(lep)
Error in svd(X) : infinite or missing values in 'x'
sample data is given to replicate the error. Can someone please help.
structure(list(bmicat2 = structure(c(1L, 3L, 2L, 1L, 1L, 1L,
1L, 1L, 3L, 3L, 3L, 3L, 3L, 1L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 1L,
3L, 3L, 1L, 1L, 2L, 1L, 2L, 2L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, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,
3L, 3L, 1L, 3L, 3L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 3L, 1L, 3L, 3L,
2L, 3L, 2L, 3L, 3L, 1L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 1L, 3L,
1L, 1L, 1L, 3L, 1L, 2L, 3L, 1L, 1L, 3L, 2L, 1L, 3L, 3L, 2L, 2L,
1L, 1L, 3L, 2L, 3L, 3L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 3L,
3L, 3L, 2L, 1L, 1L, 3L, 3L, 1L, 2L, 3L, 3L, 1L, 3L, 3L, 1L, 1L,
1L, 3L, 1L, 3L, 3L, 2L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 1L, 1L, 1L,
3L, 2L, 2L, 2L, 1L, 3L, 1L, 3L, 1L, 3L, 2L, 1L, 2L, 3L, 1L, 3L,
1L, 1L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 3L, 2L, 2L, 2L, 1L, 3L,
1L, 1L, 3L), .Label = c("Normal", "Overweight", "Obesity"), class = "factor"),
Leptin = c(47710.88, 200022.04, 161067.35, 55561.76, 100728.06,
69783.61, 54981.01, 58801.34, 128313.05, 157378.46, 292366.57,
121608.04, 206046.88, 54981.01, 154969.33, 516791.75, 104766.73,
440134.48, 286576.47, 343513.87, 40020.2, 30077.63, 359266.48,
290381.41, 23005.7, 48080.73, 134741.37, 114631.03, 49644.25,
139956.69, 138242.54, 19862, 64541.08, 57119.32, 115382.48,
7238.99, 154969.33, 82321.93, 85406.54, 19170.47, 57208.08,
277488.74, 290791.3, 206703.97, 25333.82, 20134.62, 32823.3,
231036.03, 111986.18, 352190.59, 128041.35, 185025.96, 63451.72,
143404.56, 71163.46, 252067.35, 46223.39, 185077.75, 172339.07,
41381.36, 91498.49, 233969.82, 24245.94, 248133.29, 145890.48,
196431.01, 146690.84, 218617.65, 151333.68, 245695.08, 336242.88,
266936.45, 64105.63, 301181.31, 150192.02, 253863.48, 314169.03,
406059.04, 68228.37, 335171.04, 37547.56, 123713.8, 75034,
45708.91, 67449.43, 15920.57, 38444.37, 19170.47, 174853.97,
236689.18, 22879.68, 34599.46, 57562.83, 177486.58, 244481.84,
122637.48, 58094.2, 82921.65, 382788.21, 119733.17, 64192.75,
8787.33, 17146.98, 21986.45, 13077.71, 18320.19, 119777.74,
61615.67, 5708.97, 24307.05, 244118.62, 10780.13, 12158.23,
80265.64, 70215.06, 122189.66, 48219.23, 156702.5, 128313.05,
115072.95, 152956.29, 107776.73, 108914.21, 85835.09, 13608.28,
24853.42, 58359.56, 29967.69, 168944.14, 22435.55, 67709.18,
17444.89, 51058.87, 21072.27, 34702.75, 9711.01, 43870.71,
113571.74, 26863.91, 294914.16, 15920.57, 23381.51, 309409.6,
19587.14, 99905.3, 79494.37, 90768.21, 131129.25, 411527.63,
132269.4, 83735.58, 92014.19, 57030.54, 62578.38, 131676.19,
238711.8, 48080.73, 429691.15, 88750.76, 32770.45, 163022.26,
98045.93, 8421.27, 113659.95, 27210.74, 421265.4, 225005.8,
39431.91, 18748.27, 224660.83, 13695.33, 11186.36, 42727.62,
863581.67, 18031.36, 250895.16, 326547.74, 69351.93, 34288.82,
64932.66, 23381.51, 392858.01, 32399.47, 176370.64, 31651.9,
17592.65, 93304.24, 71938.68, 297130.98, 58624.7, 311565.88,
143685.29, 204518.07, 28689.43, 612308, 119688.61, 116888.21,
60738.48, 274462.8, 307122.48, 202886.86, 119777.74, 194409.2,
259555.37, 479766.12, 97527.66, 177029.67, 17146.98, 70560.06,
50922.4, 213003.8, 142470.09, 26747.8, 235563.59, 49460.99,
185181.36, 27210.74, 156220.34, 284284.42, 254982.61, 67059.61,
46736.22, 97657.21, 399497.2, 13433.03, 385197.12, 143685.29,
46503.31, 333111.93, 228589.14, 40117.95, 86563.73, 459114.82,
334559.84, 39824.45, 90295.84, 19309.96, 25333.82, 15604.37,
548683.37, 13954.22, 63974.92, 283344.56, 47061.73, 14794.69,
56852.91, 63320.83, 21335.74, 18176.13, 44060.31, 31436.88,
779337.47, 177842.32, 15683.81, 1804.18, 18031.36, 150572.24,
69956.22, 353788.11, 42823.25, 25927.76, 23131.34, 351871.83,
130355.47, 48034.54, 9711.01, 296020.99, 77994.24, 106641.08,
NA, 68876.79, 64323.41, 37947.12, 166071.05, 239189.29, 376318.03,
132041.15, 48818.15, 209454.17, 25572.25, 232324.09, 142750.23,
34185.02, 21204.24, 6040.1, 5708.97, 252190.95, 129219.75,
50922.4, 97786.76, 117464.95, 84978.04, 15122, 6356.95, NA,
299220.74, 28801.64, 535105.27, 421922.32, 9486.52, 159801.65,
139492.77, 40410.71, 28011.68, 67189.58, 10780.13, 173997.11,
15283.91, 192081.41, 169590.94, 29747.24, 57961.45, 55606.38,
351712.55, 157233.52, 117553.73, 201046.58, NA, 204409.11,
468179.87, 201640.99, 374968.45, 330911.94, 153817.84, 45098.7,
83050.16, NA, 20134.62, 20606.55, 328498.24, 22115.29, 40313.21,
196751.22, 6356.95, 8042.41, 252005.56, 124522.47, 9711.01,
168745.34, 110580.08, 78722.96, 104897.35, 43728.34, 20270.11,
339321.01, 170488.25, 55829.38, 173292.85, 29967.69, 5708.97,
30132.53, 12715.53, 180648.71, 146219.86, 33716.28, 85835.09,
110404.53, 327146.88, 259303.38, 164396.3, 206156.31, 21204.24,
105158.65, 152478.41, 327897.07, 29025.44, 36237.65, 426358.52,
265525.58, 52464.02, 287117.75, 658217.42, 67709.18, 107645.6,
46829.29, 186790.78, 68920, 363915.23, 269385.74, 126324.01,
146361.1, 153243.28, 101161.4, 318839.47, 132223.74, 96190,
143966.19, 162141.4, 63495.34, 35371.14, 261070.81, 197071.7,
240146.08, 73100.38, 63713.39, 248622.82, 92616.07, 163120.25,
95026.25, 266807.98, 153434.72, 145937.52, 127950.82, 376487.04,
81208.15, 311998.55, 41767.47, 59595.08, 13256.19, 35011.88,
99083.27, 47571.98, 174450.51, 8296.48, 35524.66, 68747.15,
214064.04, 127272.35, 70603.17, 99256.27, 19862, 145373.38,
184560.15, 57828.63, 115426.71)), row.names = c(NA, -425L
), class = c("tbl_df", "tbl", "data.frame"))
It is an issue of lep$Hessian having NA values. If you do this:
lep <- polr(bmicat2 ~ Leptin, data = a,Hess = TRUE)
lep$Hessian[1,1]<-0
lep$Hessian[1,2]<-0
lep$Hessian[1,3]<-0
lep$Hessian[2,1]<-0
lep$Hessian[3,1]<-0
summary(lep)
You do not have any problems but the output from lep and summary(lep) are pretty much the same, I think. Is it the imputation of 0 to missing values in the Hessian that disturbing?

changing the colour of symbols in sclass plot

I would like to create a plot displaying two variables, with groupings (ellipses) for one and symbols coloured for the other. "RW1" on the x axis, "RW2" on the y axis, grouping ellipses according to "Morph.cluster" and colouring the geometric shapes according to "ALL", which is the genetic cluster. I'm using ade4 in the adegenet package for this. The issue is that I can only seem to get the plot working for one variable at a time.
My data looks like
ID ggpop AC70 AC50 ALL Morph.cluster RW1 RW2 RW3
LT-103 gg3 NA 2 2 1 0.00793 0.01137 -0.00379
LT-109 gg4 1 1 1 2 -0.0156 -0.01056 0.00869
LT-110 gg3 1 1 1 1 0.01585 0.00077 -0.00923
LT-12 gg5 4 4 4 1 0.01299 0.00383 0.00517
LT-122 gg4 NA 3 3 1 0.02727 0.01845 -0.0078
For some samples ggpop is NA. For these samples I hope that I I can just leave the colour empty. If not, I'll just make another gg category and label these ones as empty.
Using
library(ade4)
plot_All = plot(RWggclust2_eds$RW1,RWggclust2_eds$RW2, type="n", yaxt="n",
ylab="RW2 (17%)", xlab="RW1 (26%)")
axis(2, las=2)
myCol2=transp(c('#7F7F7FFF', '#D99694FF', '#00B0F0FF', '#00B050FF', '#000000FF'), 0.6)
abline(h=0,v=0,col="grey", lty=2)
s.class(cbind(RWggclust2_eds$RW1, RWggclust2_eds$RW2),
fac=as.factor(RWggclust2_eds$Morph.cluster),
add.plot=T,
col=myCol2,
cpoint=2,
clabel=0,
pch=c(15,16,17) [as.numeric(RWggclust2_eds$Morph.cluster)],
axesell=F,
addaxes=F,
cstar=0,
cellipse = 1.5)
I get the above with the ellipses drawn the way that I want, defining the morphological clusters, but the colours of the geometric shapes are not what I am looking for. I would like the colours to be according to the five groups defined in “ALL”, which are the genetic clusters.
However, using
RWggclust2_eds <- read.csv("RWggclust2_eds.csv")
str(RWggclust2_eds)
RWggclust2_eds$ALL = as.factor(RWggclust2_eds$ALL)
RWggclust2_eds$ALL = as.factor(RWggclust2_eds$ggpop)
RWggclust2_eds$Morph.cluster = as.factor(RWggclust2_eds$Morph.cluster)
plot_All = plot(RWggclust2_eds$RW1,RWggclust2_eds$RW2, type="n", yaxt="n",
ylab="RW2 (17%)", xlab="RW1 (26%)")
axis(2, las=2)
myCol2=transp(c('#7F7F7FFF', '#D99694FF', '#00B0F0FF', '#00B050FF', '#000000FF'), 0.6)
abline(h=0,v=0,col="grey", lty=2)
s.class(cbind(RWggclust2_eds$RW1, RWggclust2_eds$RW2),
fac=as.factor(RWggclust2_eds$ALL),
add.plot=T,
col=myCol2,
cpoint=2,
clabel=0,
pch=c(15,16,17) [as.numeric(RWggclust2_eds$Morph.cluster)],
axesell=F,
addaxes=F,
cstar=0,
cellipse = 1.5)
I get the above, with ellipses drawn on the genetic clusters (the “ALL” column in my dataset), which is not what I want (I want the shapes to be grouped according to my first plot), but the colours in the plot are correct.
I've tired all sorts of iterations of the "col" argument as well
#index=as.integer(RWggclust2_eds$ALL)
#sclasscolour=rainbow(6)
#sclasscolour=transp(c('#7F7F7FFF', '#D99694FF', '#00B0F0FF', '#00B050FF', '#000000FF'), 0.6)
#myCol2= transp(c(sclasscolour), 1) [index]
The issue is I think that the colour of symbols is being read from "fac", but is seems like the distrubtion of Morph.cluster is as well.
I would be glad to post my datafile if there is a way to do so.
You are almost there. I think in s.class, the color of the points will go together with the eclipse (at least as far as I have tried).
Since you had to layer the eclipse onto an empty plot, I think the easiest solution is to first plot the points according to what you like, then add on the eclipses without the points (setting pch=NA)
I used alpha from ggplot for the transparency but feel free to change it back
library(ade4)
library(ggplot2)
# i use alpha from ggplot2
# how i read and proceeded with your csv file
#CSV = read.csv("RWggclust2_eds.csv")
#RWggclust2_eds = CSV[,c("ggpop","ALL","Morph.cluster","RW1","RW2")]
# similar to what you did
# just providing names
myCol2=alpha(c('#7F7F7FFF', '#D99694FF', '#00B0F0FF', '#00B050FF', '#000000FF'), 0.6)
# set it according to popn
names(myCol2) = levels(RWggclust2_eds$ggpop)
# set the shapes
PCH = c(15,16,17)
# we can wrap it around using with
plot_All = with(RWggclust2_eds,
plot(RW1,RW2, yaxt="n",
ylab="RW2 (17%)", xlab="RW1 (26%)",
pch= PCH[Morph.cluster],
col=myCol2[as.character(ggpop)]
)
)
#provide a legend
legend("topleft",names(myCol2),fill=myCol2,horiz=T,cex=0.7)
axis(2, las=2)
abline(h=0,v=0,col="grey", lty=2)
s.class(cbind(RWggclust2_eds$RW1, RWggclust2_eds$RW2),
fac=as.factor(RWggclust2_eds$Morph.cluster),
add.plot=T,
cpoint=2,
clabel=0,
pch=NA,
axesell=F,
addaxes=F,
cstar=0,
cellipse = 1.5)
You get the plot:
Below is the data for making the plot
dput(RWggclust2_eds)
structure(list(ggpop = structure(c(3L, 4L, 3L, 5L, 4L, 1L, 3L,
3L, 1L, 1L, 4L, 3L, 5L, 1L, 3L, 1L, 1L, 1L, 1L, 4L, 3L, 1L, 1L,
3L, 1L, 3L, 3L, 5L, 3L, 1L, 3L, 1L, 3L, 3L, 3L, 1L, 4L, 3L, 1L,
4L, 5L, 3L, 4L, 1L, 2L, 2L, 5L, 5L, 4L, 4L, 2L, 5L, 2L, 2L, 3L,
3L, 1L, 1L, 3L, 4L, 1L, 3L, 1L, 1L, 1L, 4L, 1L, 2L, 4L, 1L, 5L,
1L, 4L, 3L, 5L, 1L, 3L, 5L, 1L, 3L, 4L, 4L, 5L, 5L, 3L, 3L, 1L,
1L, 3L, 2L, 1L, 1L, 2L, 1L, 3L, 1L, 1L, 1L, 3L, 1L, 3L, 1L, 5L,
3L, 1L, 3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0122,
-0.00317, 0.00018, 0.00051, -0.00184, -0.00323, 0.01701,
-0.00027, 0.00288, -0.00689, -0.01864, 0.003, 0.00463, 0.0119,
-0.01515, 0.00364, 0.02048, -0.01233, -0.00774, -0.00348,
-0.00219, 0.00676, -0.00978, 0.00767, -0.00819, -0.01892,
-0.00584, 0.012, 0.00617, -0.01743, -0.02724, -0.03363, 0.01004,
0.02196, 0.01074, 0.0053, 0.00197, -0.01368, 0.00802, -0.00659,
-0.00521, -0.00938, 0.00901, 0.00639, 0.02151, -0.01637,
-0.02571, -0.00354, -0.00396, 0.03228, 0.01599, -0.00646,
0.00993, 0.02582, -0.01889, 0.01162, 0.00487, 0.02127, -0.00994,
-0.01862, 0.01338, 0.00229, 0.01818, 0.00687, 0.00545, 0.01723,
0.00438, 0.00832, 0.00095, -0.00751, -0.00645, -0.01469,
-0.01141, 0.01079, -0.00567, 0.00348, 0.00904, 0.00055, 0.02002,
-0.00382, -0.00244, 0.03351, -0.01225, 0.00865, -0.00105,
-0.01337, 0.00853, -0.00584, -0.001, -0.00068, -0.01427,
0.00742, 0.00352, -0.01016, -0.00713, -0.00129, -0.01557,
-0.00792, -0.00067, 0.00432, -0.01509, 0.03477, 0.01061,
-0.00097, 0.00797, -0.01066, -0.01534, 0.0192, 0.00266, -0.00549,
-0.02314, 0.00139, -0.02194, -0.00406, 0.00287, -0.01561,
-0.00329, 0.01625, 0.00822, 0.01719, -0.00545, 0.0019, 0.00278,
0.00436, 0.01424, -0.01102, -0.01331, 0.00928, 0.01181, 0.00416,
0.01673, 0.00715, -0.0159, -0.00208, -0.03367, 9e-04, -0.00011,
-0.02768, -0.01608, -0.00345, 0.00062, 0.00048, -0.01542,
0.02665, -0.0181, 0.0098, -0.00995, 0.00842, 0.00123, 0.00896,
0.02197, -0.00163, -0.01072, -0.01137, -0.01126, -0.01656,
0.00518, 0.03913, -0.02236, -0.02881, -0.0117, 0.01644, -0.00465,
-0.00811, 0.00107, 0.02483, 0.00197, -0.01018, 0.01023, 0.01279,
-0.01052, -0.0069, -0.01409, 0.01462, 0.00435, 0.01912, -0.01392,
0.02166, 0.00621, 0.0112, -0.01114, 0.00728, -0.0086, 5e-05,
0.00352, -0.00939, -0.01502, 0.00394, 0.01684, -0.00077,
-0.00037, 0.00563, -0.03636, 0.00443, 0.01819, 0.00603, -0.00363,
-0.02126, 0.01857, 0.00014, -0.03048, 0.00621, -0.00452,
0.02451, 0.00715, -0.00705, 0.01619, -0.016, -0.00362, -0.00949,
0.02046, -0.016, -0.01655, 0.00079, 0.00188, -0.01497, 0.01269,
0.02089, -0.01006, -0.01574, -0.00349, -0.01533, 0.01438,
0.0055, -0.00257, 0.00254, -0.00647, -0.00356, 0.02158, -0.03029,
0.01802, -0.00391, -0.01141, -0.0355, 0.00911, 0.00136, 0.00631,
-0.00551, -0.01403, 0.00929, 0.0059, -0.00238, 0.00028, -0.01683,
0.00347, -0.0213, 0.00411, 0.00953, 0.00029, -0.0028, 0.01801,
0.00405, -0.0079, -0.01496, -0.00638, -0.01092, -0.00773,
-0.00368, 0.00526, -0.00739), RW2 = c(0.01137, -0.01056,
0.00077, 0.00383, 0.01845, 0.00058, 0.02422, 0.00129, -0.01173,
-0.0161, 0.01019, 0.00135, 0.00579, 0.00576, 0.00751, 0.01384,
0.00408, -0.00634, -0.00044, -0.00168, -0.00021, 0.00019,
0.00428, -0.00924, 0.00098, 0.00518, -0.00184, -0.00469,
-0.00921, -3e-05, 0.00688, -0.00778, 0.00165, -0.00664, 0.00856,
-0.00157, 0.00322, -7e-05, -0.00405, 6e-04, -0.00464, -0.0019,
-0.00849, 0.00363, -0.01107, 0.00175, -0.03478, -0.00816,
-0.00338, 0.02075, -0.00039, -0.02465, -0.01461, 0.00529,
-0.00144, -0.00259, -0.00803, -0.00693, 0.00761, 0.01159,
0.01422, -0.00876, -0.00469, -0.00052, 0.00823, 0.01019,
0.00032, 0.00842, 0.02197, 4e-04, -0.02939, 0.00432, 0.00774,
0.00848, -0.00342, -0.00953, -0.01048, -0.0014, -0.01465,
0.0027, 0.00879, -4e-05, 0.00985, -0.03324, 0.01427, 0.00036,
-0.01587, -0.0012, 0.00331, 0.0089, -0.00684, -0.0348, 0.00497,
0.01468, -0.00218, -0.01662, 0.00608, -0.01143, 0.00312,
-0.01165, 0.00966, 0.00179, -0.01137, 0.00335, 0.0152, -0.00442,
0.01031, -0.01745, -0.01658, 0.003, 0.011, -0.00583, 0.0171,
0.00062, 0.00842, 0.00759, -0.00401, 0.01916, -0.00491, 0.01416,
0.00117, -0.00164, -0.02815, 0.00648, 0.00067, -0.006, -0.00454,
0.00368, 0.00666, 0.00352, 0.00368, -0.00999, 0.01213, -0.0022,
-0.0015, -0.02383, 0.00748, -4e-05, -0.02224, -0.01355, 0.00525,
0.00142, 0.01344, 0.00639, 0.00822, 0.01765, 0.00431, -0.0019,
0.00204, 0.03139, 0.00251, -0.00123, -0.00833, -0.01289,
0.00716, 0.00198, 0.00263, 0.00632, 0.00179, -5e-05, 0.0088,
-0.0017, -0.00574, 0.00356, -0.02567, 0.00984, 0.00209, 0.0072,
0.00377, -0.0085, 0.01386, 0.00746, 0.00415, -0.01666, 0.00516,
0.00883, -0.01979, 0.01931, 0.00208, 0.00411, 0.01303, -0.00108,
0.00854, -0.00262, -0.02486, 0.0179, -0.00035, 0.01651, 0.00952,
0.00668, -0.00279, 0.00344, 0.00314, -0.00599, 0.00954, -0.02629,
-0.00226, 0.00282, 0.00765, 0.00624, -0.00497, -0.00269,
0.01129, -0.02122, 0.00841, -0.0011, -0.00747, 0.01913, -0.0024,
0.01528, -0.00184, -0.005, -0.01863, 0.02115, -0.00814, 0.01531,
0.00641, -0.00111, -0.01767, 0.01528, -0.00414, -0.01503,
-0.00082, 0.00429, 0.00032, 0.01733, -0.00656, 0.00176, -0.00597,
0.00423, -0.00541, 0.01085, -0.00327, -4e-04, 0.01826, -0.00852,
0.0098, -0.01052, 0.0188, 0.00234, -0.00113, -0.00335, -0.01121,
-0.00305, 0.0065, -0.0032, -0.00877, -0.01151, -0.01051,
-0.00295, 0.00751, -0.00828, -0.00164, -0.00109, 0.01665,
0.01285, -0.00875, -0.01182, -0.01427, 0.01631, -0.00241,
-0.0055, 0.00393, 0.00351, 0.00061, -0.00063, 0.01053, -0.00094,
0.00509, 0.00162, 0.00295, -0.00789, -0.03162, 0.00523, -0.00361,
-0.00171, -0.00459, 0.00237, -0.03454, -0.01103, 0.00944)), class = "data.frame", row.names = c(NA,
-281L))

error bars should not be very long in barplots in r

I am plotting grouped barplots with error bars, but my error bars are very long as in this image
[![https://i.stack.imgur.com/VUByO.png][1]][1].
I would like shorter error bars as in this image
[![https://i.stack.imgur.com/JhaUJ.png][2]][2]
The code used
per$Leaf_Location <- factor(per$Leaf_Location, levels = unique(per$Leaf_Location))
per$Time <- factor(per$Time, levels = unique(per$Time))
ggplot(per, aes(x=Leaf_Location, y=Damage, fill=as.factor(Time))) +
stat_summary(fun.y=mean,
geom="bar",position=position_dodge(),colour="black",width=.7,size=.7) +
stat_summary(fun.ymin=min,fun.ymax=max,geom="errorbar",
color="black",position=position_dodge(.7), width=.2) +
stat_summary(geom = 'text', fun.y = max, position = position_dodge(.7),
label = c("a","b","c","d","d","a","b","c","d","d","a","b","c","d","d"), vjust = -0.5) +
scale_fill_manual("Legend", values = c("grey36","grey46","grey56","grey76","grey86","grey96")) +
xlab("Leaf Location") +
ylab("Damage ") +
theme_bw()
data:
per =
structure(list(Site = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2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L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c("20_days",
"40_days", "60_days", "80_days", "100_days"), class = "factor"),
Damage = c(25.25, 26.07, 24.43, 20.73, 17.8, 6.9, 45.05,
33.47, 24.43, 51.67, 41.72, 34.17, 81.67, 73.33, 55.83, 34.28,
26.08, 13.28, 26.27, 14.1, 6.93, 37.55, 29.33, 23.62, 49.17,
38.45, 31.38, 70.83, 60.83, 44.2, 31.03, 25.2, 14.97, 14.38,
6.5, 4.33, 52.2, 39.17, 30.97, 75, 62.5, 38.33, 87.5, 62.5,
57.5, 45.02, 31.02, 26.07, 46.72, 34.32, 21.5, 50.83, 34.23,
25.25, 45.83, 33.47, 27.7, 67.67, 57.5, 52.67, 30.98, 23.62,
9.1, 18.17, 18.57, 10.15, 46.67, 34.27, 23.62, 54.17, 40.05,
29.37, 70.83, 59.17, 47.53, 8.67, 5.63, 0.87, 9.87, 3.03,
0, 17.75, 6.88, 0, 62.5, 37.5, 27.7, 70.83, 57.5, 50.83,
6.5, 2.17, 1.3, 6.93, 3.03, 0.53, 14.82, 5.2, 0, 37.5, 28.52,
13, 75, 37.5, 37.5, 15.3, 9.53, 5.63, 9.43, 3.03, 0.43, 16.4,
6.07, 0, 57.5, 34.23, 21.98, 78.33, 62.5, 37.5, 12.08, 6.5,
1.3, 10.73, 3.03, 0, 15.2, 3.9, 0.43, 62.5, 37.5, 21.98,
64.17, 55.83, 41.73, 8.73, 3.57, 0, 8.57, 2.17, 0, 16.5,
7.7, 0.43, 42.58, 36.68, 13, 65.83, 47.5, 37.5, 8.03, 5.07,
0.43, 10.68, 7.27, 3.5, 48.38, 38.42, 24.83, 45.03, 38.4,
30.8, 73.33, 63.33, 50.83, 3.37, 2.17, 0.9, 9, 6.02, 5.2,
21.07, 12.37, 6.02, 45.02, 32.65, 21.67, 68.78, 56.68, 50,
0, 0, 0, 7.8, 4.33, 4.33, 25.17, 20.65, 13.15, 48.37, 39.23,
27.17, 75.83, 62.5, 49, 11.78, 12.72, 3.8, 20.18, 14.87,
8.95, 46.7, 39.32, 33.03, 49.18, 40.05, 24.43, 69.17, 60,
48.33, 0, 0, 0, 15.25, 9.82, 7.75, 45.9, 38.47, 35.52, 50.88,
37.61, 33.47, 79.17, 71.67, 58.33)), .Names = c("Site", "Field",
"Leaf_Location", "Time", "Damage"), row.names = c(NA, -225L), class = "data.frame")
Here's a simplified reproducible example to explain
first, some dummy data:
per = data.frame(x=rep(c('a','b'), each=100), y=c(2+rnorm(100), 3+rnorm(100,0,2)))
Now you are plotting the error bars, using fun.ymin=min, fun.ymax=max, which will cause them to extend the full range of the data, as in the following graph:
ggplot(per, aes(x, y)) +
stat_summary(fun.y = mean, geom="bar") +
geom_point(position = position_jitter(0.1)) +
stat_summary(fun.ymin=min, fun.ymax=max, geom="errorbar", width=0.4) +
theme_bw()
Whereas, it is more conventional to use error bars that extend either +/- one standard deviation, as in the following:
ggplot(per, aes(x, y)) +
stat_summary(fun.y = mean, geom="bar") +
stat_summary(
fun.ymin=function(y) {mean(y) - sd(y)},
fun.ymax=function(y) {mean(y) + sd(y)},
geom="errorbar", width=0.2) +
theme_bw()
Or one standard error, like this:
ggplot(per, aes(x, y)) +
stat_summary(fun.y = mean, geom="bar") +
stat_summary(
fun.ymin=function(y) {mean(y) - sqrt(var(y)/length(y))},
fun.ymax=function(y) {mean(y) + sqrt(var(y)/length(y))},
geom="errorbar", width=0.2) +
theme_bw()
EDIT - example data were added to question, after this answer was originally posted
We can applying exactly the same approach as above to your example data:
ggplot(per, aes(x=Leaf_Location, y=Damage, fill=as.factor(Time))) +
stat_summary(fun.y=mean, geom="bar",position=position_dodge(),colour="black",width=.7,size=.7) +
stat_summary(
fun.ymin=function(y) {mean(y) - sqrt(var(y)/length(y))},
fun.ymax=function(y) {mean(y) + sqrt(var(y)/length(y))},
geom="errorbar",
position=position_dodge(.7), width=.2)

bestglm : Error in levels(x)[x] : only 0's may be mixed with negative subscripts

was trying to use bestglm function via the AIC method to come up with a logistic regression model.
The following is a summary of the data set I ran it on:
dataset summary
The following is the line I ran:
best1 <- bestglm(trainset, IC="AIC", family=binomial)
The following is the error message I have received:
Error in levels(x)[x] : only 0's may be mixed with negative subscripts
In addition: Warning messages:
1: In model.response(mf, "numeric") :
using type = "numeric" with a factor response will be ignored
2: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors
dput(testset)
structure(list(EyeContact = structure(c(2L, 1L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L,
1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 2L, 2L,
2L, 1L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 2L,
1L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 2L, 1L,
2L), .Label = c("N", "Y"), class = "factor"), Post.Processing = structure(c(2L,
2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 2L,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1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("N", "Y"), class = "factor"),
SkinTone = structure(c(3L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L,
2L, 1L, 2L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
2L, 2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 1L, 3L, 3L, 2L, 2L, 1L,
2L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
2L, 2L, 3L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L,
2L, 1L, 1L, 2L), .Label = c("Dark", "Fair", "NULL"), class = "factor"),
Smile = structure(c(5L, 3L, 1L, 1L, 5L, 4L, 1L, 1L, 5L, 1L,
4L, 4L, 1L, 1L, 4L, 3L, 1L, 2L, 2L, 1L, 4L, 3L, 5L, 5L, 1L,
3L, 1L, 5L, 5L, 2L, 5L, 1L, 2L, 5L, 1L, 2L, 2L, 1L, 4L, 5L,
5L, 4L, 3L, 5L, 2L, 4L, 2L, 3L, 5L, 3L, 5L, 4L, 1L, 5L, 5L,
4L, 5L, 5L, 5L, 1L, 5L, 2L, 2L, 1L, 5L, 5L, 3L, 5L, 4L, 4L,
5L, 4L, 1L, 3L, 2L, 1L, 1L, 5L, 4L, 5L, 4L, 5L, 5L, 1L, 2L,
4L, 3L, 5L, 5L, 1L, 5L, 1L, 4L, 1L, 4L, 5L, 1L, 5L, 4L, 4L,
5L, 5L, 1L), .Label = c("CS", "NS", "NULL", "O", "ST"), class = "factor"),
HairLength = structure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1L, 1L, 3L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("N", "NULL", "Y"), class = "factor"),
Makeup = structure(c(4L, 4L, 2L, 2L, 3L, 2L, 2L, 3L, 2L,
2L, 1L, 1L, 2L, 2L, 3L, 4L, 1L, 2L, 2L, 4L, 1L, 4L, 2L, 3L,
4L, 2L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L,
2L, 2L, 2L, 3L, 3L, 2L, 1L, 2L, 3L, 3L, 1L, 2L, 1L, 3L, 2L,
4L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 1L, 2L, 2L,
3L, 2L, 3L, 2L, 4L, 4L, 2L, 3L, 2L, 1L, 2L, 3L, 3L, 1L, 2L,
1L, 2L, 4L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 2L, 4L, 2L, 2L,
1L, 3L, 1L, 1L), .Label = c("H", "L", "N", "NULL"), class = "factor"),
Results = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = c("N", "Y"), class = "factor"),
prediction = c(9.32475933917106e-09, 0.0385259384817495,
0.0678681154072461, 0.234968717458685, 0.0290199853775816,
0.171162293958793, 0.00129264601900783, 0.00675484440459677,
0.128155946032347, 0.133539709174044, 0.118744423809008,
0.060206929901843, 0.128155946032347, 0.146426608321148,
0.0552623520735392, 0.227991153820736, 2.54581077993876e-08,
0.0195543511193415, 0.128155946032347, 0.256310568145846,
0.0520386124569491, 0.111383163512112, 0.0402597164944323,
0.0141022328039524, 0.55471858422641, 0.128155946032347,
0.35526622136263, 0.128155946032347, 0.382743622548627, 0.00485338573377989,
0.128155946032347, 0.0324058895421302, 0.320728574893713,
0.320728574893713, 0.35526622136263, 0.146426608321148, 0.0179540767871002,
0.398798221640772, 0.362407391381727, 0.00485338573377989,
0.00129264601900783, 0.128155946032347, 0.0823507208338033,
0.00675484440459677, 0.0195543511193415, 0.320728574893713,
0.128155946032347, 0.174534177022049, 0.0477307982973154,
0.0625662879441275, 0.0174929064796301, 0.135882446473831,
0.00696631574219797, 0.419831884479578, 0.0862150002573959,
0.128155946032347, 0.0698582713166507, 0.128155946032347,
0.174534177022049, 0.146426608321148, 0.0234463612462439,
0.0141022328039524, 0.0239924885903984, 0.0290199853775816,
3.15391485574326e-09, 1.14002192545012e-08, 0.0345251778805331,
0.208346726243955, 0.0203551415502053, 0.020830802150735,
0.128155946032347, 0.197915823620481, 0.146426608321148,
9.32475933917106e-09, 9.32475933917106e-09, 0.128155946032347,
0.0552623520735392, 0.016802787713206, 0.0345251778805331,
0.146426608321148, 0.00675484440459677, 0.00579370288906212,
0.320728574893713, 0.00316694181006374, 0.320728574893713,
0.146426608321148, 1.66951123737628e-08, 0.0466701670833381,
0.0402597164944323, 0.382743622548627, 0.128155946032347,
0.128155946032347, 0.118744423809008, 0.171162293958793,
0.0402597164944323, 0.146426608321148, 0.0895467055067367,
0.0110101302622226, 0.05872534886842, 0.35526622136263, 0.0141022328039524,
0.118744423809008, 0.00414031965843898)), .Names = c("EyeContact",
"Post.Processing", "HairColour", "Animals", "Age", "Backview",
"SkinTone", "Smile", "HairLength", "HairTexture", "HairStyle",
"Outfit", "Background", "TypeofShot", "Obstruction", "Makeup",
"Results", "prediction"), row.names = c(2L, 3L, 9L, 17L, 19L,
22L, 23L, 28L, 29L, 41L, 42L, 45L, 47L, 53L, 55L, 67L, 68L, 69L,
72L, 78L, 80L, 81L, 82L, 83L, 84L, 90L, 94L, 95L, 101L, 103L,
106L, 111L, 113L, 116L, 118L, 119L, 120L, 122L, 123L, 128L, 130L,
134L, 136L, 138L, 144L, 146L, 148L, 150L, 152L, 161L, 162L, 163L,
165L, 168L, 174L, 175L, 180L, 181L, 183L, 194L, 204L, 207L, 210L,
213L, 214L, 215L, 221L, 224L, 230L, 234L, 235L, 236L, 237L, 239L,
240L, 244L, 249L, 250L, 255L, 259L, 262L, 272L, 277L, 278L, 280L,
281L, 284L, 289L, 296L, 297L, 304L, 306L, 308L, 316L, 321L, 323L,
327L, 329L, 332L, 335L, 337L, 339L, 340L), class = "data.frame")
The model is running, it gives some output but the print method doesn't work.
> print(best1)
AIC
Best Model:
Error in levels(x)[x] : only 0's may be mixed with negative subscripts
In addition: Warning messages:
1: In model.response(mf, "numeric") :
using type = "numeric" with a factor response will be ignored
2: In Ops.factor(y, z$residuals) : '-' not meaningful for factors
but best1 structure is correct and best1$BestModel is provided
best1$BestModel
Call: glm(formula = y ~ ., family = family, data = Xi, weights = weights)
Coefficients:
(Intercept) Post.ProcessingY Age22 Age23
-40.416 -244.338 59.277 -41.652
SkinToneFair SkinToneNULL SmileNS SmileNULL
245.316 -5.102 -80.986 -142.908
SmileO SmileST HairLengthNULL HairLengthSM
-121.258 -80.482 -159.677 -20.045
OutfitI OutfitNULL OutfitO BackgroundN
41.652 -41.653 -410.492 19.895
BackgroundNULL BackgroundP TypeofShotECU TypeofShotLS
-82.640 -208.283 16.369 -101.467
TypeofShotMS MakeupL MakeupN MakeupNULL
101.819 39.438 -122.850 285.187
Degrees of Freedom: 102 Total (i.e. Null); 79 Residual
Null Deviance: 69.99
Residual Deviance: 5.545 AIC: 53.55
You could replace the print.bestglm method with
print.bestglm <- function (x, ...)
{
ti <- x$Title
cat(ti, fill = TRUE)
if ((x$ModelReport$Bestk > 0) || (x$ModelReport$IncludeInterceptQ)) {
cat("Best Model:", fill = TRUE)
if (any(x$ModelReport$NumDF > 1))
out <- summary(x$BestModel)
else out <- summary(x$BestModel)$coefficients
print(out)
}
else cat("Best Model is the null model with no parameters.",
fill = TRUE)
}
The problem is that the code uses a deprecated feature. It calls aov on a glm object, which is wrong. I think that using the replacement function should solve the problem.

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,
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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 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