Hi Michelle,

This probably wont help all that much as it doesnt calculate anything, but
perhaps its useful as an example of how it works. Im sure theres much
easier ways to do this than the method I have taken, but it works.

So, this code produces some diagnostic plots for lme or lmer models
containing factors. It extracts residuals and plots then against fitted
values and the factors to check for heterogeneity in variance and patterns
in the residuals etc. Maybe its useful to other people too... i know it
saves me 5 minutes for each model!

modcheck <- function(model,norm=TRUE,fac.names){
     # extract residuals
     if(norm==TRUE) res <- residuals(model, type="normalized") else res <-
residuals(model)
     # extract factors
     if(class(model)=="lme") factors <- model$data[,fac.names]
          else factors <- model@frame[2:length(fac.names)]
     if(class(model)=="lme") names(factors) <- fac.names
     # graphics
     par(mfrow=c(2,2), mai=c(0.7,0.7,0.1,0.1))
     for(i in 1:length(names(factors))){
          plot(res ~ fitted(model), col=factors[,i],
main=names(factors)[i], xlab="", ylab="")
          abline(h=0)
          mtext("Fitted", side=1, line=2)
          mtext("Residuals", side=2, line=2)

          }
     for(i in 1:length(names(factors))){
          plot(res ~ factors[,i], xlab="", ylab="")
          abline(h=0)
          mtext(names(factors)[i], side=1, line=2)
          mtext("Residuals", side=2, line=2)

     }
     par(mfrow=c(1,1))
}



hope that helps!

Alan

--------------------------------------------------
Email: aghaynes@gmail.com
Mobile: +41794385586
Skype: aghaynes


On Wed, Dec 7, 2011 at 4:04 AM, Gosse, Michelle <
Michelle.Gosse@foodstandards.gov.au> wrote:

> Hi all,
>
> I've run into something that I assume is an obvious error for the
> initiated, when I am trying to use a function inside nlmer.
>
> I think I am calling my variable names at the wrong point, I thought I
> could use these as input when I get to the nlmer command, but it appears
> not. My (partial) code is:
>
> ll <- function(transformedxy, dummy1, dummy2, dummy3, dummy4, dummy5,
> dummy6, dummy7, dummy8, dummy9, rawresponse) {
>            ll1 <- log(1/sqrt(2*pi*Scale))
>            ll2 <- -(transformedxy - 1 + dummy1 + dummy2 + dummy3 + dummy4
> + dummy5 + dummy6 + dummy7 + dummy8 +
> dummy9)^2/(2*Scale)+(lambda_value-1)*log(rawresponse)
>            ll <- ll1 + ll2
>            return(ll)
>            }
>
> library(lme4)
> mixed_effects <- nlmer(RESPONSE ~ ll(BOXCOXXY, AGEGRP4, AGEGRP5, AGEGRP6,
> AGEGRP7, AGEGRP8, RACE1, RACE3, WEEKEND, SEQ, RESPONSE) ~ SUBJECT,
>                                                data=data0,
>                                                start =
> c(AGEGRP4=Agegrp4Coeff, AGEGRP5=Agegrp5Coeff, AGEGRP6=Agegrp6Coeff
> ,AGEGRP7=Agegrp7Coeff ,AGEGRP8=Agegrp8Coeff,
>
>  RACE1=Race1Coeff , RACE3=Race3Coeff, WEEKEND=WeekendCoeff ,SEQ=SeqCoeff ),
>                                                verbose = TRUE,
>                                                nAGQ = 1,
>                                                doFit = TRUE,
>                                                weights = REPLICATE_VAR,
>                                                contrasts = NULL,
>                                                model = TRUE)
>
> The error I get is:
> Error in nlmer(RESPONSE ~ ll(BOXCOXXY, AGEGRP4, AGEGRP5, AGEGRP6, AGEGRP7,
>  :
>  parameter names must be distinct from names of the variables in data
>
> Variables such as Scale, lambda_value, and Agegrp4Coeff are global, so are
> not being pulled from the data frame.
>
> I thought I used the function to set up how the variables were adjusted,
> off the model statement, but it seems I have this wrong. I looked at the
> SSlogis function to see if I could get some hints, as I am new to function
> programming, and got lost.
>
> I can get a trivial function like this working, but I don't know how to
> extend it to what I am trying to do above:
> addtwo <- function(x) x+2
> values <- rbind(1,2,3,4,5,6,7,8,9)
> addtwo(values)
>
> I've had a look at
> http://stats.lse.ac.uk/penzer/ST419materials/CSchpt3.pdf <
> http://www.google.co.nz/url?sa=t&rct=j&q=writing%20functions%20in%20r&source=web&cd=1&ved=0CB4QFjAA&url=http%3A%2F%2Fstats.lse.ac.uk%2Fpenzer%2FST419materials%2FCSchpt3.pdf&ei=BtbeTpazOpCSiQft1pnJBQ&usg=AFQjCNGwVQDZNJw03c32qZl8WTckhDEHeQ>
>  and
> http://www.stat.cmu.edu/~cshalizi/402/programming/writing-functions.pdfand I'm still quite confused. I think I need to do the call to the data
> frame variables within the function and not within nlmer. Is there a simple
> example to walk me through this that I can download, as I can't follow the
> intricacies of the SSlogis function (e.g. why some names in the code start
> with periods)?
>
> Cheers
> Michelle
>
> UNCLASSIFIED
>
> **********************************************************************
> This email and any files transmitted with it are confide...{{dropped:12}}
>
> _______________________________________________
> R-sig-mixed-models@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
>

	[[alternative HTML version deleted]]


