How to use the `plot` and `abline` inside a `cat` which is inside a function?











up vote
0
down vote

favorite












I'm having problems with the following code, I do not know how to introduce this instruction below inside a cat instruction which is in the function.



df <- data.frame(x,y)
plot(y,x)
abline(lm(y ~ x))


R marks an error saying that a comma is needed between plot(x,y) and abline(lm(y~ x)) but when adding it, the display is



Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
display list redraw incomplete
2: In doTryCatch(return(expr), name, parentenv, handler) :
invalid graphics state
3: In doTryCatch(return(expr), name, parentenv, handler) :
invalid graphics state


What can I do to fix the problem?



Please someone help me, thank you in advance.



Summary<-function(x,y,print=TRUE)
{
p<-2
n<-length(x)


x<-matrix(c(rep(1,n),x),n,p)
bg<-solve(t(x)%*%x,t(x)%*%y)
invx<-solve(t(x)%*%x)
xty<-t(x)%*%y
e<-y-x%*%bg
SCT<-sum(y^2)-n*(mean(y)^2)
SCE<-sum(e*e)
SCRegression<-SCT-SCE

sigma<-SCE/(n-p)

varbeta<-sigma*invx


seb0 <- sqrt(varbeta[1,1])
seb1 <- sqrt(varbeta[2,2])

alfa <- 0.05

valuet <- qt(1-alfa/2,n-p)

valuechi1 <- qchisq(1-alfa/2,n-p)
valuechi2 <- qchisq(alfa/2,n-p)



bg[1]-valuet*seb0
bg[1]+valuet*seb0



bg[2]-valuet*seb1
bg[2]+valuet*seb1


tvalueb1 <- bg[2]/seb1
tvalueb1
tvalueb0 <- bg[1]/seb0
tvalueb0


valuepb0 <- 2*pt(1-tvalueb0,n-p)
valuepb0

valuepb1 <- 2*pt(tvalueb1,n-p)
valuepb1


SCE/valuechi1
SCE/valuechi2


x0 <- mean(x)
yhat <- bg[1]+bg[2]*x0
x0

varmu <- varbeta[1,1]+x0^2*varbeta[2,2]+2*x0*varbeta[1,2]


semu <- sqrt(varmu)
semu

yhat - valuet*semu
yhat + valuet*semu

Rsq <- SCRegression/SCT
Rsq

yhatt <- bg[1]+bg[2]*x
yhatt

ee <- y-yhatt

df <- data.frame(x,y)

results <- list(TSS=SCT, ESS=SCE, p=p, x=x, y=y)

if (SCE == 0) warning("Error square sum is zero", call.=FALSE)

if (print) {
cat("Results for the variables", "nt",
deparse(match.call()$x), " and ", deparse(match.call()$y),
"nn", sep="")
cat("The total square sum is: ", SCT, "nn",
"The error square sum is: ", SCE, "nn",
"The errors graph to determine homogeneity or heterogeneity is: ",plot(x,ee) , "nn",
"The linear model plot: ",plot(y,x) abline(lm(y ~ x)) , "nn",
sep="")
invisible(results)
} else {
results
}
}









share|improve this question







New contributor




Isa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
    – Tung
    Nov 17 at 22:13










  • @Tung ok let me read about it
    – Isa
    Nov 17 at 22:16










  • The cat function returns NULL and only produce character output at the console or other file device (but not graphics devices). Base graphics work via side effect and so most of them also return NULL. You should not be trying to mix them. If you are trying to produce a mixed output then learn to use Sweave or Rmarkdown.
    – 42-
    Nov 17 at 22:38












  • @42- if you write a # before the line "The linear model plot: ",plot(y,x) abline(lm(y ~ x)) , "nn", then the code works fine, no problem at all. So cat actually works, produce the errors graph.
    – Isa
    Nov 17 at 22:44












  • @Tung I'm not testing the code with sample of data, I just used a random vector x<-y<-1:10. (Because first I'd like to be sure the code works properly and then to substitute the original data )
    – Isa
    Nov 17 at 22:49

















up vote
0
down vote

favorite












I'm having problems with the following code, I do not know how to introduce this instruction below inside a cat instruction which is in the function.



df <- data.frame(x,y)
plot(y,x)
abline(lm(y ~ x))


R marks an error saying that a comma is needed between plot(x,y) and abline(lm(y~ x)) but when adding it, the display is



Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
display list redraw incomplete
2: In doTryCatch(return(expr), name, parentenv, handler) :
invalid graphics state
3: In doTryCatch(return(expr), name, parentenv, handler) :
invalid graphics state


