[R] Non-linear modelling with several variables including a categorical variable

Prof J C Nash (U30A) nashjc at uottawa.ca
Wed Jul 3 15:51:52 CEST 2013

If preytype is an independent variable, then models based on it should 
be OK. If preytype comes into the parameters you are trying to estimate, 
then the easiest way is often to generate all the possible combinations 
(integers --> fairly modest number of these) and run all the least 
squares minimizations. Crude but effective. nlxb from nlmrt or nlsLM 
from minpack.lm may be more robust in doing this, but less efficient if 
nls works OK.


On 13-07-03 06:00 AM, r-help-request at r-project.org wrote:
> Message: 10
> Date: Tue, 2 Jul 2013 19:01:55 +0700
> From: Robbie Weterings<robbie.weterings at gmail.com>
> To:r-help at r-project.org
> Subject: [R] Non-linear modelling with several variables including a
> 	categorical variable
> Message-ID:
> 	<CAFe5dHZRM+BpG1v77EzHun+tacV64J_9pnSFGh_xne5CSZ9qdQ at mail.gmail.com>
> Content-Type: text/plain
> Hello everyone,
> I am trying to model some data regarding a predator prey interaction
> experiment (n=26). Predation rate is my response variable and I have 4
> explanatory variables: predator density (1,2,3,4 5), predator size, prey
> density (5,10,15,20,25,30) and prey type (3 categories). I started with
> several linear models (glm) and found (as expected) that prey and predator
> density were non-linear related to predation rates. If I use a log
> transformation on these variables I get really nice curves and an adjusted
> R2 of 0.82, but it is not really the right approach for modelling
> non-linear relationships. Therefore I switched to non-linear least square
> regression (nls). I have several predator-prey models based on existing
> ecological literature e.g.:
> model1 <- nls(rates ~ (a * prey)/(1 + b * prey), start = list(a = 0.27,b =
> 0.13), trace = TRUE) ### Holling's type II functional response
> model2 <- nls(rates ~ (a*prey)/(1+ (b * prey) + c * (pred -1 )), start =
> list(a=0.22451, b=-0.18938, c=1.06941), trace=TRUE, subset=I1) ###
> Beddington-**DeAngelis functional response
> These models work perfectly, but now I want to add prey type as well. In
> the linear models prey type was the most important variable so I don't want
> to leave it out. I understand that you can't add categorical variables in
> nls, so I thought I try a generalized additive model (gam).
> The problem with the gam models is that the smoothers (both spline and
> loess) don't work on both variables because there are only a very
> restricted number of values for prey density and predator density. I can
> manage to get a model with a single variable smoothed using loess. But for
> two variables it is simply not working. The spline function does not work
> at all because I have so few values (5) for my variables (see model 4).
> model3 <- gam(rates~ lo(pred, span=0.9)+prey) ## this one is actually
> working but does not include a smoother for prey.
> model4 <- gam(rates~ s(pred)+prey) ## this one gives problems:
> *A term has fewer unique covariate combinations than specified maximum
> degrees of freedom*
> My question is: are there any other possibilities to model data with 2
> non-linear related variables in which I can also include a categorical
> variable. I would prefer to use nls (model2) with for example different
> intercepts for each category but I'm not sure how to get this sorted, if it
> is possible at all. The dataset is too small to split it up into the three
> categories, moreover, one of the categories only contains 5 data points.
> Any help would be really appreciated.
> With kind regards,
> -- Robbie Weterings *Project Manager Cat Drop Thailand ** Tel:
> +66(0)890176087 * 65/13 Mooban Chakangrao, Naimuang Muang Kamphaeng Phet
> 62000, Thailand àÅ¢·Õè 65/13 Á.ªÒ¡Ñ§ÃÒÇ ¶¹¹ ÃÒª´íÒà¹Ô¹2 ã¹àÁ×ͧ ÍíÒàÀÍ/
> ࢵ àÁ×ͧ¡íÒàྦྷྪà ¨Ñ§ËÇÑ´ ¡íÒàྦྷྪà 62000
> <http://www.catdropfoundation.org>
> <http://www.catdropfoundation.org/facebook/Facebook.html>
> *www.catdropfoundation.org* <http://www.catdropfoundation.org/>
> *www.facebook.com/catdropfoundation*<http://www.facebook.com/catdropfoundation>
> *Boorn 45, 9204 AZ, Drachten, The Netherlands* [[alternative HTML
> version deleted]]

More information about the R-help mailing list