Why function does not display what it is supposed to display?












0















I have the following code in R but I have problems in the output, it should display something different. This is what shows



Summary(x, y)

The total square sum is: 17.5The error square sum is: 0
[[1]]
NULL

[[2]]
[1] "n"

[[3]]
NULL


It was supposed to display



The total square sum is: number1

The error square sum is: number2


Could you please check it?



(This is somehow a sample, in reality I have to display more things the standar error is: number3, the variance is number4, etc..)



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)

result <- list(
cat("The total square sum is:", SCT),
"n",
cat("The error square sum is:", SCE, "n"))
return(result)
}

x <- y <- 1:6
Summary(x, y)









share|improve this question




















  • 1





    What are the arguments you are passing to the function (x,y)?

    – Chabo
    Nov 14 '18 at 21:11











  • @Chabo x<-c(1,2,3,4,5,6) and for y the same: y<-c(1,2,3,4,5,6)

    – Isa
    Nov 14 '18 at 21:13






  • 1





    You can't "list" a "cat". cat() is a function that runs with side effects. It prints to the screen immediately. It returns NULL which is why you see that when the list gets printed.

    – MrFlick
    Nov 14 '18 at 21:14








  • 1





    What @MrFlick is suggesting: A <- 1; B <- 2; cat("num A: ", A, "n", "n", "num B: ", B, "n", sep="")

    – AkselA
    Nov 14 '18 at 21:19






  • 2





    MrFlick deleted those tags because while your code may be intended to deal with statistics, your problem had nothing to do with those statistical concepts. For instance, no one struggling with performing a regression would find this question useful.

    – joran
    Nov 14 '18 at 21:27
















0















I have the following code in R but I have problems in the output, it should display something different. This is what shows



Summary(x, y)

The total square sum is: 17.5The error square sum is: 0
[[1]]
NULL

[[2]]
[1] "n"

[[3]]
NULL


It was supposed to display



The total square sum is: number1

The error square sum is: number2


Could you please check it?



(This is somehow a sample, in reality I have to display more things the standar error is: number3, the variance is number4, etc..)



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)

result <- list(
cat("The total square sum is:", SCT),
"n",
cat("The error square sum is:", SCE, "n"))
return(result)
}

x <- y <- 1:6
Summary(x, y)









share|improve this question




















  • 1





    What are the arguments you are passing to the function (x,y)?

    – Chabo
    Nov 14 '18 at 21:11











  • @Chabo x<-c(1,2,3,4,5,6) and for y the same: y<-c(1,2,3,4,5,6)

    – Isa
    Nov 14 '18 at 21:13






  • 1





    You can't "list" a "cat". cat() is a function that runs with side effects. It prints to the screen immediately. It returns NULL which is why you see that when the list gets printed.

    – MrFlick
    Nov 14 '18 at 21:14








  • 1





    What @MrFlick is suggesting: A <- 1; B <- 2; cat("num A: ", A, "n", "n", "num B: ", B, "n", sep="")

    – AkselA
    Nov 14 '18 at 21:19






  • 2





    MrFlick deleted those tags because while your code may be intended to deal with statistics, your problem had nothing to do with those statistical concepts. For instance, no one struggling with performing a regression would find this question useful.

    – joran
    Nov 14 '18 at 21:27














0












0








0








I have the following code in R but I have problems in the output, it should display something different. This is what shows



Summary(x, y)

The total square sum is: 17.5The error square sum is: 0
[[1]]
NULL

[[2]]
[1] "n"

[[3]]
NULL


It was supposed to display



The total square sum is: number1

The error square sum is: number2


Could you please check it?



(This is somehow a sample, in reality I have to display more things the standar error is: number3, the variance is number4, etc..)



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)

result <- list(
cat("The total square sum is:", SCT),
"n",
cat("The error square sum is:", SCE, "n"))
return(result)
}

x <- y <- 1:6
Summary(x, y)









