Using method="nls" to plot 3 parameter Weibull curve - r

I would like to plot several 3 parameter Weibull curves with the function y ~ mexp(-1(x/b)^c).
In most cases I could define the starting parameters and plot the curves using nls2 and ggplot2. However, I fail to plot one particular curve in ggplot2 (I could fit the curves using nls2 though).
It is important to me to use the the method="nls" command in ggplot (and not loess, the latter tends to overfit the curves in many cases).
Could anyone help me with this? Much appreciated!
#Load packages
library(ggplot2)
library(nlme)
library(nls2)
# library(proto)
#model structure: 3 parameter Weibull
#y ~ m*exp(-1*(x/b)^c)
dip3dta<-structure(list(ploidy = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = c("dip", "trp"), class = "factor"),
geno = structure(c(3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L), .Label = c("dip1", "dip2", "dip3", "dip4", "dip5",
"dip6", "dip7", "dip8", "trp1", "trp2", "trp3", "trp4", "trp5",
"trp6", "trp7", "trp8"), class = "factor"), Photo = c(10.03907124,
16.04016877, 5.799933798, 6.256058037, 1.34916505, 9.609508391,
12.84023945, 8.436093321, 7.732332332, 15.38729611, 2.157795186,
5.93553951, 3.37322132), WBPhoto = c(11.77970983, 13.52705488,
7.585920181, 6.118582453, 2.570461685, 10.80358492, 9.445462376,
5.386306724, 5.840252952, 15.84494637, 3.60398487, 9.32456564,
3.437440219), PDLWP = c(3.1, 2.6, 5.8, 7.7, 19, 3.5, 4.25,
9, 8.16, 2.25, 13.92, 4.33, 14.58), Treatment = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "DC", class = "factor")), row.names = 27:39, class = "data.frame")
#Define the outer boundaries to search for initial values
grddip3 <- data.frame(m=c(0,45),
b=c(0,8),
cc=c(0,0.6))
#Brute-force initial values
fitdip3 <- nls2(Photo ~ m*exp(-1*(PDLWP/b)^cc),
data=dip3dta,
start = grddip3,
algorithm = "brute-force")
fitdip3
finalfitdip3 <- nls(Photo ~ m*exp(-1*(PDLWP/b)^cc),
data=dip3dta,
start=as.list(coef(fitdip3)),
algorithm = "port",
lower = coef(fitdip3)/1000,
control=list(maxiter=1000))
The above code works fine but I can't plot the curve in ggplot using method="nls"
# Plot in ggplot
exampledip3<-ggplot(dip3dta, aes(x=PDLWP,y=Photo)) + geom_point(size=2)+
theme_classic()+
theme(legend.position="none")+
theme(axis.text=element_text(size=18),
axis.title=element_text(size=17,),axis.title.y=element_text(margin=margin(0,20,0,0)))+
stat_smooth(method = "nls", formula = y ~ m*exp(-1*(x/b)^c), size = 0.9, se = FALSE, colour = "black")

Related

error with stat_compare_means and multiple groups

