Re: [R] piecewise nls?

2010-04-17 Thread Christian Ritz
Hi Derek, have a look at the following made-up example: f1 <- function(x){2*x} f2 <- function(x){-10*x+1} x<-rnorm(10) x (x<0)*f1(x) (x>=0)*f2(x) (x<0)*f1(x) + (x>=0)*f2(x) Therefore I suggest you should specify the model as follows: yourNLSmodel <- nls(Y ~ (X=Z) * g(X,a,d,e), data = myData

Re: [R] Estimate Slope from Boltzmann Model (package: DRC)

2010-01-25 Thread Christian Ritz
Hi Bob, you fitted a log-logistic model specifying the model function LL.4(). If you want to fit a Boltzmann or logistic model then you can use the model function L.4() in place of LL.4(): drc.preTBS.2 <- drm( Response ~ Dose, data = mydata, fct= L.4( names = c( "Slope", "Lower Limit", "Upper

Re: [R] Starting estimates for nls Exponential Fit

2009-12-08 Thread Christian Ritz
Hi Antoon, now that you mention trying out different methods, maybe you should consider fitting a sigmoidal curve to the entire dataset and not only the exponential part (which constitutes a very small dataset) as seems to have been the endeavour that initiated the posting to R-help. One optio

Re: [R] Plot nls line on plot?

2009-10-08 Thread Christian Ritz
Hi Doug, you can add the fitted curve using the following general paradigm: ## Plotting the data plot(p~z) ## Defining grid of z values ## (100 values ensures a smooth curve in your case) zValues <- seq(min(z), max(z), length.out = 100) ## Adding predicted values corresponding to the grid val

Re: [R] drc results differ for different versions

2009-05-22 Thread Christian Ritz
Yes, you're right: taking logarithms is no longer needed! Christian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, min

Re: [R] drc results differ for different versions

2009-05-22 Thread Christian Ritz
Hi Hans, I hope I can resolve your problems below (Marc, thank you very much for cc'ing me on your initial response!). Have a look at the following R lines: ## Fitting the model using drm() (from the latest version) m1<- drm(response ~ dose, data = d, fct = LL.4()) summary(m1) plot(m1) ## Che

Re: [R] fitting assimptotic decaing with - and + on X

2009-04-22 Thread Christian Ritz
Hi Milton, you're right that most of the functions in the package "drc" are suited for sigmoidal/s-shaped curves defined on the positive axis. However, there is one exception: the logistic model: library(drc) sp.coredep.attr.m1 <- drm(preference~dst, data=sp.coredep.attr, fct=L.4()) plot(sp.co

Re: [R] to extract data

2009-04-20 Thread Christian Ritz
Hi, maybe the following line works for you: beechworth.dt.2 <- beechworth.dt[as.numeric(beechworth.dt$Year) %in% 1927:2007, ] (using as.numeric() to make sure that "Year" is numeric, maybe not needed?). Christian __ R-help@r-project.org mailing li

Re: [R] Generate bivariate binomial data

2009-04-17 Thread Christian Ritz
Hi Thierry, I think it should be possible to generate such correlated data using a mixed model approach: 1) Generate pairs of correlated linear predictor values using an ordinary linear mixed model setup (for example using rnorm() repeatedly) 2) Back-transform these values using the inverse l

Re: [R] How to prevent inclusion of intercept in lme with interaction

2009-04-01 Thread Christian Ritz
Hi Dieter, the following model assumes a linear relationship between the response "newbone" and the independent variable "t" with a common intercept equal to 0 and treatment-dependent slopes: grd.lme0 <- lme(newbone~t:treat-1, data=grd, random=~1|subject) summary(grd.lme0) Christian

Re: [R] nls, convergence and starting values

2009-03-30 Thread Christian Ritz
Hi Patrick, there exist specialized functionality in R that offer both automated calculation of starting values and relatively robust optimization, which can be used with success in many common cases of nonlinear regression, also for your data: library(drc) # on CRAN ## Fitting 3-parameter lo

Re: [R] Estimating LC50 from a Weibull distribution

2009-03-22 Thread Christian Ritz
Hi Greg, you can use the extension package 'drc' from CRAN: conc <- c(10.3, 10.8, 11.6, 13.2, 15.8, 20.1) # Exposure concentrations orign <- c(76, 79, 77, 76, 78, 77) # Original number of subjects ndead <- c(16, 22, 40, 69, 78, 77) # Number dead after 96 h d <- data.frame(conc=conc, orign=orign

Re: [R] An error in fitting a non linear regression

2009-02-20 Thread Christian Ritz
Hi Saeed, one approach is to try out several initial value combinations for a and b. It often helps to find initial values of the same order of magnitude and of the same sign as the final estimates. To get such initial values, you could linearize the model: lm(log(q) ~ I(-depth)) and supply

Re: [R] Loess fitting with bisquare

2009-01-22 Thread Christian Ritz
Hi, doing a search in R gives help.search("loess") ?loess Look out for the "family" argument in the help page. Christian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R

Re: [R] Fitting a modified logistic with glm?

2008-11-09 Thread Christian Ritz
Hi Mike, the model you consider is a special case of the four-parameter logistic model where the lower and upper asymptotes are fixed at 0.5 and 1, respectively. Therefore, this (dose-response) model can fitted using the R package 'drc': library(drc) xy.m <- drm(y~x, fct = L.4(fixed=c(NA,0.5,

Re: [R] standard errors for predict.nls?

2008-11-03 Thread Christian Ritz
Dear Christoph, using the package 'alr3' it's not difficult! Have a look at the following example: ## Fitting a Michaelis-Menten model Puromycin.m1<-nls(rate~a*conc/(b+conc), data=Puromycin[1:12,], start=list(a=200, b=1)) library(alr3) ## Predictions (with standard errors) at concentrations 0

Re: [R] Fitting weibull and exponential distributions to left censoring data

2008-11-02 Thread Christian Ritz
Hi Göran, the R package NADA is specifically designed for left-censored data: http://www.statistik.uni-dortmund.de/useR-2008/slides/Helsel+Lee.pdf The function cenreg() in NADA is a front end to survreg(). Christian Göran Broström wrote: > On Fri, Oct 31, 2008 at 2:27 PM, Terry Therneau <[E

Re: [R] Network meta-analysis, varConstPower in nlme

2008-10-15 Thread Christian Ritz
Hi Christian, I believe that the argument "var" has changed name to "weights". The following lines work for me: ... sigma <- rep(sqrt(.5), nrow(lumley1)) # not nrow= lme1 <- lme(Y1 ~ trt.B + trt.C + trt.D + trt.E, random = ~ 1 | trtpair, data=lumley1, weights = varConstPower(form=~sigma, fixed

Re: [R] lmer: random factor nested in a fixed factor

2008-10-06 Thread Christian Ritz
Dear Agnes, I think your model specification should look like this: YourModel1 <- lmerlmer(y ~ poptype*matingtype + (1|poptype:pop) + (1|poptype:fam), data = ...) The "1" in front of "|" refers to models that are random intercepts models as opposed to general random coefficients models in whi

Re: [R] nls convergence trouble

2008-09-04 Thread Christian Ritz
Hi Benoit, another way of making Petr's point is by looking at the profile log likelihood function for b; that is, only estimating the a parameter for a grid of b values: ## Defining mean function for fixed b lgma <- function(b){ function(C0, m, V, a){ (V + b * m * a + C0 * V * b - ((C0 *

Re: [R] psychometric functions

2008-08-21 Thread Christian Ritz
Hi Mario, in many applications there is not much difference between logistic and Gaussian distributions (just as logit and probit models often produce similar fits)... Moreover it's possible to fit sigmoidal curves using models such as the (log-)logistic where the lower and/upper limits are esti

Re: [R] Quick plotmath question

2008-07-12 Thread Christian Ritz
Hi Mike, try: plot(1:10, main=expression(paste(Delta*i, " >> ", 0))) Christian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provid

Re: [R] A problem with anova()

2008-05-09 Thread Christian Ritz
Hi Peter, I think one option for what anova could do in the nonlinear case is to report the analysis of variance (or deviance) table obtained when doing a lack-of-fit test, that is comparing the nonlinear regression model to an appropriate ANOVA model. This is for example the use of anova in t

Re: [R] mean and variance of ratio

2008-02-18 Thread Christian Ritz
Dear Irene, if you have a vector of estimates of x1, x2, y1, y2 and the corresponding estimated variance-covariance matrix (accessible through the method "vcov"), then one possibility is to use the function delta.method() in the package alr3 (on CRAN). This function returns: 1) an estimate of

Re: [R] Generalized nonlinear mixed model function?

2008-02-14 Thread Christian Ritz
Hi Philip, your data are event times because you're monitoring the same trees in each plot over time, the event being death of a tree. Therefore methods from survival analysis are more appropriate. Start out having a look at the package survival, possibly considering a Cox model with adjustment

Re: [R] Error propagation

2008-02-08 Thread Christian Ritz
Hi Steve, I think you need to use apply() as in the following tiny example: x <- data.frame(response = c(-121,-131,-135)) apply(x, 1, function(response){rnorm(10, mean = response, sd = rnorm(10, mean = 9.454398, sd = 1.980136))}) Christian __ R-hel

Re: [R] Ignore error t.test in a loop

2008-02-02 Thread Christian Ritz
Hi, have a look at ?try which leads to using a construct of the following form: myTtest <- try(t.test(...)) if (inherits(myTtest, "try-error")) { cat(myTtest) myTtest <- NA } Christian __ R-help@r-project.org mailing list https://stat.ethz

Re: [R] formula for nls

2008-01-25 Thread Christian Ritz
Hi Jarek, an alternative approach is to provide more precise starting values! It pays off to realise that it's possible to find quite good guesses for some of the parameters in your model function: t ~ tr+(ts-tr)/((1+(a*h)^n)^(1-(1/n))) The parameters tr and ts correspond to the re

Re: [R] how to interpolate a plot with a logistic curve

2007-12-06 Thread Christian Ritz
Dear Simone, you can use the package 'drc' to fit a logistic model, that is a non-linear regression model based on the equation c+(d-c)/(1+exp(b(x-e))), to your data (below named 'simone'): ## Fitting a 4-parameter logistic model (also called Boltzmann model) simone.m1 <- drm(size~x, data=sim

Re: [R] F distribution from lme()?

2007-11-01 Thread Christian Ritz
Dear Andreas, try: anova(incub.lme2, type="marginal") Read more about marginal vs. sequential tests in the help page ?anova.lme. Christian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting g

Re: [R] Some matrix and sandwich questions

2007-10-30 Thread Christian Ritz
Hi again, in the MLE setting the score function (no expectation taken) is the estimating function. So for the OLS situation the basic estimating function is: (in the terminology of Zeileis' paper) psi(x,y,beta) = (y - x^t beta) x^t Christian __ R

Re: [R] Some matrix and sandwich questions

2007-10-30 Thread Christian Ritz
Dear Michael, as to questions 1) and 2): The following article by A. Zeileis provides more details on the package 'sandwich': http://www.jstatsoft.org/v16/i09/ (see also the references). Use 'model.matrix' to construct a design matrix: x <- runif(10,0,10) model.matrix(~x) Christian

Re: [R] help with nls and Hill equation

2007-10-16 Thread Christian Ritz
Hi! I would suggest trying out a few different starting values as a first unsystematic approach. For example changing hill=1 to hill=2 results in convergence: foo.nls<-nls(var~Emax*(Dose^hill)/((EC50^hill)+(Dose^hill)), start=list(Emax=-4,EC50=269,hill=2),trace=T,data=foo) Christian ___

Re: [R] nls fits by groups

2007-09-23 Thread Christian Ritz
Dear Aleksi, there are other approaches you could consider: using nls or gnls (in the package nlme): m1 <- nls(y ~ a[group]*x^b[group], start=list(a=c(1, ..., 1), b=c(1, ..., 1))) or m2 <- gnls(rate ~ a*x^b, params=list(a~group-1, b~group-1), start=list(a=c(1, ..., 1), b=c(1, ..., 1))) In

Re: [R] return(x=x,y=y,prob=prob) hasn't been used in R now?

2007-09-23 Thread Christian Ritz
Hi! Use a list structure for all the components you want to have returned by the function: return(list(x=x, y=y, prob=prob)) Christian __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting gui

[R] plot vs curve

2007-09-12 Thread Christian Ritz
Dear list members, is it intentional that: curve(cos, xlim = c(-5, 5)) plot(cos, xlim = c(-5, 5)) produce different plots? Shouldn't the 'xlim' argument in both cases set the 'from' and 'to' argument if they aren't supplied (at least that's what I understood reading the help page for 'curv