share|improve this question
















I have the following code in R but I have problems in the output, it should display something different. This is what shows



Summary(x, y)

The total square sum is: 17.5The error square sum is: 0
[[1]]
NULL

[[2]]
[1] "n"

[[3]]
NULL


It was supposed to display



The total square sum is: number1

The error square sum is: number2


Could you please check it?



(This is somehow a sample, in reality I have to display more things the standar error is: number3, the variance is number4, etc..)



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)

result <- list(
cat("The total square sum is:", SCT),
"n",
cat("The error square sum is:", SCE, "n"))
return(result)
}

x <- y <- 1:6
Summary(x, y)






r display






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 0:48









AkselA

4,57421325




4,57421325










asked Nov 14 '18 at 21:03









IsaIsa

3010




3010








  • 1





    What are the arguments you are passing to the function (x,y)?

    – Chabo
    Nov 14 '18 at 21:11











  • @Chabo x<-c(1,2,3,4,5,6) and for y the same: y<-c(1,2,3,4,5,6)

    – Isa
    Nov 14 '18 at 21:13






  • 1





    You can't "list" a "cat". cat() is a function that runs with side effects. It prints to the screen immediately. It returns NULL which is why you see that when the list gets printed.

    – MrFlick
    Nov 14 '18 at 21:14








  • 1





    What @MrFlick is suggesting: A <- 1; B <- 2; cat("num A: ", A, "n", "n", "num B: ", B, "n", sep="")

    – AkselA
    Nov 14 '18 at 21:19






  • 2





    MrFlick deleted those tags because while your code may be intended to deal with statistics, your problem had nothing to do with those statistical concepts. For instance, no one struggling with performing a regression would find this question useful.

    – joran
    Nov 14 '18 at 21:27














  • 1





    What are the arguments you are passing to the function (x,y)?

    – Chabo
    Nov 14 '18 at 21:11











  • @Chabo x<-c(1,2,3,4,5,6) and for y the same: y<-c(1,2,3,4,5,6)

    – Isa
    Nov 14 '18 at 21:13






  • 1





    You can't "list" a "cat". cat() is a function that runs with side effects. It prints to the screen immediately. It returns NULL which is why you see that when the list gets printed.

    – MrFlick
    Nov 14 '18 at 21:14








  • 1





    What @MrFlick is suggesting: A <- 1; B <- 2; cat("num A: ", A, "n", "n", "num B: ", B, "n", sep="")

    – AkselA
    Nov 14 '18 at 21:19






  • 2





    MrFlick deleted those tags because while your code may be intended to deal with statistics, your problem had nothing to do with those statistical concepts. For instance, no one struggling with performing a regression would find this question useful.

    – joran
    Nov 14 '18 at 21:27








1




1





What are the arguments you are passing to the function (x,y)?

– Chabo
Nov 14 '18 at 21:11





What are the arguments you are passing to the function (x,y)?

– Chabo
Nov 14 '18 at 21:11













@Chabo x<-c(1,2,3,4,5,6) and for y the same: y<-c(1,2,3,4,5,6)

– Isa
Nov 14 '18 at 21:13





@Chabo x<-c(1,2,3,4,5,6) and for y the same: y<-c(1,2,3,4,5,6)

– Isa
Nov 14 '18 at 21:13




1




1





You can't "list" a "cat". cat() is a function that runs with side effects. It prints to the screen immediately. It returns NULL which is why you see that when the list gets printed.

– MrFlick
Nov 14 '18 at 21:14







You can't "list" a "cat". cat() is a function that runs with side effects. It prints to the screen immediately. It returns NULL which is why you see that when the list gets printed.

– MrFlick
Nov 14 '18 at 21:14






1




1





What @MrFlick is suggesting: A <- 1; B <- 2; cat("num A: ", A, "n", "n", "num B: ", B, "n", sep="")

– AkselA
Nov 14 '18 at 21:19





