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
}
}
r plot statistics linear-regression
New contributor
|
show 5 more comments
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
}
}
r plot statistics linear-regression
New contributor
Could you make your problem reproducible by sharing a sample of your data so others can help (please do not usestr()
,head()
or screenshot)? You can use thereprex
anddatapasta
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 vectorx<-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
|
show 5 more comments
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
}
}
r plot statistics linear-regression
New contributor
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
r plot statistics linear-regression
New contributor
New contributor
New contributor
asked Nov 17 at 21:56
Isa
1064
1064
New contributor
New contributor
Could you make your problem reproducible by sharing a sample of your data so others can help (please do not usestr()
,head()
or screenshot)? You can use thereprex
anddatapasta
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 vectorx<-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
|
show 5 more comments
Could you make your problem reproducible by sharing a sample of your data so others can help (please do not usestr()
,head()
or screenshot)? You can use thereprex
anddatapasta
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 vectorx<-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
|
show 5 more comments
active
oldest
votes
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 thereprex
anddatapasta
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