How to perform multiple t.tests with R package srvyr? - r

As a follow up on a recent SO question (see here) I am wondering how to perform multiple t.tests in R with weighted data (package srvyr). I cant make it run and would be happy if anyone could help me here. I added a random sample in the code below.
Many thanks!
#create data
surveydata <- as.data.frame(replicate(1,sample(1:5,1000,rep=TRUE)))
colnames(surveydata)[1] <- "q1"
surveydata$q2 <- sample(6, size = nrow(surveydata), replace = TRUE)
surveydata$q3 <- sample(6, size = nrow(surveydata), replace = TRUE)
surveydata$q4 <- sample(6, size = nrow(surveydata), replace = TRUE)
surveydata$group <- c(1,2)
#replace all value "6" wir NA
surveydata[surveydata == 6] <- NA
#add NAs to group 1 in q1
surveydata$q1[which(surveydata$q1==1 & surveydata$group==1)] = NA
surveydata$q1[which(surveydata$q1==2 & surveydata$group==1)] = NA
surveydata$q1[which(surveydata$q1==3 & surveydata$group==1)] = NA
surveydata$q1[which(surveydata$q1==4 & surveydata$group==1)] = NA
surveydata$q1[which(surveydata$q1==5 & surveydata$group==1)] = NA
#add weights
surveydata$weights <- round(runif(nrow(surveydata), min=0.2, max=1.5), 3)
#create vector for relevant questions
rquest <- names(surveydata)[1:4]
# create survey design
library(srvyr)
surveydesign <- surveydata %>%
as_survey_design(strata = group, weights = weights, variables = c("group", all_of(rquest)))
# perform multiple t.test (doesn't work yet)
outcome <- do.call(rbind, lapply(names(surveydesign$variables)[-1], function(i) {
tryCatch({
test <- t.test(as.formula(paste(i, "~ survey")), data = surveydesign)
data.frame(question = i,
group1 = test$estimate[1],
group2 = test$estimate[2],
difference = diff(test$estimate),
p_value = test$p.value, row.names = 1)
}, error = function(e) {
data.frame(question = i,
group1 = NA,
group2 = NA,
difference = NA,
p_value = NA, row.names = 1)
})
}))