What can I do to fix the problem?



Please someone help me, thank you in advance.



Summary<-function(x,y,print=TRUE)
{
p<-2
n<-length(x)


x<-matrix(c(rep(1,n),x),n,p)
bg<-solve(t(x)%*%x,t(x)%*%y)
invx<-solve(t(x)%*%x)
xty<-t(x)%*%y
e<-y-x%*%bg
SCT<-sum(y^2)-n*(mean(y)^2)
SCE<-sum(e*e)
SCRegression<-SCT-SCE

sigma<-SCE/(n-p)

varbeta<-sigma*invx


seb0 <- sqrt(varbeta[1,1])
seb1 <- sqrt(varbeta[2,2])

alfa <- 0.05

valuet <- qt(1-alfa/2,n-p)

valuechi1 <- qchisq(1-alfa/2,n-p)
valuechi2 <- qchisq(alfa/2,n-p)



bg[1]-valuet*seb0
bg[1]+valuet*seb0



bg[2]-valuet*seb1
bg[2]+valuet*seb1


tvalueb1 <- bg[2]/seb1
tvalueb1
tvalueb0 <- bg[1]/seb0
tvalueb0


valuepb0 <- 2*pt(1-tvalueb0,n-p)
valuepb0

valuepb1 <- 2*pt(tvalueb1,n-p)
valuepb1


SCE/valuechi1
SCE/valuechi2


x0 <- mean(x)
yhat <- bg[1]+bg[2]*x0
x0

varmu <- varbeta[1,1]+x0^2*varbeta[2,2]+2*x0*varbeta[1,2]


semu <- sqrt(varmu)
semu

yhat - valuet*semu
yhat + valuet*semu

Rsq <- SCRegression/SCT
Rsq

yhatt <- bg[1]+bg[2]*x
yhatt

ee <- y-yhatt

df <- data.frame(x,y)

results <- list(TSS=SCT, ESS=SCE, p=p, x=x, y=y)

if (SCE == 0) warning("Error square sum is zero", call.=FALSE)

if (print) {
cat("Results for the variables", "nt",
deparse(match.call()$x), " and ", deparse(match.call()$y),
"nn", sep="")
cat("The total square sum is: ", SCT, "nn",
"The error square sum is: ", SCE, "nn",
"The errors graph to determine homogeneity or heterogeneity is: ",plot(x,ee) , "nn",
"The linear model plot: ",plot(y,x) abline(lm(y ~ x)) , "nn",
sep="")
invisible(results)
} else {
results
}
}









share|improve this question







New contributor




Isa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
    – Tung
    Nov 17 at 22:13










  • @Tung ok let me read about it
    – Isa
    Nov 17 at 22:16










  • The cat function returns NULL and only produce character output at the console or other file device (but not graphics devices). Base graphics work via side effect and so most of them also return NULL. You should not be trying to mix them. If you are trying to produce a mixed output then learn to use Sweave or Rmarkdown.
    – 42-
    Nov 17 at 22:38












  • @42- if you write a # before the line "The linear model plot: ",plot(y,x) abline(lm(y ~ x)) , "nn", then the code works fine, no problem at all. So cat actually works, produce the errors graph.
    – Isa
    Nov 17 at 22:44












  • @Tung I'm not testing the code with sample of data, I just used a random vector x<-y<-1:10. (Because first I'd like to be sure the code works properly and then to substitute the original data )
    – Isa
    Nov 17 at 22:49















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm having problems with the following code, I do not know how to introduce this instruction below inside a cat instruction which is in the function.



df <- data.frame(x,y)
plot(y,x)
abline(lm(y ~ x))


R marks an error saying that a comma is needed between plot(x,y) and abline(lm(y~ x)) but when adding it, the display is



Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
display list redraw incomplete
2: In doTryCatch(return(expr), name, parentenv, handler) :
invalid graphics state
3: In doTryCatch(return(expr), name, parentenv, handler) :
invalid graphics state


What can I do to fix the problem?



Please someone help me, thank you in advance.