What @MrFlick is suggesting: A <- 1; B <- 2; cat("num A: ", A, "n", "n", "num B: ", B, "n", sep="")

– AkselA
Nov 14 '18 at 21:19




2




2





MrFlick deleted those tags because while your code may be intended to deal with statistics, your problem had nothing to do with those statistical concepts. For instance, no one struggling with performing a regression would find this question useful.

– joran
Nov 14 '18 at 21:27





MrFlick deleted those tags because while your code may be intended to deal with statistics, your problem had nothing to do with those statistical concepts. For instance, no one struggling with performing a regression would find this question useful.

– joran
Nov 14 '18 at 21:27












1 Answer
1






active

oldest

votes


















1














list() creates a list from objects, but cat() does not return an object, it just prints to the console. That is why two of the list elements says NULL (they are empty, while one contains the character string "n" (an actual object).



Printing text with more involved formatting can be difficult and non-intuitive, but I find that much inspiration and help can be found in existing R code.



Take print.lm() for example, which is the function responsible for displaying the result from linear regressions using lm().



Run stats:::print.lm and you will see:



function (x, digits = max(3L, getOption("digits") - 3L), ...) 
{
cat("nCall:n", paste(deparse(x$call), sep = "n", collapse = "n"),
"nn", sep = "")
if (length(coef(x))) {
cat("Coefficients:n")
print.default(format(coef(x), digits = digits), print.gap = 2L,
quote = FALSE)
}
else cat("No coefficientsn")
cat("n")
invisible(x)
}


Looks a bit busy, but it's not too bad. You'll see that each call to cat() contains one or more character strings and delimiters (like n and t for new line and tabulate) laid out in order, with sep, the separator, specified at the end. Sometimes there is a paste() call inside cat(), in which case paste() merely 'prepares' some characters for cat() to print. We also note that there are several calls to cat() and print(), and mixing and matching is done, without issue. And at the very end is an example of invisible() as MrFlick mentioned in a comment. This command makes sure that the function won't print its argument (in this case x), but you can still assign it to a variable.



With these insights, can we improve on Summary()?



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

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

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", sep="")
invisible(results)
} else {
results
}
}


Looks a bit more involved. Lets test it.



Wendy <- Carlos <- 1:6

Summary(x=Wendy, y=Carlos)

Results for the variables
Wendy and Carlos

The total square sum is: 17.5

The error square sum is: 0

Warning message:
Error square sum is zero





share|improve this answer


























  • Thank you for your answer AkselA. I improved the code but in the output, in the Result for the variables part, displays structure(c(1,1,...)) .Dim=c(6L,2L) instead of Wendy name.

    – Isa
    Nov 15 '18 at 18:42













  • why does that occur?

    – Isa
    Nov 15 '18 at 18:43











  • @Isa: Notice that x is never reused as a variable name in my version of the function. When x is turned into a matrix I store it as xm, x remains as is. This is crucial for substitute() to work as intended.

    – AkselA
    Nov 15 '18 at 18:57













  • How did you displayed both names then?

    – Isa
    Nov 15 '18 at 19:07








  • 1





    @Isa: I changed the function to use match.call()$x and match.call()$y instead of substitute(). Now it shouldn't be affected by variable reassignments.

    – AkselA
    Nov 15 '18 at 19:20











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',
autoActivateHeartbeat: false,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308666%2fwhy-function-does-not-display-what-it-is-supposed-to-display%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