I would like to label my boxplots with pvalues.
Here is my code:
ggplot(df_annot,aes(x=Insect,y=index,fill=Fungi))+geom_boxplot(alpha=0.8)+
geom_point(aes(fill=Fungi),size = 3, shape = 21,position = position_jitterdodge(jitter.width = 0.02,jitter.height = 0))+
facet_wrap(~Location,scales="free" )+
stat_compare_means(aes(group="Insect"))+
guides(fill=guide_legend("M. robertii")) +
scale_x_discrete(labels= c("I+","I-","soil alone"))+
ylab(index_name)+
theme(plot.title = element_text(size = 18, face = "bold"))+
theme(axis.text=element_text(size=14),
axis.title=element_text(size=14)) +
theme(legend.text=element_text(size=14),
legend.title=element_text(size=14)) +
theme(strip.text.x = element_text(size = 14))
Here is the error message that I'm getting:
Warning messages:
1: Unknown or uninitialised column: 'p'.
2: Computation failed in stat_compare_means(): argument "x" is missing, with no default
3: Unknown or uninitialised column: 'p'.
4: Computation failed in stat_compare_means(): argument "x" is missing, with no default
I've tried moving around the aes() from the main ggplot call to the boxplot call. I've tried different inherit.aes in the stat_compare_means().
I've tried first subsetting the root section and making them separately , but the same error.
Any help is appreciated.
thanks
here is my data:
> dput(df_annot)
structure(list(Location = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L), .Label = c("Root", "Rhizospheric Soil"
), class = "factor"), Bean = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Bean", "No bean"), class = "factor"),
Fungi = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L), .Label = c("M+", "M-"), class = "factor"), Insect = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Insect",
"NI"), class = "factor"), index = c(2.90952191983974, 3.19997588762484,
2.96753469534499, 2.93030877512644, 2.72220793003196, 3.09008037591454,
2.63687890737919, 2.73583925812843, 3.06766793411045, 3.26431040286099,
3.03361194852963, 2.9181623054061)), row.names = c("S-B1",
"S-B2", "S-B3", "S-BF-1", "S-BF-2", "S-BF-3", "S-BFi-1", "S-BFi-2",
"S-BFi-3", "S-Bi-1", "S-Bi-2", "S-Bi-3"), class = "data.frame")
A possible and easy fix to your error maybe to use the exact variable name (i.e. remove the double quotes from the variable name) rather that the quoted variable name (i.e. character) in the stat_compare_means (), so the function should look like this:
stat_compare_means(aes(group=Insect))
A working example using ggboxplot() is as follows:
library(ggpubr)
boxplot <- ggboxplot(ToothGrowth, x = "dose", y = "len", add = "jitter",
color = "supp", group="supp", palette = "jco", legend.title="Supplier")
boxplot <- boxplot + stat_compare_means(aes(group=supp), label = "p.signif", method="wilcox.test", hide.ns=T, paired=F)
print(bxp.legend)
There is a warning message for the above example, but I do not know how improve the code to remove the warning message:
`cols` is now required.
Please use `cols = c(p)`

How to add gradient color to a surface3d in R when Z axis is between 0 and 1