Summary<-function(x,y,print=TRUE)
{
p<-2
n<-length(x)


x<-matrix(c(rep(1,n),x),n,p)
bg<-solve(t(x)%*%x,t(x)%*%y)
invx<-solve(t(x)%*%x)
xty<-t(x)%*%y
e<-y-x%*%bg
SCT<-sum(y^2)-n*(mean(y)^2)
SCE<-sum(e*e)
SCRegression<-SCT-SCE

sigma<-SCE/(n-p)

varbeta<-sigma*invx


seb0 <- sqrt(varbeta[1,1])
seb1 <- sqrt(varbeta[2,2])

alfa <- 0.05

valuet <- qt(1-alfa/2,n-p)

valuechi1 <- qchisq(1-alfa/2,n-p)
valuechi2 <- qchisq(alfa/2,n-p)



bg[1]-valuet*seb0
bg[1]+valuet*seb0



bg[2]-valuet*seb1
bg[2]+valuet*seb1


tvalueb1 <- bg[2]/seb1
tvalueb1
tvalueb0 <- bg[1]/seb0
tvalueb0


valuepb0 <- 2*pt(1-tvalueb0,n-p)
valuepb0

valuepb1 <- 2*pt(tvalueb1,n-p)
valuepb1


SCE/valuechi1
SCE/valuechi2


x0 <- mean(x)
yhat <- bg[1]+bg[2]*x0
x0

varmu <- varbeta[1,1]+x0^2*varbeta[2,2]+2*x0*varbeta[1,2]


semu <- sqrt(varmu)
semu

yhat - valuet*semu
yhat + valuet*semu

Rsq <- SCRegression/SCT
Rsq

yhatt <- bg[1]+bg[2]*x
yhatt

ee <- y-yhatt

df <- data.frame(x,y)

results <- list(TSS=SCT, ESS=SCE, p=p, x=x, y=y)

if (SCE == 0) warning("Error square sum is zero", call.=FALSE)

if (print) {
cat("Results for the variables", "nt",
deparse(match.call()$x), " and ", deparse(match.call()$y),
"nn", sep="")
cat("The total square sum is: ", SCT, "nn",
"The error square sum is: ", SCE, "nn",
"The errors graph to determine homogeneity or heterogeneity is: ",plot(x,ee) , "nn",
"The linear model plot: ",plot(y,x) abline(lm(y ~ x)) , "nn",
sep="")
invisible(results)
} else {
results
}
}









share|improve this question







New contributor




Isa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I'm having problems with the following code, I do not know how to introduce this instruction below inside a cat instruction which is in the function.



df <- data.frame(x,y)
plot(y,x)
abline(lm(y ~ x))


R marks an error saying that a comma is needed between plot(x,y) and abline(lm(y~ x)) but when adding it, the display is



Warning messages:
1: In doTryCatch(return(expr), name, parentenv, handler) :
display list redraw incomplete
2: In doTryCatch(return(expr), name, parentenv, handler) :
invalid graphics state
3: In doTryCatch(return(expr), name, parentenv, handler) :
invalid graphics state


What can I do to fix the problem?



Please someone help me, thank you in advance.



Summary<-function(x,y,print=TRUE)
{
p<-2
n<-length(x)


x<-matrix(c(rep(1,n),x),n,p)
bg<-solve(t(x)%*%x,t(x)%*%y)
invx<-solve(t(x)%*%x)
xty<-t(x)%*%y
e<-y-x%*%bg
SCT<-sum(y^2)-n*(mean(y)^2)
SCE<-sum(e*e)
SCRegression<-SCT-SCE

sigma<-SCE/(n-p)

varbeta<-sigma*invx


seb0 <- sqrt(varbeta[1,1])
seb1 <- sqrt(varbeta[2,2])

alfa <- 0.05

valuet <- qt(1-alfa/2,n-p)

valuechi1 <- qchisq(1-alfa/2,n-p)
valuechi2 <- qchisq(alfa/2,n-p)



bg[1]-valuet*seb0
bg[1]+valuet*seb0



bg[2]-valuet*seb1
bg[2]+valuet*seb1


tvalueb1 <- bg[2]/seb1
tvalueb1
tvalueb0 <- bg[1]/seb0
tvalueb0


valuepb0 <- 2*pt(1-tvalueb0,n-p)
valuepb0

valuepb1 <- 2*pt(tvalueb1,n-p)
valuepb1


SCE/valuechi1
SCE/valuechi2


x0 <- mean(x)
yhat <- bg[1]+bg[2]*x0
x0

varmu <- varbeta[1,1]+x0^2*varbeta[2,2]+2*x0*varbeta[1,2]


semu <- sqrt(varmu)
semu

yhat - valuet*semu
yhat + valuet*semu

Rsq <- SCRegression/SCT
Rsq

yhatt <- bg[1]+bg[2]*x
yhatt

ee <- y-yhatt

df <- data.frame(x,y)

results <- list(TSS=SCT, ESS=SCE, p=p, x=x, y=y)

if (SCE == 0) warning("Error square sum is zero", call.=FALSE)

if (print) {
cat("Results for the variables", "nt",
deparse(match.call()$x), " and ", deparse(match.call()$y),
"nn", sep="")
cat("The total square sum is: ", SCT, "nn",
"The error square sum is: ", SCE, "nn",
"The errors graph to determine homogeneity or heterogeneity is: ",plot(x,ee) , "nn",
"The linear model plot: ",plot(y,x) abline(lm(y ~ x)) , "nn",
sep="")
invisible(results)
} else {
results
}
}






