Code that works when run as straight code, but that fails when run as a function

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Code that works when run as straight code, but that fails when run as a function

Sorkin, John
I have code that works perfectly when run as in-line code, but that fails when the code is put into a function with the following message:
Error in contest1D.lmerModLmerTest(model, ll, rhs = rhs, ddf = ddf, confint = confint, :
length(L) == length(model@beta) is not TRUE

Why does moving the code to a function lead to an error? What can I do to avoid the error?
Thank you,
John

In-line code:
fit2aCort <- lmer(SBP~age+jFStage+AHIgt5+jFStage*AHIgt5+(1|patid),data=alldata)

c1 <-c(1,  5.99,  1, 0,   0,  1, 0 )
c2 <-c(1,  5.99,  0, 1,   0,  0, 1 )

model <- fit2aCort
s1a <- contest(model,L=c1,joint=FALSE)
s1as1E <- s1a$Estimate

s2a <- contest(model,L=c2,joint=FALSE)
s2E <- s2a$Estimate

Time <- c(1,2)
SBP  <- c(s1E,s2E)
plot(Time,SBP)
title("Predicted Sys SBP, age 5.99 yr (SS 524 obs with BP 190mmHg removed)")
plotCI(Time,SBP,uiw=1.96*c(s1a$"Std. Error",s2a$"Std. Error"))


Code put into a function that fails:
plotit <- function(c1,c2,model)
{
s1a <- contest(model,L=c1,joint=FALSE)
s1as1E <- s1a$Estimate

s2a <- contest(model,L=c2,joint=FALSE)
s2E <- s2a$Estimate

Time <- c(1,2)
SBP  <- c(s1E,s2E)
plot(Time,SBP)
title("Predicted Sys SBP, age 5.99 yr (SS 524 obs with BP 190mmHg removed)")
plotCI(Time,SBP,uiw=1.96*c(s1a$"Std. Error",s2a$"Std. Error"))
}

fit2aCort <- lmer(SBP~age+jFStage+AHIgt5+jFStage*AHIgt5+(1|patid),data=alldata)

c1 <-c(1,  5.99,  1, 0,   0,  1, 0 )
c2 <-c(1,  5.99,  0, 1,   0,  0, 1 )
plotit(c1,c2,fit2aCort)

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Code that works when run as straight code, but that fails when run as a function

Sarah Goslee
Since this isn't reproducible, my first guess would be that alldata,
which is required for your model, is not visible within the
environment of the function.

Or something similar: that kind of problem is almost always a scoping issue.

Sarah

On Fri, Aug 23, 2019 at 3:46 PM Sorkin, John <[hidden email]> wrote:

>
> I have code that works perfectly when run as in-line code, but that fails when the code is put into a function with the following message:
> Error in contest1D.lmerModLmerTest(model, ll, rhs = rhs, ddf = ddf, confint = confint, :
> length(L) == length(model@beta) is not TRUE
>
> Why does moving the code to a function lead to an error? What can I do to avoid the error?
> Thank you,
> John
>
> In-line code:
> fit2aCort <- lmer(SBP~age+jFStage+AHIgt5+jFStage*AHIgt5+(1|patid),data=alldata)
>
> c1 <-c(1,  5.99,  1, 0,   0,  1, 0 )
> c2 <-c(1,  5.99,  0, 1,   0,  0, 1 )
>
> model <- fit2aCort
> s1a <- contest(model,L=c1,joint=FALSE)
> s1as1E <- s1a$Estimate
>
> s2a <- contest(model,L=c2,joint=FALSE)
> s2E <- s2a$Estimate
>
> Time <- c(1,2)
> SBP  <- c(s1E,s2E)
> plot(Time,SBP)
> title("Predicted Sys SBP, age 5.99 yr (SS 524 obs with BP 190mmHg removed)")
> plotCI(Time,SBP,uiw=1.96*c(s1a$"Std. Error",s2a$"Std. Error"))
>
>
> Code put into a function that fails:
> plotit <- function(c1,c2,model)
> {
> s1a <- contest(model,L=c1,joint=FALSE)
> s1as1E <- s1a$Estimate
>
> s2a <- contest(model,L=c2,joint=FALSE)
> s2E <- s2a$Estimate
>
> Time <- c(1,2)
> SBP  <- c(s1E,s2E)
> plot(Time,SBP)
> title("Predicted Sys SBP, age 5.99 yr (SS 524 obs with BP 190mmHg removed)")
> plotCI(Time,SBP,uiw=1.96*c(s1a$"Std. Error",s2a$"Std. Error"))
> }
>
> fit2aCort <- lmer(SBP~age+jFStage+AHIgt5+jFStage*AHIgt5+(1|patid),data=alldata)
>
> c1 <-c(1,  5.99,  1, 0,   0,  1, 0 )
> c2 <-c(1,  5.99,  0, 1,   0,  0, 1 )
> plotit(c1,c2,fit2aCort)
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.



--
Sarah Goslee (she/her)
http://www.numberwright.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Fwd: Code that works when run as straight code, but that fails when run as a function

Bert Gunter-2
In reply to this post by Sorkin, John
Sorry -- neglected to cc the list.
Bert

---------- Forwarded message ---------
From: Bert Gunter <[hidden email]>
Date: Fri, Aug 23, 2019 at 2:06 PM
Subject: Re: [R] Code that works when run as straight code, but that fails
when run as a function
To: Sorkin, John <[hidden email]>


s1E is not defined in your function in:
SBP  <- c(s1E,s2E)

R will then look for it in the caller (the Global environment presumably),
but maybe it isn't there. Presumably it was when you ran the code
interactively.

Bert Gunter

"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Aug 23, 2019 at 12:46 PM Sorkin, John <[hidden email]>
wrote:

> I have code that works perfectly when run as in-line code, but that fails
> when the code is put into a function with the following message:
> Error in contest1D.lmerModLmerTest(model, ll, rhs = rhs, ddf = ddf,
> confint = confint, :
> length(L) == length(model@beta) is not TRUE
>
> Why does moving the code to a function lead to an error? What can I do to
> avoid the error?
> Thank you,
> John
>
> In-line code:
> fit2aCort <-
> lmer(SBP~age+jFStage+AHIgt5+jFStage*AHIgt5+(1|patid),data=alldata)
>
> c1 <-c(1,  5.99,  1, 0,   0,  1, 0 )
> c2 <-c(1,  5.99,  0, 1,   0,  0, 1 )
>
> model <- fit2aCort
> s1a <- contest(model,L=c1,joint=FALSE)
> s1as1E <- s1a$Estimate
>
> s2a <- contest(model,L=c2,joint=FALSE)
> s2E <- s2a$Estimate
>
> Time <- c(1,2)
> SBP  <- c(s1E,s2E)
> plot(Time,SBP)
> title("Predicted Sys SBP, age 5.99 yr (SS 524 obs with BP 190mmHg
> removed)")
> plotCI(Time,SBP,uiw=1.96*c(s1a$"Std. Error",s2a$"Std. Error"))
>
>
> Code put into a function that fails:
> plotit <- function(c1,c2,model)
> {
> s1a <- contest(model,L=c1,joint=FALSE)
> s1as1E <- s1a$Estimate
>
> s2a <- contest(model,L=c2,joint=FALSE)
> s2E <- s2a$Estimate
>
> Time <- c(1,2)
> SBP  <- c(s1E,s2E)
> plot(Time,SBP)
> title("Predicted Sys SBP, age 5.99 yr (SS 524 obs with BP 190mmHg
> removed)")
> plotCI(Time,SBP,uiw=1.96*c(s1a$"Std. Error",s2a$"Std. Error"))
> }
>
> fit2aCort <-
> lmer(SBP~age+jFStage+AHIgt5+jFStage*AHIgt5+(1|patid),data=alldata)
>
> c1 <-c(1,  5.99,  1, 0,   0,  1, 0 )
> c2 <-c(1,  5.99,  0, 1,   0,  0, 1 )
> plotit(c1,c2,fit2aCort)
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Code that works when run as straight code, but that fails when run as a function

Rui Barradas
In reply to this post by Sorkin, John
Hello,

Inline.

Às 20:45 de 23/08/19, Sorkin, John escreveu:

> I have code that works perfectly when run as in-line code, but that fails when the code is put into a function with the following message:
> Error in contest1D.lmerModLmerTest(model, ll, rhs = rhs, ddf = ddf, confint = confint, :
> length(L) == length(model@beta) is not TRUE
>
> Why does moving the code to a function lead to an error? What can I do to avoid the error?
> Thank you,
> John
>
> In-line code:
> fit2aCort <- lmer(SBP~age+jFStage+AHIgt5+jFStage*AHIgt5+(1|patid),data=alldata)
>
> c1 <-c(1,  5.99,  1, 0,   0,  1, 0 )
> c2 <-c(1,  5.99,  0, 1,   0,  0, 1 )
>
> model <- fit2aCort
> s1a <- contest(model,L=c1,joint=FALSE)
> s1as1E <- s1a$Estimate
>
> s2a <- contest(model,L=c2,joint=FALSE)
> s2E <- s2a$Estimate
>
> Time <- c(1,2)
> SBP  <- c(s1E,s2E)
> plot(Time,SBP)
> title("Predicted Sys SBP, age 5.99 yr (SS 524 obs with BP 190mmHg removed)")
> plotCI(Time,SBP,uiw=1.96*c(s1a$"Std. Error",s2a$"Std. Error"))
>
>
> Code put into a function that fails:
> plotit <- function(c1,c2,model)
> {
> s1a <- contest(model,L=c1,joint=FALSE)
> s1as1E <- s1a$Estimate
>
> s2a <- contest(model,L=c2,joint=FALSE)
> s2E <- s2a$Estimate

Is s1as1E a typo? The way you define and use s2E suggests that it should be

s1E <- s1a$Estimate


Hope this helps,

Rui Barradas

>
> Time <- c(1,2)
> SBP  <- c(s1E,s2E)
> plot(Time,SBP)
> title("Predicted Sys SBP, age 5.99 yr (SS 524 obs with BP 190mmHg removed)")
> plotCI(Time,SBP,uiw=1.96*c(s1a$"Std. Error",s2a$"Std. Error"))
> }
>
> fit2aCort <- lmer(SBP~age+jFStage+AHIgt5+jFStage*AHIgt5+(1|patid),data=alldata)
>
> c1 <-c(1,  5.99,  1, 0,   0,  1, 0 )
> c2 <-c(1,  5.99,  0, 1,   0,  0, 1 )
> plotit(c1,c2,fit2aCort)
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.