I've seen many posts (here, here, here, and here) on how to add color gradient on the Z axis (but none on "z" values that range from 0 to 1). The only thing is that when I do this, I end up with only two colors if my data on the Z axis is between 0 and 1.
Here is an example:
I would like to have a figure where the Z axis is show a red color when it's near 0 and yellow when it's near 1.
The other problem is that I have a bunch of NA's in the Z axis because I'm defining the surface for only the x and y values that correspond to the points. Usually, people use "outer(x,y,f)" to compute the surface. I don't have an equation where I can just plug the numbers.
Is there a way that I can do this?
df3d = structure(list(phi = c(0.714779631270897, 0.687691682891498,
0.596648688803568, 0.573930669753368, 0.742367142156744, 0.647098819439728,
0.695488766544905, 0.728284245613654, 0.688278993976676, 0.692076206940355,
0.721356887106184, 0.551532807978921, 0.54294513452377, 0.529948458419129,
0.583705941140962, 0.556086109758564, 0.721770088612814, 0.711284095827769,
0.573741332655988, 0.527342613188125, 0.762709309318822, 0.740228675759072,
0.539713252759555, 0.696487636519962, 0.709494568163841, 0.537216639879562,
0.551801008711386, 0.545341937291782, 0.584139265723182, 0.64967079561165,
0.562544215947123, 0.716870075612315, 0.523337825235807, 0.588702763971338,
0.744644767844755, 0.551489639273234, 0.617165392352849, 0.556723007149084,
0.66554863194508, 0.570156474465965, 0.59324644850682, 0.552326531317577,
0.607405070778153, 0.765602115588822, 0.532910404322836, 0.749202895901834,
0.638084894011913, 0.594508381800896, 0.745877525852658, 0.742265176757939,
0.55200104972317, 0.598724220429779, 0.704160605412078, 0.709273655686999,
0.57882815350951, 0.80558646355475, 0.739236441867173, 0.556469513099474,
0.560730917777703, 0.715514054617767, 0.562095774851614, 0.540152840905987,
0.561824376055385, 0.595049050758879, 0.544700858333275, 0.54379044778355,
0.735023707587803, 0.75761987117526, 0.529370104304623, 0.756142990929929,
0.580486562475464, 0.555099817471069, 0.537232767721754, 0.68405457472067,
0.572070245916932, 0.73826438688156, 0.776877621879421, 0.5417182204358,
0.757617713719944, 0.536922997394714, 0.695880672257972, 0.570816629701256,
0.551885077056955, 0.697426644089613, 0.700677930911186, 0.722074526398648,
0.547841598427244, 0.744115961419341, 0.568163711481982, 0.631039420851915,
0.52569185852275, 0.655488455712025, 0.715875702650255, 0.562828009151803,
0.565017441865273, 0.554557230119741, 0.641911755728664, 0.549787832704858,
0.551682550480448, 0.522229525069209), sp = structure(c(4L, 4L,
1L, 1L, 2L, 2L, 2L, 2L, 4L, 3L, 2L, 1L, 1L, 1L, 1L, 1L, 4L, 4L,
1L, 1L, 2L, 2L, 1L, 3L, 2L, 1L, 1L, 1L, 1L, 4L, 1L, 4L, 1L, 1L,
2L, 1L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 1L, 2L, 2L,
1L, 1L, 4L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 1L, 2L, 1L,
1L, 4L, 2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 4L, 1L, 1L, 1L, 1L, 1L,
1L, 1L), .Label = c("fortis", "fuliginosa", "magnirostris", "scandens"
), class = "factor"), pc1 = c(0.175880701440334, 0.00718708371839084,
0.141108047117647, -0.0241407292755287, -0.362347619490667, -0.278187055817663,
-0.322472422874688, -0.342113759548994, -0.0480003258625404,
0.471768235224601, -0.324560745197095, 0.0893840127998557, 0.392067958177292,
0.333197422567793, 0.143274241985899, 0.39728316736576, 0.107258309440993,
0.172013966873444, 0.198033002646736, 0.0233433518931576, -0.379151278648175,
-0.360331402784382, 0.0815105012533928, 0.4916774405792, -0.325531606767521,
0.0464793855349116, 0.128993599551295, 0.0393187306328187, 0.116498023384732,
0.0585444918583008, 0.0519773823187942, 0.117485670789894, 0.141592582273004,
0.0866016090395172, -0.353101745830432, 0.0903683502030376, -0.0766571214760896,
0.0189849871337894, 0.0284234379094188, -0.074411018513597, -0.125981989564305,
-0.04066896524291, 0.0513708917900996, -0.384362095569569, 0.133461942504857,
-0.32950028028642, -0.0970510208736005, 0.169708833257483, -0.363153793934809,
-0.358442393985438, 0.0823660510982192, 0.14891498101178, 0.0874718551667044,
-0.286609834093365, 0.247017305539772, -0.42431120384093, -0.323957076921413,
0.120304498088591, 0.0372009683336541, -0.334862217128121, 0.0850391283675992,
0.426550700956589, 0.053540404847934, 0.114569082118706, 0.145035302093536,
0.462956587489796, -0.352558028645024, -0.370105398490897, 0.249974349261361,
-0.374913268845847, 0.209780781689884, 0.313250151589845, 0.46260008422501,
-0.304611304484123, 0.11736172451962, -0.35863773173462, -0.391035427221015,
0.219372693586083, -0.373985839773145, 0.28640321397829, -0.319643095574694,
0.0125879234209831, 0.182454650537706, -0.0307250825972499, -0.32490678343306,
-0.341204851832981, 0.314073748792412, -0.364615463916348, -0.0644240574912661,
-0.267640246495039, 0.10370599000585, -0.288131406123636, 0.0357411052061282,
0.295614964446489, -0.0145385512772513, -0.0451979384514853,
0.190115107687624, 0.159441037623466, 0.0550870424124392, 0.0582226744080579
), pc2 = c(-1.01095247497725, -1.03868939268555, 0.217310975677827,
0.0285247896165632, 0.0526206694724207, 0.029933782968998, 0.0777356682984891,
0.178400497047045, -0.895131692154304, 0.209867904648101, -0.0527418216237663,
0.00827859255924409, 0.112996963663788, -0.0395108234571918,
0.173676295351724, 0.203897905654255, -0.936940800121312, -1.04245666692378,
0.171077913138838, -0.164692367490732, 0.0227473300072106, 0.108660664812142,
-0.0570692402038391, 0.219114322364657, -0.00559526046181254,
-0.0904496365732674, 0.0329879550738144, -0.0513100262471313,
0.157624496486177, -0.430836781866961, 0.0336830138484876, -0.997472053889813,
-0.151743057518861, 0.153748243948929, -0.0290891308461303, 0.00866038555153437,
0.131519041243216, -0.0113322871452352, -0.487378228261218, -0.0178833351102055,
0.0262770136476736, -0.0671756888678338, 0.190653963041647, 0.0874833382301275,
-0.0729306295513451, -0.114781088459982, 0.176113469790657, 0.229289749785351,
0.023115521362388, 0.0124139031005011, 0.00629127323542669, 0.229545586035766,
-0.643425633985522, -0.119025249254049, 0.222273563398108, 0.0949392931025451,
-0.103328613004053, 0.0497069994557915, 0.0169108098226666, 0.0176907608810171,
0.0525638095222423, 0.0991718002465503, 0.031701514651561, 0.194031271868605,
0.00563908525013029, 0.144806228737922, 0.145921630779316, 0.164295633824383,
-0.0579825386055256, 0.104068297238545, 0.204915386707032, 0.153880371324229,
0.0676594796683301, 0.183052585806673, 0.113255499327757, 0.107866805397445,
0.142039558115177, 0.0274014273919194, 0.133609276043029, 0.023767214013592,
0.0322573857202049, 0.0409388634816843, 0.0643799435826686, -0.850272489901295,
0.0430623373727956, 0.0213513249227984, 0.112589167129505, 0.0764778027855769,
-0.0187866951639582, 0.0514999426382286, -0.141852017637047,
0.132798155087113, -0.811488800456735, 0.18297353727076, 0.00129211340539928,
-0.0604306388888919, 0.39467615944551, 0.0406033888777663, -0.0115831761153328,
-0.190035979057187)), .Names = c("phi", "sp", "pc1", "pc2"), row.names = c("phi[1245,12]",
"phi[1058,12]", "phi[594,12]", "phi[1999,12]", "phi[1546,12]",
"phi[353,12]", "phi[312,12]", "phi[21,12]", "phi[1371,12]", "phi[1874,12]",
"phi[384,12]", "phi[124,12]", "phi[2085,12]", "phi[163,12]",
"phi[221,12]", "phi[1321,12]", "phi[1767,12]", "phi[1883,12]",
"phi[490,12]", "phi[225,12]", "phi[1719,12]", "phi[1613,12]",
"phi[268,12]", "phi[2132,12]", "phi[1458,12]", "phi[1173,12]",
"phi[1335,12]", "phi[1357,12]", "phi[388,12]", "phi[985,12]",
"phi[184,12]", "phi[945,12]", "phi[2143,12]", "phi[1273,12]",
"phi[1738,12]", "phi[2081,12]", "phi[822,12]", "phi[1236,12]",
"phi[2044,12]", "phi[2018,12]", "phi[1065,12]", "phi[314,12]",
"phi[943,12]", "phi[514,12]", "phi[448,12]", "phi[1535,12]",
"phi[1798,12]", "phi[960,12]", "phi[22,12]", "phi[128,12]", "phi[190,12]",
"phi[2037,12]", "phi[772,12]", "phi[1553,12]", "phi[417,12]",
"phi[1659,12]", "phi[1529,12]", "phi[1369,12]", "phi[2075,12]",
"phi[1722,12]", "phi[712,12]", "phi[80,12]", "phi[1050,12]",
"phi[1877,12]", "phi[1195,12]", "phi[1138,12]", "phi[1549,12]",
"phi[1886,12]", "phi[90,12]", "phi[1990,12]", "phi[423,12]",
"phi[783,12]", "phi[165,12]", "phi[1975,12]", "phi[951,12]",
"phi[1681,12]", "phi[1647,12]", "phi[1286,12]", "phi[1666,12]",
"phi[1029,12]", "phi[1989,12]", "phi[668,12]", "phi[1859,12]",
"phi[763,12]", "phi[879,12]", "phi[1639,12]", "phi[839,12]",
"phi[1366,12]", "phi[731,12]", "phi[34,12]", "phi[250,12]", "phi[25,12]",
"phi[457,12]", "phi[465,12]", "phi[1010,12]", "phi[1388,12]",
"phi[2055,12]", "phi[917,12]", "phi[188,12]", "phi[130,12]"), class = "data.frame")
library(scatterplot3d) #http://www.statmethods.net/graphs/scatterplot.html
library(rgl)
library(akima)
sp= c("fortis","fuliginosa","magnirostris","scandens")
open3d()
par3d(windowRect = c(10, 10, 600, 600))
plot3d(x = df3d$pc1,
y = df3d$pc2,
z = df3d$phi,
col=c("#FF3030","#9ACD31", "#1D90FF", "#FF8001")[(as.factor(df3d$sp))],
xlab = "PC1",
ylab = "PC2",
zlab = "Fitness",
type = "p",
# size = round(as.numeric(df3d$z.mean)),
main = "yo")
for(j in 1:length(sp)){
df3d.sp = df3d[df3d$sp == sp[j],]
if(nrow(df3d.sp) == 1){next} else{
s = interp(df3d.sp$pc1,
df3d.sp$pc2,
df3d.sp$phi,
duplicate="strip")
z = s$z*2
zlim <- range(df3d$phi)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- heat.colors(zlen) # height color lookup table
col <- colorlut[ z-zlim[1]+1 ] # assign colors to heights for each point
surface3d(s$x,s$y,s$z,color=col, alpha = 0.5)
}
}
The best I could do is something like this:
for(j in 1:length(sp)){
df3d.sp = df3d[df3d$sp == sp[j],]
if(nrow(df3d.sp) == 1){next} else{
s = interp(df3d.sp$pc1,
df3d.sp$pc2,
df3d.sp$phi,
duplicate="strip")
rbPal <- colorRampPalette(c('yellow','red'))
nb.div = 10
data.col =as.data.frame(matrix(as.factor(cut(s$z,breaks = nb.div)),
dim(s$z)[1],dim(s$z)[2]))
col.index=matrix(as.numeric(unlist(data.col)),
dim(s$z)[1],dim(s$z)[2])
Col <- rbPal(nb.div)[col.index]
col= matrix(Col,dim(s$z)[1],dim(s$z)[2])
surface3d(s$x,s$y,s$z,color=col, alpha = 0.5)
}
}
The problem is that the colors are not going from red to yellow (0->1). They are randomly associated:
Also, the colors are not constrained to be between 0 and 1.
How could I do this?
I've just tried a new code and it seems to work, but not with the data that I have.
library(scatterplot3d)
library(rgl)
library(akima)
x = rnorm(100)
y = rnorm(100)
z1 = -x^2-y^2+x^3
expit <- function(x) 1/(1+exp(-x))
logit <- function(x) log(x/(1-x))
z = expit(z1+1)
plot3d(x = x,
y = y,
z = z,
col="red",
xlab = "PC1",
ylab = "PC2",
zlab = "Fitness",
type = "p",
# size = round(as.numeric(df3d$z.mean)),
main = "yo")
s = interp(x,
y,
z,
duplicate="strip")
rbPal <- colorRampPalette(c('red','yellow'))
nb.div = 10
data.col = as.data.frame(matrix(as.factor(cut(s$z, breaks = nb.div)),
dim(s$z)[1],dim(s$z)[2]))
col.index = matrix(as.numeric(unlist(data.col)),
dim(s$z)[1],dim(s$z)[2])
Col <- rbPal(nb.div)[col.index]
col= matrix(Col, dim(s$z)[1], dim(s$z)[2])
surface3d(s$x,s$y,s$z,color=col, alpha = 1)
Why would that one work?
I found the answer. I needed to order the cut values and then remap the values of the range with the colors. Not elegant, but working...
data.col = as.data.frame(matrix(as.factor(cut(s$z,ordered_result = T,
include.lowest = TRUE,
right = TRUE,
breaks = nb.div)),
dim(s$z)[1],
dim(s$z)[2],byrow = FALSE))
range = levels(cut(s$z,ordered_result = T,
include.lowest = TRUE,
right = TRUE,
breaks = nb.div))
library(plyr)
for(i in 1:ncol(data.col)){
data.col[,i] <- mapvalues(data.col[,i],
from=range,
to=rbPal(nb.div),
warn_missing = FALSE)
}