As I understand it you have a series of question columns in the example q1 to q4. You've used srvyr to generate a weights column. It is possible in our data that for a particular question one entire group maybe all NA and you'd like to generate results into a df even when that is true. You want a weighted Student's t-test making use of the weights column not a simple t-test. The only function I found that provides that is weights::wtd.t.test which doesn't offer a formula interface but wants to be fed vectors.
In order of steps taken:
Load requisite libraries
library(srvyr)
library(dplyr)
library(rlang)
library(weights)
Build a custom function that removes the NAs by variable, pulls the vectors for x, y, weightx, weighty, runs the test, and extracts the info you want into a df row.
multiple_wt_ttest <- function(i) {
i <- ensym(i)
xxx <- surveydata %>%
filter(!is.na(!!i)) %>%
split(.$group)
newx <- pull(xxx[[1]], i)
newy <- pull(xxx[[2]], i)
wtx <- pull(xxx[[1]], weights)
wty <- pull(xxx[[2]], weights)
test <- wtd.t.test(x = newx,
y = newy,
weight = wtx,
weighty = wty,
samedata = FALSE)
data.frame(question = rlang::as_name(i),
group1 = test$additional[[2]],
group2 = test$additional[[3]],
difference = test$additional[[1]],
p.value = test$coefficients[[3]])
}
Once we have the function we can use lapply to apply it column by column (notice it handles the case in q2 where group == 1 is all NA.
lapply(names(surveydata)[1:4], multiple_wt_ttest)
#> [[1]]
#> question group1 group2 difference p.value
#> 1 q1 NaN 3.010457 NaN NA
#>
#> [[2]]
#> question group1 group2 difference p.value
#> 1 q2 3.009003 3.071842 -0.06283922 0.515789
#>
#> [[3]]
#> question group1 group2 difference p.value
#> 1 q3 2.985096 2.968867 0.0162288 0.8734034
#>
#> [[4]]
#> question group1 group2 difference p.value
#> 1 q4 2.856255 3.047787 -0.1915322 0.04290471
Finally, wrap it in a do.call and rbind to make the df you desire
do.call(rbind, lapply(names(surveydata)[1:4], multiple_wt_ttest))
#> question group1 group2 difference p.value
#> 1 q1 NaN 3.010457 NaN NA
#> 2 q2 3.009003 3.071842 -0.06283922 0.51578905
#> 3 q3 2.985096 2.968867 0.01622880 0.87340343
#> 4 q4 2.856255 3.047787 -0.19153218 0.04290471
Your data (without showing all the gyrations to create it and heading the first 200 rows)
surveydata <-
structure(list(q1 = c(NA, 1L, NA, 4L, NA, 5L, NA, 3L, NA, 5L,
NA, 5L, NA, 1L, NA, 5L, NA, 3L, NA, 4L, NA, 5L, NA, 4L, NA, 2L,
NA, 5L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2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L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("1", "2"), class = "factor"),
weights = c(1.445, 0.408, 0.621, 0.961, 1.492, 0.625, 1.131,
0.246, 0.612, 0.621, 1.311, 0.649, 1.282, 0.898, 1.268, 0.641,
0.764, 0.759, 0.306, 0.707, 0.899, 0.785, 1.279, 0.458, 0.882,
0.384, 1.492, 0.468, 0.785, 0.707, 0.489, 1.113, 0.692, 0.293,
0.642, 1.327, 0.362, 1.405, 1.173, 0.732, 0.661, 0.522, 1.001,
0.374, 1.181, 0.819, 1.389, 0.43, 0.477, 0.879, 0.634, 0.417,
0.359, 1.007, 0.866, 0.203, 1.469, 0.294, 1.326, 1.391, 0.871,
1.036, 1.251, 0.417, 1.074, 1.268, 0.963, 0.469, 0.215, 1.074,
0.644, 1.054, 0.787, 0.714, 0.568, 0.397, 1.421, 0.692, 0.262,
0.644, 0.793, 0.808, 0.25, 0.842, 1.039, 0.359, 0.987, 1.257,
0.301, 0.203, 0.823, 1.328, 1.192, 0.256, 1.099, 0.668, 1.129,
0.413, 0.266, 1.121, 0.893, 1.484, 0.568, 1.255, 0.531, 0.461,
0.773, 0.298, 0.233, 0.676, 0.478, 0.806, 0.556, 0.201, 0.801,
0.348, 1.396, 0.552, 0.384, 0.615, 0.499, 0.819, 0.954, 0.943,
0.956, 0.323, 0.706, 0.699, 0.9, 1.156, 1.436, 1.115, 0.762,
0.258, 1.421, 0.644, 1.349, 0.251, 0.735, 0.479, 1.055, 1.395,
1.062, 1.155, 0.869, 0.436, 0.415, 0.745, 1.247, 0.21, 0.879,
0.776, 0.747, 0.835, 0.609, 0.733, 0.563, 1.067, 1.436, 0.679,
1.497, 1.385, 1.087, 1.286, 0.503, 0.738, 0.504, 0.665, 1.421,
1.288, 0.691, 0.972, 0.467, 0.425, 0.406, 0.862, 0.749, 0.935,
0.291, 0.444, 1.118, 1.048, 0.886, 0.982, 0.578, 1.402, 0.778,
1.139, 0.804, 0.618, 1.147, 0.594, 0.984, 0.986, 0.941, 0.794,
0.323, 1.41, 0.902, 0.417)), row.names = c(NA, 200L), class = "data.frame")

Related

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,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, 1L, 1L, 2L, 1L, 2L, 1L, 1L,
2L, 1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L,
2L, 2L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L,
3L, 1L, 3L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L,
1L, 3L, 2L, 2L, 2L, 2L, 2L, 3L, 1L, 2L), RW1 = c(0.00793,
-0.0156, 0.01585, 0.01299, 0.02727, 0.00344, 0.031, 0.01568,
-0.00411, -0.00076, 0.01339, 0.02017, 0.02123, -0.0061, 0.01376,
-0.02301, 0.00501, 0.02418, -0.00102, -0.00813, 0.0032, -0.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))