list() creates a list from objects, but cat() does not return an object, it just prints to the console. That is why two of the list elements says NULL (they are empty, while one contains the character string "n" (an actual object).



Printing text with more involved formatting can be difficult and non-intuitive, but I find that much inspiration and help can be found in existing R code.



Take print.lm() for example, which is the function responsible for displaying the result from linear regressions using lm().



Run stats:::print.lm and you will see:



function (x, digits = max(3L, getOption("digits") - 3L), ...) 
{
cat("nCall:n", paste(deparse(x$call), sep = "n", collapse = "n"),
"nn", sep = "")
if (length(coef(x))) {
cat("Coefficients:n")
print.default(format(coef(x), digits = digits), print.gap = 2L,
quote = FALSE)
}
else cat("No coefficientsn")
cat("n")
invisible(x)
}


Looks a bit busy, but it's not too bad. You'll see that each call to cat() contains one or more character strings and delimiters (like n and t for new line and tabulate) laid out in order, with sep, the separator, specified at the end. Sometimes there is a paste() call inside cat(), in which case paste() merely 'prepares' some characters for cat() to print. We also note that there are several calls to cat() and print(), and mixing and matching is done, without issue. And at the very end is an example of invisible() as MrFlick mentioned in a comment. This command makes sure that the function won't print its argument (in this case x), but you can still assign it to a variable.



With these insights, can we improve on Summary()?



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

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

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", sep="")
invisible(results)
} else {
results
}
}


Looks a bit more involved. Lets test it.



Wendy <- Carlos <- 1:6

Summary(x=Wendy, y=Carlos)

Results for the variables
Wendy and Carlos

The total square sum is: 17.5

The error square sum is: 0

Warning message:
Error square sum is zero





share|improve this answer


























  • Thank you for your answer AkselA. I improved the code but in the output, in the Result for the variables part, displays structure(c(1,1,...)) .Dim=c(6L,2L) instead of Wendy name.

    – Isa
    Nov 15 '18 at 18:42













  • why does that occur?

    – Isa
    Nov 15 '18 at 18:43











  • @Isa: Notice that x is never reused as a variable name in my version of the function. When x is turned into a matrix I store it as xm, x remains as is. This is crucial for substitute() to work as intended.

    – AkselA
    Nov 15 '18 at 18:57













  • How did you displayed both names then?

    – Isa
    Nov 15 '18 at 19:07








  • 1





    @Isa: I changed the function to use match.call()$x and match.call()$y instead of substitute(). Now it shouldn't be affected by variable reassignments.

    – AkselA
    Nov 15 '18 at 19:20
















1














list() creates a list from objects, but cat() does not return an object, it just prints to the console. That is why two of the list elements says NULL (they are empty, while one contains the character string "n" (an actual object).



Printing text with more involved formatting can be difficult and non-intuitive, but I find that much inspiration and help can be found in existing R code.



Take print.lm() for example, which is the function responsible for displaying the result from linear regressions using lm().



Run stats:::print.lm and you will see:



function (x, digits = max(3L, getOption("digits") - 3L), ...) 
{
cat("nCall:n", paste(deparse(x$call), sep = "n", collapse = "n"),
"nn", sep = "")
if (length(coef(x))) {
cat("Coefficients:n")
print.default(format(coef(x), digits = digits), print.gap = 2L,
quote = FALSE)
}
else cat("No coefficientsn")
cat("n")
invisible(x)
}


Looks a bit busy, but it's not too bad. You'll see that each call to cat() contains one or more character strings and delimiters (like n and t for new line and tabulate) laid out in order, with sep, the separator, specified at the end. Sometimes there is a paste() call inside cat(), in which case paste() merely 'prepares' some characters for cat() to print. We also note that there are several calls to cat() and print(), and mixing and matching is done, without issue. And at the very end is an example of invisible() as MrFlick mentioned in a comment. This command makes sure that the function won't print its argument (in this case x), but you can still assign it to a variable.



With these insights, can we improve on Summary()?



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

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

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", sep="")
invisible(results)
} else {
results
}
}


Looks a bit more involved. Lets test it.



Wendy <- Carlos <- 1:6

Summary(x=Wendy, y=Carlos)

Results for the variables
Wendy and Carlos

The total square sum is: 17.5

The error square sum is: 0

Warning message:
Error square sum is zero