Combine bar plot and stat_smooth() line from different data sets in ggplot2

I'm trying to overlay a stat_smooth() line from one dataset over a bar plot of another. Both csv files draw from the same dataset, but I had to make a new one for the bar plot because I had to add a few columns (including error bars) that wouldn't make sense in the big csv. So, I have code for the bar plot, and code for the line made using stat_smooth, but can't figure out how to combine them. I just want a graph with the line on top of the bars. Here's the code for the bar plot:
`e <- read.csv("Retro Complex.csv", header=T, sep=",")
e <- subset(e, Accuracy != 0)
limits <- aes(ymax = Confidence + SE, ymin = Confidence - SE)
e$Complexity <- factor(e$Complexity)
p <- ggplot(e, aes(e$Complexity, Confidence))
p +
geom_bar(position = "dodge", stat = "identity") +
geom_errorbar(limits, position = "dodge", width = 0.25) +
coord_cartesian(ylim=c(0,1)) +
scale_y_continuous(labels = percent) +
ggtitle("Retro")`
And here's for the line
`ggplot(retroacc, aes(x=Complexity.Sample, y=risk)) +
stat_smooth(aes(x=Complexity.Sample, y=risk), data=retroacc,
method="glm", method.args=list(family="binomial"), se=FALSE) +
ylim(0,1)`
Here's what they both look like:
Stat_smooth() line:
Barplot:
Sample Data
For the bar plot:
structure(list(Complexity = structure(1:5, .Label = c("1", "2",
"3", "4", "5"), class = "factor"), Accuracy = c(1L, 1L, 1L, 1L,
1L), Risk = c(0.69297164, 0.695793434, 0.695891571, 0.746606335,
0.748717949), SE = c(0.003621776, 0.004254081, 0.00669456, 0.008114764,
0.021963804), Proportion = c(0.823475656, 0.809299751, 0.863727821,
0.94724695, 0.882352941), SEAcc = c(0.002716612, 0.003267882,
0.004639995, 0.004059001, 0.015325003)), .Names = c("Complexity",
"Accuracy", "Confidence", "SE", "Proportion", "SEAcc"), row.names = c(1L,
3L, 5L, 7L, 9L), class = "data.frame")
For the line:
structure(list(risk = c(0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), acc = c(0L, 1L, 1L, 1L,
0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
Uniqueness = c(0.405166959, 0.407414244, 0.285123931, 0.248994487,
0.259019778, 0.334552913, 0.300580793, 0.354632526, 0.309841996,
0.331460876, 0.289981111, 0.362405881, 0.37389863, 0.253672193,
0.342903451, 0.294459829, 0.387447291, 0.519657612, 0.278964406
), Average.Similarity = c(0.406700667, 0.409547355, 0.275663862,
0.240909144, 0.251796956, 0.31827466, 0.240574971, 0.349093002,
0.34253811, 0.348084627, 0.290495997, 0.318312198, 0.404143605,
0.290789337, 0.293259599, 0.320214236, 0.382449298, 0.506295194,
0.335167223), Complexity.Sample = c(8521L, 11407L, 3963L,
2536L, 2327L, 3724L, 4005L, 5845L, 5770L, 5246L, 3629L, 3994L,
4285L, 1503L, 8222L, 3683L, 5639L, 10288L, 3076L)), .Names = c("risk",
"acc", "Uniqueness", "Average.Similarity", "Complexity.Sample"
), class = "data.frame", row.names = c(NA, -19L))
So yeah, if any of you guys know how to combine these into one plot please let me know!!

Error in r.squaredGLMM()

I am constructing GLMMs (using glmer() of "lme4" R package) and sometimes I get an error when estimating R2 values (using r.squaredGLMM() from "MuMIn" package).
The model I am trying to fit is simmilar to this one:
library(lme4)
lmA <- glmer(x~y+(1|w)+(1|w/k), data = data1, family = binomial(link="logit"))
Then, to estime R2, I use:
library(MuMIn)
r.squaredGLMM(lmA)
And I get this:
The result is correct only if all data used by the model has not changed since model was fitted. Error in .rsqGLMM(fam = family(x),
varFx = var(fxpred), varRe = varRe, : 'names' attribute [2] must be the same length as the vector [0]
Do you have any idea why this error appears? For instance, If I use only a single random factor (in this case, (1|w)) this error does not appear.
Here is my dataset:
data1 <-
structure(list(w = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L,
1L, 2L, 1L), .Label = c("CA", "CB"), class = "factor"), k = structure(c(4L,
4L, 3L, 3L, 3L, 4L, 1L, 3L, 2L, 3L, 2L), .Label = c("CAF01-CAM01",
"CAM01", "CBF01-CBM01", "CBM01"), class = "factor"), x = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L), y = c(-0.034973549,
0.671720643, 4.557044729, 5.347170897, 2.634240583, -0.555740207,
4.118277809, 2.599825716, 0.95853864, 4.327804344, 0.057331718
)), .Names = c("w", "k", "x", "y"), class = "data.frame", row.names = c(NA,
-11L))
Any thoughts?
This was a bug that has been fixed in version >= 1.15.8 (soon on CRAN, currently on R-Forge).

