Unknown error messages in R using nls() with SSlogis() and predict() - r

I'm fitting a logistic model (self-starting; SSlogis) to data of multiple populations of birds using nls(). My goal is to fit an expected function to the data (using only part of each dataset) and display a measure of the variance about the expectation on a graph. I then want to fit and plot the observed function (using the entire dataset for each population) to determine if the observed dynamics fell within the variance of the expectation. Here's my code as currently written to accomplish this:
CE.mod = nls(CE.observed ~ SSlogis(t.CattleEgret, Asym, xmid, scal))
with(collapse.data, plot(CE.time, CE.obs))
CE.extrap = predict(CE.mod, data.frame(t.CattleEgret = CE.time))
lines(CE.time, CE.extrap)
CE.se.fit = sqrt(apply(attr(CE.extrap, "gradient"), 1, function(x)
sum(vcov(CE.mod)*outer(x,x))))
matplot(CE.time, CE.extrap+outer(CE.se.fit, qnorm(c(0.5, 0.025, 0.975))),
type = "l", lty = c(1,1,1), ylab = "Abundance (# per party hour)",
xlab = "Time (year)", main = "Cattle Egret Collapse Analysis",
pch = 15, font.lab = 2, font.axis = 2, cex = 4, cex.lab = 1.5,
cex.axis = 2, cex.main = 2, frame.plot = FALSE, lwd = 4, 10)
with(collapse.data, matpoints(CE.time, CE.obs, pch = 15, cex = 3))
lines(CE.time, predict(nls(CE.obs ~ SSlogis(log(CE.time),
Asym, xmid, scal))), lty = 3, lwd = 4)
Where (from the "collapse.data" file):
t.CattleEgret = c(1:20)
CE.time = c(1:45)
CE.obs = c(0.3061324, 0.0000100, 0.2361211, 0.5058240, 2.0685032, 2.1944544,
4.2689494, 4.9508297, 3.1334720, 3.6570752, 5.6753381, 10.9133183,
5.4518257, 20.4166979, 15.9741054, 19.0970426, 13.7559959, 14.1358153,
15.9986416, 29.6762828, 10.3760667, 8.4284488, 6.1060359, 3.7099982,
3.3584060, 2.5981386, 2.5697082, 2.8091952, 5.5487979, 1.6505442,
2.2696972, 2.1835692, 3.6747876, 4.8307886, 3.5019731, 2.8397137,
1.8605288, 11.1848738, 2.6268683, 4.1215127, 2.3996210, 2.6569938,
2.1987387, 3.0267252, 2.4420927)
CE.observed = c(0.3061324, 0.0000100, 0.2361211, 0.5058240, 2.0685032, 2.1944544,
4.2689494, 4.9508297, 3.1334720, 3.6570752, 5.6753381, 10.9133183,
5.4518257, 20.4166979, 15.9741054, 19.0970426, 13.7559959, 14.1358153,
15.9986416, 29.6762828)
That code works fine and produces a figure like this:
If, however, I remove the "log()" from the final line of the code so as to write this:
lines(CE.time, predict(nls(CE.obs ~ SSlogis(CE.time,
Asym, xmid, scal))), lty = 3, lwd = 4),
The line will not plot and I receive this error:
Error in nls(y ~ 1/(1 + exp((xmid - x)/scal)), data = xy, start = list(xmid =
aux[1L], : step factor 0.000488281 reduced below 'minFactor' of 0.000976562
which I cannot alter, even if I play around with the nls.controls and change the 'minFactor' value. I also get this error message following the initial line defining the mod (the ##.mod portion) for some populations.
Also, for some populations I receive an error message following the final line of code that reports this:
Error in qr.solve(QR.B, cc) : singular matrix 'a' in solve
I can think of no rationalization for natural log-transforming the data, and I'm left to assume that I have simply altered the data (in this case arbitrarily logged it) in such a way to allow the predict() and SSlogis() functions to function properly, but I don't know why. I haven't been able to find any suitable answers in any forums to such an issue. Any help would be greatly appreciated.
*Update: I've attempted to implement the nlsLM function as recommended by Roland (below). That does indeed clean up the portion of code with the confusing log() use:
lines(CE.time, predict(nlsLM(CE.obs ~ Asym/(1 + exp((xmid - CE.time)/scal)), start
= list(Asym = max(CE.obs), xmid = popsizetime[1], scal = 1), control =
nls.lm.control(maxiter = 1000))
However, for other populations I run into the same error message as above at the initial model specification:
ChMa.mod = nls(ChMa.observed ~ SSlogis(t.ChestnutMannikin, Asym, xmid, scal))
Error in nls(y ~ 1/(1 + exp((xmid - x)/scal)), data = xy, start = list(xmid =
aux[1L], : step factor 0.000488281 reduced below 'minFactor' of 0.000976562
Switched to:
ChMa.mod = nlsLM(ChMa.observed ~ Asym/(1 + exp((xmid - t.ChestnutMannikin)/
scal)), start = list(Asym = max(ChMa.obs), xmid = popsizetime[2],
scal = 1), control = nls.lm.control(maxiter = 1000))
Where
ChMa.observed = c(4.02785074, 0.33847154, 0.99029776, 2.86516540, 0.59588068,
0.01334333, 2.07693362, 0.62485994, 3.48979515, 3.67785202, 20.84180181)
t.ChestnutMannikin = c(1:11)
popsizetime[2] = 11
While this switch does avoid the error message, nlsLM evaluates the function but does not evaluate the gradient. Without the evaluation of the gradient I cannot use the se.fit code and therefore cannot obtain an estimate of the variance for plotting.

I've found the answer to my problems: I need to add a component of my model that generates a gradient for the function I'm regressing with nlsLM.
log.model = function(t.RedventedBulbul, Asym, xmid, scal) {
numericDeriv(quote(Asym/(1 + exp((xmid - t.RedventedBulbul)/scal))),
c("Asym", "xmid", "scal"), parent.frame())
}

Related

r gbm package summary function performing erroring

I am trying to use the gbm package within r, and am having problems with the summary function. Hoping that someone can help out. My code is as follows:
library(ISLR)
Caravan$Purchase <- ifelse(Caravan$Purchase == "Yes", 1, 0)
train_index <- 1:1000
train <- Caravan[train_index, ]
test <- Caravan[-train_index, ]
library(gbm)
set.seed(1234)
boost <- gbm(Purchase ~ ., data = train, n.trees=400, shrinkage =0.01,
distribution = "bernoulli")
summary(boost)
I get the following error message with traceback:
Error in plot.window(xlim, ylim, log = log, ...) : need finite 'xlim' values
5.
plot.window(xlim, ylim, log = log, ...)
4.
barplot.default(rel.inf[i[cBars:1]], horiz = TRUE, col = rainbow(cBars, start = 3/6, end = 4/6), names = object$var.names[i[cBars:1]], xlab = "Relative influence", ...)
3.
barplot(rel.inf[i[cBars:1]], horiz = TRUE, col = rainbow(cBars, start = 3/6, end = 4/6), names = object$var.names[i[cBars:1]], xlab = "Relative influence", ...)
2.
summary.gbm(boost)
1.
summary(boost)
I have tried using the workaround here: http://www.samuelbosch.com/2015/09/workaround-ntrees-is-missing-in-r.html to no avail.
Any suggestions?
EDIT 1: Confirmed error occurs in R 3.5.0, but does not occur in 3.4.4.

Unable to generate plot from dccfit (R)

R version: 3.4.2
I'm using rugarch and mgarch to spec and fit model with DCC to my data. The model is generated successfully, however I'm unable to generate the plots. Here's a snippet of my code:
library(rugarch)
library(rmgarch)
da=read.table("d-msft3dx0113.txt",header=T)
MSFT.ret = da[,3]
GSPC.ret = da[,6]
MSFT.GSPC.ret = cbind(MSFT.ret,GSPC.ret)
garch11.spec = ugarchspec(mean.model = list(armaOrder = c(0,0)),
variance.model = list(garchOrder = c(1,1),
model = "sGARCH"),
distribution.model = "norm")
dcc.garch11.spec = dccspec(uspec = multispec( replicate(2, garch11.spec) ),
dccOrder = c(1,1),
distribution = "mvnorm")
dcc.fit = dccfit(dcc.garch11.spec, data = MSFT.GSPC.ret)
dcc.fcst = dccforecast(dcc.fit, n.ahead=100)
plot(dcc.fcst)
When I call for plot, I get this error:
plot(dcc.fcst)
Make a plot selection (or 0 to exit):
Conditional Mean Forecast (vs realized returns)
Conditional Sigma Forecast (vs realized |returns|)
Conditional Covariance Forecast
Conditional Correlation Forecast
EW Portfolio Plot with forecast conditional density VaR limits
Selection: 1
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
I then give it a new plot area:
plot.new()
plot(dcc.fcst)
Which gives me this unhelpful plot:
Selection1Plot
I have the same question, too. I don't know why plot(dcc.fic) cannot work. So I do it manually to extract the correlation and covariance. rcov and rcor are two functions to extract what we need.
plot(rcov(dcc.fit)[1,2,], type = "l", col = "blue",
main = "Conditional Covariance", xlab = "Time",
ylab = "Covariance")
plot(rcor(dcc.fit)[1,2,], type = "l", col = "purple",
main = "Conditional Correlation", xlab = "Time",
ylab = "Correlation")

Export Raster from R-INLA

so I am in dire need of help. I have finally managed to construct my R-INLA model and get it to graph as needed. via the code below:
First I create the stacks (note this is the very end of my INLA process, the mesh etc has already been done)
stk.abdu = inla.stack(data = list(y = 1, e = 0), A = list(abdu.mat, 1),tag = 'abdu', effects = list(list(i = 1:sc.mesh.5$n), data.frame(Intercept = 1,dwater=winter.abdu$dwater,elev=winter.abdu$elev,forest=winter.abdu$forest,developed=winter.abdu$developed,openwater=winter.abdu$OpenWater,barren=winter.abdu$barren,shrubland=winter.abdu$shrubland,herb=winter.abdu$herb,planted=winter.abdu$planted,wetland=winter.abdu$wetland,dist=winter.abdu$dwater)))
stk.quad = inla.stack(data = list(y = 0, e = 0.1), A = list(quad.mat, 1),tag = 'quad', effects = list(list(i = 1:sc.mesh.5$n), data.frame(Intercept = 1,dwater=dummy$dwater,elev=dummy$elev,forest=dummy$forest,developed=dummy$developed,openwater=dummy$openwater,barren=dummy$barren,shrubland=dummy$shrubland,herb=dummy$herb,planted=dummy$planted,wetland=dummy$wetland,dist=dummy$dwater)))
stk.prd<-inla.stack(data = list(y = NA), A = list(Aprd, 1),tag = 'prd', effects = list(list(i = 1:sc.mesh.5$n), data.frame(Intercept = 1,dwater=prddf2$dwater,elev=prddf2$elev,forest=prddf2$forest,developed=prddf2$developed,openwater=prddf2$openwater,barren=prddf2$barren,shrubland=prddf2$shrubland,herb=prddf2$herb,planted=prddf2$planted,wetland=prddf2$wetland,dist=prddf2$dwater)))
stk.all.prd = inla.stack(stk.abdu,stk.quad,stk.prd)
Next I fit my model
ft.inla.prd<-inla(y ~ 0 + Intercept + elev + dwater + forest+ developed + f(inla.group(dist,n=50,method="quantile"),model="rw1",scale.model=TRUE)+f(i,model=sc.spde),family="binomial",data=inla.stack.data(stk.all.prd),control.predictor = list(A = inla.stack.A(stk.all.prd),compute=TRUE),E=inla.stack.data(stk.all.prd)$e,control.compute=list(dic = TRUE),control.fixed=list(expand.factor.strategy="INLA"))
Then I change the predicted values from logit to probabilities
ft.inla.prd$newfield <- exp(ft.inla.prd$summary.random$i$mean)/(1 + exp(ft.inla.prd$summary.random$i$mean))
And finally I use inla.mesh.project and levelplot to create my image
xmean <- inla.mesh.project(projgrid,ft.inla.prd$newfield)
levelplot(xmean, col.regions=topo.colors(99), main='Probability of Presence',xlab='', ylab='', scales=list(draw=FALSE))
So my problem is that I now want to export this data (what is projected as the graph) as a raster so that I can work with it in ArcGIS. However, I have not been able to find a way to do so.
Any input is greatly appreciated

How to fit a model line in a graph

I tried to create a fitted line through the graph, but it doesn't show at all, it just shows the graph itself. What am I doing wrong here? Check my code below.
cor.merged <- cor.merged[order(cor.merged$No.florets), ]
plot(vis.rate ~ No.florets, data = cor.merged[cor.merged$line == 'M',],
main = "Nectar production vs visitation rate",
xlab = "No.florets", ylab = "Visitation rate",
pch = 19)
m1 <- lm(cor.merged$vis.rate[cor.merged$line == 'M'] ~
poly(cor.merged$No.florets[cor.merged$line == 'M'], 2, raw = T))
summary(m1)
lines(cor.merged$No.florets[cor.merged$line == 'M'] ~
predict(m1),
col = 'red', lwd = 3, type = 'c')

different behavior on same code in different installation of r

When I run following code:
library(turboEM)
library(SparseM)
library(quantreg)
library(numDeriv)
library(foreach)
library(parallel)
libraary(iterators)
library(doParallel)
library(logbin)
library(glm2)
#-------------------------------------------------------------------------
data(heart)
head(heart)
start.p <- sum(data1$deaths) / sum(data1$patients)
fit.glm <- glm(cbind(deaths, patients-deaths) ~ factor(age) + factor(severity) +
factor(onset) + factor(region), family = binomial(log),
start = c(log(start.p), -rep(1e-4, 8)), data = data1,trace = TRUE, maxit = 100)
fit.logbin <- logbin(formula(fit.glm), trace = 1,data=data1)
summary(fit.logbin)
# Speed up convergence by using single EM algorithm
fit.logbin.em <- update(fit.logbin, method = "em")
summary(fit.logbin.em)
# Speed up convergence by using acceleration methods
fit.logbin.acc <- update(fit.logbin, accelerate = "squarem")
summary(fit.logbin.acc)
fit.logbin.em.acc <- update(fit.logbin.em, accelerate = "squarem")
summary(fit.logbin.em.acc)
sink("C:\\Users\\ak.biglarian\\Desktop\\MrsZooghi\\Out.logbin.txt")
summary(fit.logbin.em)
sink()
#-------------------------------------------------------------------------
logbin(formula = formula(fit.glm), data = heart, method = "em",
trace = 1)
on different installations of R-project, I get different results on same code!
In one installation I get the code running pretty good but in another installation I get following errors:
> fit.logbin <- logbin(formula(fit.glm), trace = 1,data=data1)
Error in logbin(formula(fit.glm), trace = 1, data = data1) :
unused argument (data = data1)
or
> logbin(formula = formula(fit.glm), data = heart, method = "em",
+ trace = 1)
Error in logbin(formula = formula(fit.glm), data = heart, method = "em", :
unused arguments (formula = formula(fit.glm), data = heart, method = "em")
What is the reason? And how shall I fix this?

Resources