Hi, I'm trying to fit a model which has a response bias, but also a day to day bias. If I try to simulate the data, I don't get the right values with optim, and also I can't use the function to give a prediction interval. My simulated data are: DF <- as.data.frame(cbind(x=rep(1:10,2),dag=rep(1:2,each=10))) bias <- c(-0.2,0.5) DF$y <- ((DF$x-0.1) * 5)+2 + bias[DF$dag]+rnorm(20,0,sd=0.5) Which I try to fit with: fn <- function(x){ a <- x[1] b <- x[2] c <- x[2] sum((DF$y - (((DF$x-c)*a)+b + x[DF$dag+2]))^2) } optim(c(1,1,1,1,1),fn) But with poor succes. Also, in the real model, I have a response which is y/time (like in lm(y/time~x1 + x2,...) ) , but if I put the time variable at the right side (lm(y~I(x1 + x2)*time) , it gets an coefficient. Is there a way to avoid this? Thanks Bart
Bernardo, thanks for your solution, I was short in time last weeks, but I tried it and it works fine. I'll post it to the list, as it may help other people too. Regards Bart ----- Original Message ----- From: "Bernardo Rangel Tura" <tura at centroin.com.br> To: "Bart Joosen" <Bartjoosen at hotmail.com> Sent: Wednesday, May 30, 2007 8:36 PM Subject: Re: [R] Fitting model with response and day bias> On Wed, 2007-05-30 at 17:20 +0200, Bart Joosen wrote: >> Hello, >> >> you are right about lm(I(y/time)~x1+x2), but what I would like to do is: >> lm(y~(x1+x2)*time), where x1 and x2 should have coefficients, but time >> not. >> Time is just used as a multiplier and should not have a coefficient. >> But I don't know if its possible. >> >> The reason why time is not in my code is that it's simplified to make it >> a >> bit understandable. >> >> >> Thanks anyway >> >> Bart > > Bart, > > If I understand you problem, you have a dataset with 4 variables (y, x1, > x2 and time) and you need fit them model: > > y = A+B*(x1*time)+C*(x2*time)+error > > and use command: lm(y~(x1+x2)*time). > > I think the comand is: lm(y~I(x1*time)+I(x2*time)) > > I sugest try this ... > > I hope this help you ... > > If is solve your problem put solution in list to help other people > -- > Bernardo Rangel Tura, M.D, PhD > National Institute of Cardiolgy > Rio de Janeiro - Brasil > >