Setting names of new calculated variables and assign values using a function

I am writing a function, which is using references from two data sets (RawDataSplit and Hierarchy). All these datasets are connected. I need to summarise the data based on a function that will be applied to all the selected columns. I am using summarise_ to do this.
I have created a function which will be used to summarise the columns per_qid, this is to calculate percentage for each column.
Then I am creating a function with three arguments: fav_fun(dataset,levelname,demo_var)
dataset is for the data on which we need aggregations, in this case it is RawDataSplit
levelname is one of the column in RawDataSplit by which data would be grouped
demo_var would be one of the variable chosen from data set 'Hierarchy', this would be one of the demographic variable like Age.
Function for aggregation:
per_qid<-function(x){
counts<-sum(ifelse(x==4|x==5,1,0),na.rm=TRUE
total<-sum(ifelse(x==1|x==2|x==3|x==4|x==5,1,0),na.rm=TRUE)
#Masking and rounding
x<- ifelse(total<masking_criteria,NA,roundUp(counts*100/total)) )
x
}
Function to aggregate RawDataSplit file:
fav_fun<-function(dataset,levelname,demo_var){
demo_var<- enquo(demo_var)
levelname<-enquo(levelname)
demographic<-Hierarchy[,demo_var]
demo<-levels(demographic)
summ_method<- paste('per_qid(',questions,')')
summ_name<-paste0(questions,'_',demographic)
fav <- dataset %>%
group_by(!!levelname) %>%
filter(status=="Complete") %>%
filter(!!demo_var==demo[1]) %>%
summarise_(.dots = setNames,(summ_method,summ_name))
fav
}
this fav_fun is throwing different kind of errors sometimes for summarise_ and sometimes for other variables. Maybe something is wrong with the quoting and enquoting of variable also.
The sample data for these datasets are:
RawDataSplit:
structure(list(QID_1 = c(4L, 3L, 5L, 2L, 5L, 5L), QID_2 = c(4L,
3L, 5L, 2L, 5L, 5L), QID_3 = c(4L, 4L, 4L, 6L, 5L, 5L), mastercode_1 = c(NA,
NA, NA, NA, NA, NA), mastercode_2 = structure(c(1L, 1L, 1L, 1L,
1L, 1L), .Label = "BROLLAND", class = "factor")), row.names = c(NA,
-6L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00372498>)
questions:
c("QID_1", "QID_10", "QID_11")
Hierarchy : (for now I have included one column only which is Age, I need to use it as demo_var in the function)
structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4L, 4L,
NA, 4L, NA, 3L, 3L, 2L, 4L, 3L, 3L, 3L, 4L, 5L, 4L, NA, NA, 3L,
3L, 3L, 5L, 3L, 3L, 4L, NA, 4L, 4L, 3L, 3L, 4L, 3L, 4L, 3L, 4L,
4L, NA, 4L, 3L, 4L, 3L, 4L, 4L, NA, 5L, 4L, 3L, 4L, 3L, 4L, 3L,
NA, NA, 3L, NA, 3L, 4L, 3L, 3L, NA, 4L, 4L, 4L, 4L, 3L, 4L, 3L,
2L, 2L, 3L, 4L, 4L, 2L, 2L, 4L, 2L, 2L, 4L, 2L, 2L, 4L, 3L, 2L,
3L, 4L,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NA, 2L, 2L, 5L, NA, 4L, 3L, 4L, 2L, 2L, NA, 2L, NA, 4L,
NA, 4L, 2L, 4L, NA, 2L, NA, 3L, 4L, 4L, 2L, NA, NA, NA, 2L, 3L,
4L, 4L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 4L, 3L, 3L, 2L, 3L, 4L, NA,
4L, 3L, 4L, 2L, 4L, 4L, 4L, 2L, NA, 3L, NA, 3L, NA, 3L, 4L, 4L,
3L, 4L, 3L, 2L, 4L, 5L, 3L, 3L, NA, 3L, NA, NA, NA, NA, 2L, NA,
NA, NA, NA, NA, NA, 3L, NA, 2L, NA, 4L, NA, 3L, NA, NA, 3L, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4L, NA,
2L, NA, NA, NA, NA, 3L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 3L, NA, NA, NA, NA, 2L, NA, 2L, 2L, NA, 3L, NA, NA, NA, NA,
NA, NA, NA, 2L, NA, 2L, 3L, NA, 2L, NA, 2L, 3L, 3L, NA, 2L, 4L,
3L, NA, NA, NA, NA, NA, NA, NA, 2L, NA, NA, NA, NA, NA, NA, NA,
3L, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 2L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 2L, NA, NA, 2L, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
2L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 3L,
NA, 3L, 3L, 2L, 2L, NA, 3L, 1L, NA, NA, NA, 4L, 2L, NA, NA, NA,
NA, NA, 5L, NA, NA, NA, NA, 2L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, 5L, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 2L, NA, NA, NA, NA, 2L, NA, NA, NA, 2L, NA, NA,
NA, NA, 3L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, 4L, NA,
NA, NA, NA, NA, 2L, 3L, 2L, 5L, NA, 2L, NA, 1L, 2L, NA, NA, 2L,
4L, 4L, NA, 3L, 3L, NA, 3L, 3L, NA, NA, NA, 4L, 4L, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
3L, NA, NA, 2L, NA, NA, NA, NA, 3L, 3L, NA, NA, NA, NA, NA, 3L,
NA, 3L, 2L, 2L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 2L, 3L, 3L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 5L, 2L, 2L, 2L, 2L, 3L, 2L, 3L,
2L, 3L, 4L, 4L, 3L, 4L, 3L, 4L, 4L, 3L, 2L, 2L, 2L, 4L, 4L, 2L,
3L, 4L, 3L, 3L, 4L, 4L, 4L, 2L, 2L, 3L, 3L, 2L, 3L, 2L, 4L, 3L,
2L, 3L, 3L, 4L, 4L, 2L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 4L, 3L,
2L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 4L, 2L, 3L,
3L, 3L, 3L, 2L, NA, 3L, NA, 4L, 4L, 5L, 5L, 4L, 4L, 4L, NA, 4L,
5L, 4L, 4L, 3L, 3L, NA, NA, 3L, 4L, 3L, 5L, 2L, 3L, 2L, 3L, 3L,
3L, 3L, 4L, NA, NA, 2L, 2L, NA, 2L, 3L, 3L, 2L, 4L, NA, 1L, NA,
4L, NA, 2L, 2L, 3L, 2L, 4L, NA, 3L, 2L, 3L, 2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, 3L, 4L, 2L, 3L,
2L, 4L, NA, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 2L,
3L, 3L, NA, NA, NA, NA, NA, 3L, NA, 3L, NA, NA, NA, NA, 3L, NA,
4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4L, 3L, NA, 5L, 5L, NA,
3L, NA, NA, NA, NA, NA, NA, NA, 3L, NA, 4L, 3L, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 5L, NA, NA, 4L, 4L, NA,
NA, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
4L, NA, NA, 3L, 3L, NA, 3L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 4L, NA, NA, NA, NA, 4L, 4L, 3L, 3L, NA, NA, NA, NA, 3L, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
3L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 4L, NA, NA, NA, NA, 3L, NA, 3L, NA, 4L, 3L, NA, NA,
NA, 4L, NA, 3L, NA, NA, NA, 4L, 3L, NA, NA, 3L, 3L, NA, NA, NA,
NA, NA, NA, 3L, NA, NA, 4L, NA, 5L, NA, NA, 4L, NA, NA, NA, NA,
NA, 5L, 4L, NA, 5L, NA, 2L, 2L, NA, NA, NA, NA, NA, NA, NA, NA,
5L, NA, NA, NA, NA, NA, NA, 5L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 5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, 4L, 4L, NA, NA,
NA, NA, NA, NA),
.Label = c("18 to less than 25 years old", "25 to less than 35 years old",
"35 to less than 45 years old", "45 to less than 55 years old",
"More than 55 years old"), class = "factor")
I need a function fav_fun which takes these three arguments and aggregates the RawDataSplit and modifies names too. Like for QID_1 the new name after aggregation would be QID_1_45 to less than 55 years old for level 1 of Age variable
function will be called as: fav_fun(RawDataSplit,mastercode_2,Age)

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