share|improve this answer


























  • Thank you for your answer AkselA. I improved the code but in the output, in the Result for the variables part, displays structure(c(1,1,...)) .Dim=c(6L,2L) instead of Wendy name.

    – Isa
    Nov 15 '18 at 18:42













  • why does that occur?

    – Isa
    Nov 15 '18 at 18:43











  • @Isa: Notice that x is never reused as a variable name in my version of the function. When x is turned into a matrix I store it as xm, x remains as is. This is crucial for substitute() to work as intended.

    – AkselA
    Nov 15 '18 at 18:57













  • How did you displayed both names then?

    – Isa
    Nov 15 '18 at 19:07








  • 1





    @Isa: I changed the function to use match.call()$x and match.call()$y instead of substitute(). Now it shouldn't be affected by variable reassignments.

    – AkselA
    Nov 15 '18 at 19:20














1












1








1







list() creates a list from objects, but cat() does not return an object, it just prints to the console. That is why two of the list elements says NULL (they are empty, while one contains the character string "n" (an actual object).



Printing text with more involved formatting can be difficult and non-intuitive, but I find that much inspiration and help can be found in existing R code.



Take print.lm() for example, which is the function responsible for displaying the result from linear regressions using lm().



Run stats:::print.lm and you will see:



function (x, digits = max(3L, getOption("digits") - 3L), ...) 
{
cat("nCall:n", paste(deparse(x$call), sep = "n", collapse = "n"),
"nn", sep = "")
if (length(coef(x))) {
cat("Coefficients:n")
print.default(format(coef(x), digits = digits), print.gap = 2L,
quote = FALSE)
}
else cat("No coefficientsn")
cat("n")
invisible(x)
}


Looks a bit busy, but it's not too bad. You'll see that each call to cat() contains one or more character strings and delimiters (like n and t for new line and tabulate) laid out in order, with sep, the separator, specified at the end. Sometimes there is a paste() call inside cat(), in which case paste() merely 'prepares' some characters for cat() to print. We also note that there are several calls to cat() and print(), and mixing and matching is done, without issue. And at the very end is an example of invisible() as MrFlick mentioned in a comment. This command makes sure that the function won't print its argument (in this case x), but you can still assign it to a variable.



With these insights, can we improve on Summary()?



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

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

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", sep="")
invisible(results)
} else {
results
}
}


Looks a bit more involved. Lets test it.



Wendy <- Carlos <- 1:6

Summary(x=Wendy, y=Carlos)

Results for the variables
Wendy and Carlos

The total square sum is: 17.5

The error square sum is: 0

Warning message:
Error square sum is zero





share|improve this answer















list() creates a list from objects, but cat() does not return an object, it just prints to the console. That is why two of the list elements says NULL (they are empty, while one contains the character string "n" (an actual object).



Printing text with more involved formatting can be difficult and non-intuitive, but I find that much inspiration and help can be found in existing R code.



Take print.lm() for example, which is the function responsible for displaying the result from linear regressions using lm().



Run stats:::print.lm and you will see:



function (x, digits = max(3L, getOption("digits") - 3L), ...) 
{
cat("nCall:n", paste(deparse(x$call), sep = "n", collapse = "n"),
"nn", sep = "")
if (length(coef(x))) {
cat("Coefficients:n")
print.default(format(coef(x), digits = digits), print.gap = 2L,
quote = FALSE)
}
else cat("No coefficientsn")
cat("n")
invisible(x)
}


Looks a bit busy, but it's not too bad. You'll see that each call to cat() contains one or more character strings and delimiters (like n and t for new line and tabulate) laid out in order, with sep, the separator, specified at the end. Sometimes there is a paste() call inside cat(), in which case paste() merely 'prepares' some characters for cat() to print. We also note that there are several calls to cat() and print(), and mixing and matching is done, without issue. And at the very end is an example of invisible() as MrFlick mentioned in a comment. This command makes sure that the function won't print its argument (in this case x), but you can still assign it to a variable.



With these insights, can we improve on Summary()?



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

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

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", sep="")
invisible(results)
} else {
results
}
}


