Re: [R] back tick names with predict function

2023-12-03 Thread Robert Baer



On 12/1/2023 11:47 AM, peter dalgaard wrote:

Also, and possibly more constructively, when you get an error like
  

CI.c = predict(mod2, data.frame( `plant-density` = x), interval = 'c')  # fail

Error in eval(predvars, data, env) : object 'plant-density' not found

you should check your assumptions. Does "newdata" actually contain a columnn called 
"plant-density":


Great advice/strategy. Thanks!



head(data.frame( `plant-density` = x))

   plant.density
1  65.0
2  65.11912
3  65.23824
4  65.35736
5  65.47648
6  65.59560
I.e., it doesn't. So check help for data.frame and looking for something with 
names.


On 1 Dec 2023, at 01:47 , Bert Gunter  wrote:

"Thank you Rui.  I didn't know about the check.names = FALSE argument.

Another good reminder to always read help, but I'm not sure I understood
what help to read in this case"

?data.frame , of course, which says:

"check.names

logical. If TRUE then the names of the variables in the data frame are
checked to ensure that they are syntactically valid variable names and
are not duplicated. If necessary they are adjusted (by make.names) so
that they are. "

-- Bert

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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, minimal, self-contained, reproducible code.


__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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, minimal, self-contained, reproducible code.


Re: [R] Try reproduce glmm by hand

2023-12-03 Thread Viechtbauer, Wolfgang (NP)
Dear Marc,

Plugging the BLUPs into the likelihood isn't going to give you the ll of the 
model. You need to integrate over the random effect.

intfun <- function(nu, xi, mi, pred, theta)
   dbinom(xi, xi+mi, plogis(nu)) * dnorm(nu, pred, theta)

lli <- rep(NA, nrow(df))

for (i in 1:nrow(df)) {
   lli[i] <- log(integrate(intfun, xi=df[i,1], mi=df[i,2], pred=fixep[1] + 
fixep[2]*x[i], theta=par$theta, lower=-Inf, upper=Inf)$value)
}

-sum(lli)

Best,
Wolfgang

> -Original Message-
> From: R-help  On Behalf Of Marc Girondot via R-
> help
> Sent: Saturday, December 2, 2023 17:36
> To: Marc Girondot via R-help 
> Subject: [R] Try reproduce glmm by hand
>
> Dear all,
>
> In order to be sure I understand glmm correctly, I try to reproduce by
> hand a simple result. Here is a reproducible code. The questions are in
> _
>
> Of course I have tried to find the solution using internet but I was not
> able to find a solution. I have also tried to follow glmer but it is
> very complicated code!
>
> Thanks for any help.
>
> Marc
>
> # Generate set of df with nb successes and failures
> # and ID being A, B or C (the random effect)
> # and x being the fixed effect
> set.seed(1)
> df <- rbind(matrix(data = c(sample(x=5:30, size=40, replace = TRUE),
> rep(10, 40)), ncol=2),
>  matrix(data = c(sample(x=10:30, size=40, replace = TRUE),
> rep(10, 40)), ncol=2),
>  matrix(data = c(sample(x=20:30, size=40, replace = TRUE),
> rep(10, 40)), ncol=2))
> ID <- as.factor(c(rep("A", 40), rep("B", 40), rep("C", 40)))
> x <- c(runif(40, min=10, max=30), runif(40, min=20, max=30), runif(40,
> min=40, max=60))
> x <- (x-min(x))/(max(x)-min(x))
>
> # In g0, I have the results of the glmm
> library(lme4)
> g0 <- glmer(formula = df ~ x + (1 | ID), family =
> binomial(link="logit"), nAGQ=1)
> -logLik(g0) # 'log Lik.' 268.0188 (df=3)
> # I get the fitted parameters
> fixep <- fixef(g0)
> par <- getME(g0, c("theta","beta"))
> # ___
> # Question 1: how theta is converted into the specific effect on
> (intercept) for the random effect ?
> # Then how a theta parameter is converted into intercepts?
> # ___
> intercepts <- ranef(g0)$ID
>
> # This part is ok, the predict is correct
> pfit <- 1-c(1/(1+exp(fixep["(Intercept)"]+intercepts["A",
> 1]+x[ID=="A"]*fixep["x"])),
>1/(1+exp(fixep["(Intercept)"]+intercepts["B",
> 1]+x[ID=="B"]*fixep["x"])),
>1/(1+exp(fixep["(Intercept)"]+intercepts["C", 1]+x[ID=="C"]*fixep["x"])))
>
> predict(g0, type = "response")
>
> # ___
> # Why I obtain 266.4874 and not 268.0188 as in -logLik(g0)?
> # ___
> -sum(dbinom(x=df[, 1], size=df[, 1]+df[, 2], prob=pfit, log=TRUE)) #
> 266.4874
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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, minimal, self-contained, reproducible code.