0

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", "\n\t",
        deparse(match.call()$x), " and ", deparse(match.call()$y),
        "\n\n", sep="")
    cat("The total square sum is: ", SCT, "\n\n",
        "The error square sum is: ", SCE, "\n\n",
        "The errors graph to determine homogeneity or heterogeneity is: ",plot(x,ee) , "\n\n",
        "The linear model plot: ",plot(y,x) abline(lm(y ~ x)) , "\n\n",
        sep="")
    invisible(results)
  } else {
    results
  }      
}
user9802913
  • 245
  • 4
  • 20
  • 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`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Nov 17 '18 at 22:13
  • @Tung ok let me read about it – user9802913 Nov 17 '18 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. – IRTFM Nov 17 '18 at 22:38
  • @42- if you write a # before the line `"The linear model plot: ",plot(y,x) abline(lm(y ~ x)) , "\n\n",` then the code works fine, no problem at all. So cat actually works, produce the errors graph. – user9802913 Nov 17 '18 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 ) – user9802913 Nov 17 '18 at 22:49
  • @Tung I think the minimal runnable code necessary to reproduce the error is `cat("The linear model plot: ",plot(y,x) abline(lm(y ~ x)) )`. I might be wrong because all this is inside a function. – user9802913 Nov 17 '18 at 22:52
  • Sure cat "works". But if your goal is to have the output of a call to plot appear in hte same document as other cat output then you need to do something different. – IRTFM Nov 17 '18 at 22:53
  • @42- ah, yes the goal to make both graphs appear in the same document or screen. – user9802913 Nov 17 '18 at 22:57
  • @42- Rmarkdown does not appear in the Help. why? – user9802913 Nov 17 '18 at 23:00
  • It's a package: https://rmarkdown.rstudio.com, https://github.com/rstudio/rmarkdown – IRTFM Nov 18 '18 at 00:29

0 Answers0