Looks a bit more involved. Lets test it.



Wendy <- Carlos <- 1:6

Summary(x=Wendy, y=Carlos)

Results for the variables
Wendy and Carlos

The total square sum is: 17.5

The error square sum is: 0

Warning message:
Error square sum is zero






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 19:18

























answered Nov 15 '18 at 0:36









AkselAAkselA

4,57421325




4,57421325













  • Thank you for your answer AkselA. I improved the code but in the output, in the Result for the variables part, displays structure(c(1,1,...)) .Dim=c(6L,2L) instead of Wendy name.

    – Isa
    Nov 15 '18 at 18:42













  • why does that occur?

    – Isa
    Nov 15 '18 at 18:43











  • @Isa: Notice that x is never reused as a variable name in my version of the function. When x is turned into a matrix I store it as xm, x remains as is. This is crucial for substitute() to work as intended.

    – AkselA
    Nov 15 '18 at 18:57













  • How did you displayed both names then?

    – Isa
    Nov 15 '18 at 19:07








  • 1





    @Isa: I changed the function to use match.call()$x and match.call()$y instead of substitute(). Now it shouldn't be affected by variable reassignments.

    – AkselA
    Nov 15 '18 at 19:20



















  • Thank you for your answer AkselA. I improved the code but in the output, in the Result for the variables part, displays structure(c(1,1,...)) .Dim=c(6L,2L) instead of Wendy name.

    – Isa
    Nov 15 '18 at 18:42













  • why does that occur?

    – Isa
    Nov 15 '18 at 18:43











  • @Isa: Notice that x is never reused as a variable name in my version of the function. When x is turned into a matrix I store it as xm, x remains as is. This is crucial for substitute() to work as intended.

    – AkselA
    Nov 15 '18 at 18:57













  • How did you displayed both names then?

    – Isa
    Nov 15 '18 at 19:07








  • 1





    @Isa: I changed the function to use match.call()$x and match.call()$y instead of substitute(). Now it shouldn't be affected by variable reassignments.

    – AkselA
    Nov 15 '18 at 19:20

















Thank you for your answer AkselA. I improved the code but in the output, in the Result for the variables part, displays structure(c(1,1,...)) .Dim=c(6L,2L) instead of Wendy name.

– Isa
Nov 15 '18 at 18:42







Thank you for your answer AkselA. I improved the code but in the output, in the Result for the variables part, displays structure(c(1,1,...)) .Dim=c(6L,2L) instead of Wendy name.

– Isa
Nov 15 '18 at 18:42















why does that occur?

– Isa
Nov 15 '18 at 18:43





why does that occur?

– Isa
Nov 15 '18 at 18:43













@Isa: Notice that x is never reused as a variable name in my version of the function. When x is turned into a matrix I store it as xm, x remains as is. This is crucial for substitute() to work as intended.

– AkselA
Nov 15 '18 at 18:57







@Isa: Notice that x is never reused as a variable name in my version of the function. When x is turned into a matrix I store it as xm, x remains as is. This is crucial for substitute() to work as intended.

– AkselA
Nov 15 '18 at 18:57















How did you displayed both names then?

– Isa
Nov 15 '18 at 19:07







How did you displayed both names then?

– Isa
Nov 15 '18 at 19:07






1




1





@Isa: I changed the function to use match.call()$x and match.call()$y instead of substitute(). Now it shouldn't be affected by variable reassignments.

– AkselA
Nov 15 '18 at 19:20





@Isa: I changed the function to use match.call()$x and match.call()$y instead of substitute(). Now it shouldn't be affected by variable reassignments.

– AkselA
Nov 15 '18 at 19:20




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308666%2fwhy-function-does-not-display-what-it-is-supposed-to-display%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

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values