Struggling to adapt code from stackoverflow to data set - Adding Regression Line Equation and R2 on graph












0















I found a function that adds a regression line equation on graph.
Adding Regression Line Equation and R2 on graph
If I copy the function and data sample, everything works fine. But when I try to adapt the code, I get the message, that Y can't be found.



The Function:



lm_eqn <- function(df){
m <- lm(y ~ x, df);
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq));
}


My data sample and adapted code:



library(tidyverse)
set.seed(1)
id <- 1:5000
zip <- sample(100:200, 5000, replace = TRUE)
outcome <- rbinom(5000, 1, 0.23)
df <- data.frame(id, outcome, zip) %>% as_tibble()
new_df <- df %>% group_by(zip) %>% summarise(ratio = mean(outcome))

library(ggplot2)
t <- ggplot(data = new_df, aes(x = zip, y = ratio)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()

t1 <- t + geom_text(x = 25, y = 300, label = lm_eqn(df), parse = TRUE)



Error in eval(predvars, data, env) : object 'y' not found




Where did I make the mistake? Thanks in advance!










share|improve this question




















  • 2





    Your function has the line m <- lm(y ~ x, df) but there is no variable called x or y in your df variable.

    – MrFlick
    Nov 28 '18 at 21:04











  • I have edited the function and I don't get an error anymore but no equation on the graph. I have changed the variables names from the example so I understand which operator does what but clearly I can't the see right answer. And I'm fairly new to R and functions especially.

    – Schillerlocke
    Nov 28 '18 at 22:25






  • 1





    Well, you are plotting the label at zip=25 and ratio=300. those values are outside where all your points are. Maybe choose a different position.

    – MrFlick
    Nov 28 '18 at 22:27











  • Thank you, it works now and I understand a bit more.

    – Schillerlocke
    Nov 28 '18 at 22:45
















0















I found a function that adds a regression line equation on graph.
Adding Regression Line Equation and R2 on graph
If I copy the function and data sample, everything works fine. But when I try to adapt the code, I get the message, that Y can't be found.



The Function:



lm_eqn <- function(df){
m <- lm(y ~ x, df);
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq));
}


My data sample and adapted code:



library(tidyverse)
set.seed(1)
id <- 1:5000
zip <- sample(100:200, 5000, replace = TRUE)
outcome <- rbinom(5000, 1, 0.23)
df <- data.frame(id, outcome, zip) %>% as_tibble()
new_df <- df %>% group_by(zip) %>% summarise(ratio = mean(outcome))

library(ggplot2)
t <- ggplot(data = new_df, aes(x = zip, y = ratio)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()

t1 <- t + geom_text(x = 25, y = 300, label = lm_eqn(df), parse = TRUE)



Error in eval(predvars, data, env) : object 'y' not found




Where did I make the mistake? Thanks in advance!










share|improve this question




















  • 2





    Your function has the line m <- lm(y ~ x, df) but there is no variable called x or y in your df variable.

    – MrFlick
    Nov 28 '18 at 21:04











  • I have edited the function and I don't get an error anymore but no equation on the graph. I have changed the variables names from the example so I understand which operator does what but clearly I can't the see right answer. And I'm fairly new to R and functions especially.

    – Schillerlocke
    Nov 28 '18 at 22:25






  • 1





    Well, you are plotting the label at zip=25 and ratio=300. those values are outside where all your points are. Maybe choose a different position.

    – MrFlick
    Nov 28 '18 at 22:27











  • Thank you, it works now and I understand a bit more.

    – Schillerlocke
    Nov 28 '18 at 22:45














0












0








0








I found a function that adds a regression line equation on graph.
Adding Regression Line Equation and R2 on graph
If I copy the function and data sample, everything works fine. But when I try to adapt the code, I get the message, that Y can't be found.



The Function:



lm_eqn <- function(df){
m <- lm(y ~ x, df);
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq));
}


My data sample and adapted code:



library(tidyverse)
set.seed(1)
id <- 1:5000
zip <- sample(100:200, 5000, replace = TRUE)
outcome <- rbinom(5000, 1, 0.23)
df <- data.frame(id, outcome, zip) %>% as_tibble()
new_df <- df %>% group_by(zip) %>% summarise(ratio = mean(outcome))