r plot statistics linear-regression






share|improve this question







New contributor




Isa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Isa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Isa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 17 at 21:56









Isa

1064




1064




New contributor




Isa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Isa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Isa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
    – Tung
    Nov 17 at 22:13










  • @Tung ok let me read about it
    – Isa
    Nov 17 at 22:16










  • The cat function returns NULL and only produce character output at the console or other file device (but not graphics devices). Base graphics work via side effect and so most of them also return NULL. You should not be trying to mix them. If you are trying to produce a mixed output then learn to use Sweave or Rmarkdown.
    – 42-
    Nov 17 at 22:38












  • @42- if you write a # before the line "The linear model plot: ",plot(y,x) abline(lm(y ~ x)) , "nn", then the code works fine, no problem at all. So cat actually works, produce the errors graph.
    – Isa
    Nov 17 at 22:44












  • @Tung I'm not testing the code with sample of data, I just used a random vector x<-y<-1:10. (Because first I'd like to be sure the code works properly and then to substitute the original data )
    – Isa
    Nov 17 at 22:49




















  • Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
    – Tung
    Nov 17 at 22:13










  • @Tung ok let me read about it
    – Isa
    Nov 17 at 22:16










  • The cat function returns NULL and only produce character output at the console or other file device (but not graphics devices). Base graphics work via side effect and so most of them also return NULL. You should not be trying to mix them. If you are trying to produce a mixed output then learn to use Sweave or Rmarkdown.
    – 42-
    Nov 17 at 22:38












  • @42- if you write a # before the line "The linear model plot: ",plot(y,x) abline(lm(y ~ x)) , "nn", then the code works fine, no problem at all. So cat actually works, produce the errors graph.
    – Isa
    Nov 17 at 22:44












  • @Tung I'm not testing the code with sample of data, I just used a random vector x<-y<-1:10. (Because first I'd like to be sure the code works properly and then to substitute the original data )
    – Isa
    Nov 17 at 22:49


















Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
– Tung
Nov 17 at 22:13




Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use str(), head() or screenshot)? You can use the reprex and datapasta packages to assist you with that. See also Help me Help you & How to make a great R reproducible example?
– Tung
Nov 17 at 22:13












@Tung ok let me read about it
– Isa
Nov 17 at 22:16




@Tung ok let me read about it
– Isa
Nov 17 at 22:16












The cat function returns NULL and only produce character output at the console or other file device (but not graphics devices). Base graphics work via side effect and so most of them also return NULL. You should not be trying to mix them. If you are trying to produce a mixed output then learn to use Sweave or Rmarkdown.
– 42-
Nov 17 at 22:38






The cat function returns NULL and only produce character output at the console or other file device (but not graphics devices). Base graphics work via side effect and so most of them also return NULL. You should not be trying to mix them. If you are trying to produce a mixed output then learn to use Sweave or Rmarkdown.
– 42-
Nov 17 at 22:38














@42- if you write a # before the line "The linear model plot: ",plot(y,x) abline(lm(y ~ x)) , "nn", then the code works fine, no problem at all. So cat actually works, produce the errors graph.
– Isa
Nov 17 at 22:44






@42- if you write a # before the line "The linear model plot: ",plot(y,x) abline(lm(y ~ x)) , "nn", then the code works fine, no problem at all. So cat actually works, produce the errors graph.
– Isa
Nov 17 at 22:44














@Tung I'm not testing the code with sample of data, I just used a random vector x<-y<-1:10. (Because first I'd like to be sure the code works properly and then to substitute the original data )
– Isa
Nov 17 at 22:49






@Tung I'm not testing the code with sample of data, I just used a random vector x<-y<-1:10. (Because first I'd like to be sure the code works properly and then to substitute the original data )
– Isa
Nov 17 at 22:49



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Isa is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355923%2fhow-to-use-the-plot-and-abline-inside-a-cat-which-is-inside-a-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes








Isa is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Isa is a new contributor. Be nice, and check out our Code of Conduct.













Isa is a new contributor. Be nice, and check out our Code of Conduct.












Isa is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53355923%2fhow-to-use-the-plot-and-abline-inside-a-cat-which-is-inside-a-function%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?