How to generate facetted ggplot graph where each facet has ordered data?

I want to sort my factors (Condition, Parameter and SubjectID) by MeanWeight and plot MeanWeight against SubjectID such that when faceted by Condition and Parameter, MeanWeight appears in descending order.
Here is my solution, which isn't giving me what I want:
dataSummary <- structure(list(SubjectID = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("s001",
"s002", "s003", "s004"), class = "factor"), Condition = structure(c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), .Label = c("1", "2", "3"), class = "factor"), Parameter = structure(c(1L,
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L,
3L), .Label = c("(Intercept)", "PrevCorr1", "PrevFail1"), class = "factor"),
MeanWeight = c(-0.389685536725783, 0.200987679398502, -0.808114314421089,
-0.10196105040707, 0.0274188815763494, 0.359978984195839,
-0.554583879312783, 0.643791202050396, -0.145042221940287,
-0.0144598460145723, -0.225804028997856, -0.928152539784374,
0.134025102103562, -0.267448309989731, -1.19980109795115,
0.0587152632631923, 0.0050656268880826, -0.156537446664213
)), .Names = c("SubjectID", "Condition", "Parameter", "MeanWeight"
), row.names = c(NA, 18L), class = "data.frame")
## Order by three variables
orderWeights <- order(dataSummary$Condition, dataSummary$Parameter, dataSummary$SubjectID, -dataSummary$MeanWeight)
## Set factors to the new order. I expect this to sort for each facet when plotting, but it doesn't seem to work.
conditionOrder <- dataSummary$Condition[orderWeights]
dataSummary$Condition <- factor(dataSummary$Condition, levels=conditionOrder)
paramOrder <- dataSummary$Parameter[orderWeights]
dataSummary$Parameter <- factor(dataSummary$Parameter, levels=paramOrder)
sbjOrder <- dataSummary$SubjectID[orderWeights]
dataSummary$SubjectID <- factor(dataSummary$SubjectID, levels=sbjOrder)
## Plot
ggplot(dataSummary, aes(x=MeanWeight, y=SubjectID)) +
scale_x_continuous(limits=c(-3, 3)) +
geom_vline(yintercept = 0.0, size = 0.1, colour = "#a9a9a9", linetype = "solid") +
geom_segment(aes(yend=SubjectID), xend=0, colour="grey50") +
geom_point(size=2) +
facet_grid(Parameter~Condition, scales="free_y")
I tried a few other approaches, but they didn't work either:
dataSummary <- dataSummary[order(dataSummary$Condition, dataSummary$Parameter, dataSummary$SubjectID, -dataSummary$MeanWeight),]
or this one
dataSummary <- transform(dataSummary, SubjectID=reorder(Condition, Parameter, SubjectID, MeanWeight))
You can order your data and plot it. However, the labels no longer correspond to Subject ID's, but to the reordered subjects. If that is not what you want, you cannot use faceting but have to plot the parts separately and use e.g.grid.arrangeto combind the different plots.
require(plyr)
## Ordered data
datOrder <- ddply(dataSummary, c("Condition", "Parameter"), function(x){
if (nrow(x)<=1) return(x)
x$MeanWeight <- x$MeanWeight[order(x$MeanWeight)]
x
})
## Plot
ggplot(datOrder, aes(x=MeanWeight, y=SubjectID)) +
scale_x_continuous(limits=c(-3, 3)) +
geom_vline(yintercept = 0.0, size = 0.1, colour = "#a9a9a9", linetype = "solid") +
geom_segment(aes(yend=SubjectID), xend=0, colour="grey50") +
geom_point(size=2) +
facet_grid(Parameter~Condition) +
scale_y_discrete(name="Ordered subjects")

Resources