library(ggplot2)
t <- ggplot(data = new_df, aes(x = zip, y = ratio)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()

t1 <- t + geom_text(x = 25, y = 300, label = lm_eqn(df), parse = TRUE)



Error in eval(predvars, data, env) : object 'y' not found




Where did I make the mistake? Thanks in advance!










share|improve this question
















I found a function that adds a regression line equation on graph.
Adding Regression Line Equation and R2 on graph
If I copy the function and data sample, everything works fine. But when I try to adapt the code, I get the message, that Y can't be found.



The Function:



lm_eqn <- function(df){
m <- lm(y ~ x, df);
eq <- substitute(italic(y) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits = 2),
b = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3)))
as.character(as.expression(eq));
}


My data sample and adapted code:



library(tidyverse)
set.seed(1)
id <- 1:5000
zip <- sample(100:200, 5000, replace = TRUE)
outcome <- rbinom(5000, 1, 0.23)
df <- data.frame(id, outcome, zip) %>% as_tibble()
new_df <- df %>% group_by(zip) %>% summarise(ratio = mean(outcome))

library(ggplot2)
t <- ggplot(data = new_df, aes(x = zip, y = ratio)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point()

t1 <- t + geom_text(x = 25, y = 300, label = lm_eqn(df), parse = TRUE)



Error in eval(predvars, data, env) : object 'y' not found




Where did I make the mistake? Thanks in advance!







r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 21:22









MrFlick

124k11141173




124k11141173










asked Nov 28 '18 at 21:01









SchillerlockeSchillerlocke

246




246








  • 2





    Your function has the line m <- lm(y ~ x, df) but there is no variable called x or y in your df variable.

    – MrFlick
    Nov 28 '18 at 21:04











  • I have edited the function and I don't get an error anymore but no equation on the graph. I have changed the variables names from the example so I understand which operator does what but clearly I can't the see right answer. And I'm fairly new to R and functions especially.

    – Schillerlocke
    Nov 28 '18 at 22:25






  • 1





    Well, you are plotting the label at zip=25 and ratio=300. those values are outside where all your points are. Maybe choose a different position.

    – MrFlick
    Nov 28 '18 at 22:27











  • Thank you, it works now and I understand a bit more.

    – Schillerlocke
    Nov 28 '18 at 22:45














  • 2





    Your function has the line m <- lm(y ~ x, df) but there is no variable called x or y in your df variable.

    – MrFlick
    Nov 28 '18 at 21:04











  • I have edited the function and I don't get an error anymore but no equation on the graph. I have changed the variables names from the example so I understand which operator does what but clearly I can't the see right answer. And I'm fairly new to R and functions especially.

    – Schillerlocke
    Nov 28 '18 at 22:25






  • 1





    Well, you are plotting the label at zip=25 and ratio=300. those values are outside where all your points are. Maybe choose a different position.

    – MrFlick
    Nov 28 '18 at 22:27











  • Thank you, it works now and I understand a bit more.

    – Schillerlocke
    Nov 28 '18 at 22:45








2




2





Your function has the line m <- lm(y ~ x, df) but there is no variable called x or y in your df variable.

– MrFlick
Nov 28 '18 at 21:04





Your function has the line m <- lm(y ~ x, df) but there is no variable called x or y in your df variable.

– MrFlick
Nov 28 '18 at 21:04













I have edited the function and I don't get an error anymore but no equation on the graph. I have changed the variables names from the example so I understand which operator does what but clearly I can't the see right answer. And I'm fairly new to R and functions especially.

– Schillerlocke
Nov 28 '18 at 22:25





I have edited the function and I don't get an error anymore but no equation on the graph. I have changed the variables names from the example so I understand which operator does what but clearly I can't the see right answer. And I'm fairly new to R and functions especially.

– Schillerlocke
Nov 28 '18 at 22:25




1




1





Well, you are plotting the label at zip=25 and ratio=300. those values are outside where all your points are. Maybe choose a different position.

– MrFlick
Nov 28 '18 at 22:27





Well, you are plotting the label at zip=25 and ratio=300. those values are outside where all your points are. Maybe choose a different position.

– MrFlick
Nov 28 '18 at 22:27













Thank you, it works now and I understand a bit more.

– Schillerlocke
Nov 28 '18 at 22:45





Thank you, it works now and I understand a bit more.

– Schillerlocke
Nov 28 '18 at 22:45












0






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',
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%2f53528032%2fstruggling-to-adapt-code-from-stackoverflow-to-data-set-adding-regression-line%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53528032%2fstruggling-to-adapt-code-from-stackoverflow-to-data-set-adding-regression-line%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)