I have some data I'm trying plot a fit for in ggplot. I'm using nls with a self starting four parameter logisitic regression (SSfpl)
. It can fit the formula fine using nls and it gives me coefficients but when i try to pass it to geom_smooth it gives me a computation failed in 'stat_smooth()': singular matrix 'a' in solve
.
My data looks like so:
dput(SODat)
structure(list(Treatment = structure(1:6, .Label = c(".0032uM",
".016uM", ".08uM", ".4uM", "2uM", "10uM"), class = "factor"),
Peak = c(1028.06247583425, 1466.14079105711, 1007.51610918954,
1171.03478670369, 1204.37248245019, 1042.44546803658), Area1 = c(902.715126326407,
818.917775151066, 1003.80586032706, 1268.82827952821, 1479.87729958222,
1321.77972824089), SS1 = c(131.573938723728, 90.0065951864569,
111.723312483588, 147.027334638628, 195.159502773361, 173.819528362056
), Conc_nm = c(3.2, 16, 80, 400, 2000, 10000), logconc_nm = c(0.505149978319906,
1.20411998265592, 1.90308998699194, 2.60205999132796, 3.30102999566398,
4)), .Names = c("Treatment", "Peak", "Area1", "SS1", "Conc_nm",
"logconc_nm"), class = "data.frame", row.names = 2:7)
My code for it looks as follows.
val=NULL # EC50
vallog=NULL# logEC50
allDR=NULL
vertline=NULL
for (i in 2:4){
currentfit=tryCatch(nls(SODat[,i] ~ SSfpl(logconc_nm,A,B,xmid,scal),dat=testdat),error=function(e) 0)
if(typeof (currentfit)=='list'){
vallog[i]= summary(currentfit)$coefficients[3]
val[i]=10^summary(currentfit)$coefficients[3]}
else{
vallog[i]=0
val[i]=0
}
}
vallog=vallog[-c(1:2)]
val=val[-c(1:2)]
My plotting code looks likes this:
## SS1
DRPlot_SS=ggplot(data = subset(testdatMelt,testdatMelt$variable == 'SS1'),aes(logconc_nm,value))
DRPlot_SS = DRPlot_SS + geom_point()
DRPlot_SS = DRPlot_SS + scale_x_log10(breaks=round(testdat$logconc_nm,2))
if((vertline$vallog[2])!= 0){
DRPlot_SS = DRPlot_SS + geom_smooth(method = 'nls', formula = y ~ SSfpl(x,A,B,xmid,scal),se=FALSE)
DRPlot_SS = DRPlot_SS + geom_vline(data=vertline,color='red',aes(xintercept = vallog[2]),alpha=.5)
DRPlot_SS = DRPlot_SS + geom_text(data=vertline,aes(x=(vallog[2])-.5,y=max(testdatMelt$value[testdatMelt$variable=='SS1']),color='red',
label = paste('EC50 =',round(val[2],2),'nM')))}
if(vertline$vallog[2] == 0){DRPlot_area=DRPlot_area + geom_text(label='No Fit',size=10,col='red',aes(x=median(testdatMelt$logconc_nm),
y=max(testdatMelt$value[testdatMelt$variable=='SS1'])))}
DRPlot_SS = DRPlot_SS + theme(legend.position='none')
DRPlot_SS = DRPlot_SSylim(0,max(testdatMelt$value[testdatMelt$variable=='SS1'])+100)
****Question**** Does anybody know why it would be able to fit in nls and not plot in ggplot? In this example the fit is probably pretty weak. So that's really my only explanation.