ordered logistic regression with a four way interaction in R

I have been trying to perform an ordered logistic regression with a four-way interaction and get the error message:
Error in Design(eval.parent(m)) : interaction term not second or
third order
The design is:
Dependent variable:
Developmental stage - 5 levels numbered 1-5
Independent variables:
Age - 4 different age points for which data was collected;
Ancestry - 2 categories numbered 1 & 2;
Rearing environment - 2 categories numbered 1 & 2;
Current environment - 2 categories numbered 1 & 2
As there is the potential for pseudo-replication the model is run clustered by "Colony".
As such, the code that I have been trying to use is:
library(rms)
Data$Ancestry <- factor(Data$Ancestry)
Data$Rearing <- factor(Data$Rearing)
Data$Queenless <- factor(Data$Queenless)
m <- lrm(Level ~ Age * Ancestry * Rearing * Queenless, x=T, y=T, dat = Data)
robcov(m, cluster = Data$Colony)
I'm assuming that the error message means that lrm doesn't support a 4-way interaction. Is there another way of doing this? I'm having no luck searching for alternate solutions online and I know I had problems trying to cluster using the polr function.
Many thanks for your help.
Here is my data:
structure(list(Bee.Age = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L), Colony = structure(c(1L, 1L, 1L, 1L, 5L, 5L,
5L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L, 4L, 1L, 5L, 1L, 4L, 3L, 1L, 3L, 2L, 4L, 1L,
4L, 1L, 3L, 3L, 4L, 3L, 3L, 4L, 4L, 2L, 3L, 3L, 3L, 1L, 3L,
2L, 1L, 3L, 3L, 4L, 2L, 4L, 4L, 4L, 3L, 3L, 4L, 4L, 5L, 3L,
4L, 5L, 1L, 2L, 5L, 3L, 4L, 5L, 5L, 4L, 3L, 1L, 4L, 3L, 4L,
2L, 5L, 5L, 4L, 3L, 5L, 4L, 1L, 5L, 5L, 5L, 5L, 4L, 5L, 5L,
5L, 5L, 2L, 5L, 4L, 4L, 5L, 3L, 5L, 4L, 4L, 5L, 4L, 5L, 2L,
4L, 5L, 4L, 5L, 4L, 5L, 4L, 5L), X = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA
)), .Names = c("Bee.Age", "Colony", "Ancestry", "Queenless",
"Rearing", "LevelOA", "X"), row.names = c(NA, -320L), class = "data.frame")
This is only a partial answer.
If you need to fit an ordinal model with clustered data, you can use the ordinal package:
Process data:
library(ordinal)
Data <- transform(Data,
Ancestry=factor(Ancestry),
Rearing=factor(Rearing),
Queenless=factor(Queenless),
LevelOA=ordered(LevelOA))
The only special point here is that the response variable needs to be an ordered factor (ordered()).
There are two versions of clustered cumulative-link models in ordinal (you can read about their options in the package's help files):
c1 <- clmm(LevelOA ~ Bee.Age*Ancestry*Rearing*Queenless + (1|Colony),
dat = Data)
c2 <- clmm2(ordered(LevelOA) ~ Bee.Age*Ancestry*Rearing*Queenless,
random = Colony, data= Data, Hess=TRUE)
Both of these give estimates, but not standard deviations - I think the model is too complex and something is going wrong with the SD computation, but it would take more work (maybe a lot more) to figure it out. The estimated among-colony variance is also nearly/effectively zero with both models (std. dev approx. 2.7e-5), which implies that As a preliminary, you can use MASS::polr to fit the model without clustering:
c0 <- polr(LevelOA ~ Bee.Age * Ancestry * Rearing * Queenless,
dat = Data)
because the estimated among-colony variance is zero, this gives the same coefficient estimates as the ordinal functions.

Using geom_text & facet_wrap: Error in unit(x, default.units) : 'x' and 'units' must have length > 0

I am experiencing this weird error..
Some test data:
library(ggplot2)
library(dplyr)
test <- structure(list(group = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L), .Label = c("G", "N", "P"), class = "factor"),
var = structure(c(1L, 1L, 2L, 3L, 4L, 4L, 4L, 4L, 2L, 2L,
2L, 3L, 2L, 2L, 2L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 1L, 1L,
2L, 1L, 1L, 1L, 3L, 3L, 3L, 5L, 3L, 3L, 3L, 4L, 4L, 4L, 1L,
1L, 2L, 2L, 2L, 3L), .Label = c("a", "b", "c", "d", "e"), class = "factor"),
group2 = structure(c(3L, 1L, 3L, 3L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 2L,
3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L,
4L, 1L, 1L, 2L, 3L, 4L), .Label = c("O", "P", "Q", "R"), class = "factor"),
cor = c(0.270075198428616, 0.262097140096646, -0.331312784846655,
-0.343984945812309, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA)), row.names = c(NA, -45L), .Names = c("group", "var",
"group2", "cor"), class = "data.frame")
I'd like to make this plot:
test %>%
ggplot(., aes(x=group2, y=cor)) +
geom_bar(stat="identity", position="dodge", aes(fill=var, group=var)) +
geom_text(aes(label = round(cor,2),
vjust = ifelse(cor >= 0, 0, 1),
group=var),
position = position_dodge(width=1)) +
theme_bw(base_size=18) +
facet_wrap(~group, scales="free_x")
.. which results in: Error in unit(x, default.units) : 'x' and 'units' must have length > 0
However, without geom_text it works:
And also only using rows 1:40 works:
test %>% slice(1:40) %>%
ggplot(., aes(x=group2, y=cor)) +
geom_bar(stat="identity", position="dodge", aes(fill=var, group=var)) +
geom_text(aes(label = round(cor,2),
vjust = ifelse(cor >= 0, 0, 1),
group=var),
position = position_dodge(width=1)) +
theme_bw(base_size=18) +
facet_wrap(~group, scales="free_x")
So up to row 40 there are only two levels for group, and from row 41 on there is a third level. But how can that cause this error? Or is there something else I don't see?
The error also disappears if you erase facet_wrap or if not all of your P-group is NA (for example change the last entry for cor in 0.2). So apparently, if a facet is empty (contains only NA's), geom_text can't handle that.
If you really want to include the empty facet, here's a workaround:
1. replace at least 1 (or all?) NA's in the empty facets with a 0. The bar will have 0 length, so no problem there
2. Now you have a zero label in your graph. By using alpha=ifelse(cor == 0, 0, 1) you'll make that label fully transparent.
Code:
library(ggplot2)
library(dplyr)
test <- structure(list(group = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L), .Label = c("G", "N", "P"), class = "factor"),
var = structure(c(1L, 1L, 2L, 3L, 4L, 4L, 4L, 4L, 2L, 2L,
2L, 3L, 2L, 2L, 2L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 1L, 1L,
2L, 1L, 1L, 1L, 3L, 3L, 3L, 5L, 3L, 3L, 3L, 4L, 4L, 4L, 1L,
1L, 2L, 2L, 2L, 3L), .Label = c("a", "b", "c", "d", "e"), class = "factor"),
group2 = structure(c(3L, 1L, 3L, 3L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 2L,
3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L,
4L, 1L, 1L, 2L, 3L, 4L), .Label = c("O", "P", "Q", "R"), class = "factor"),
cor = c(0.270075198428616, 0.262097140096646, -0.331312784846655,
-0.343984945812309, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
0)), row.names = c(NA, -45L), .Names = c("group", "var",
"group2", "cor"), class = "data.frame")
test %>%
ggplot(., aes(x=group2, y=cor)) +
geom_bar(stat="identity", position="dodge", aes(fill=var, group=var)) +
geom_text(aes(label = round(cor,2),
vjust = ifelse(cor >= 0, 0, 1),
group=var, alpha=ifelse(cor == 0, 0, 1)),
position = position_dodge(width=1)) +
theme_bw(base_size=18) +
facet_wrap(~group, scales="free_x")
If you want to remove the legend of geom_text, simply add show_guide = FALSE.
EDIT: If the transparency doesn't work right, #beetroot came up with this nice alternative: replace alpha=ifelse(cor == 0, 0, 1) with size=ifelse(cor == 0, NA, 3))

Resources