Home > other >  Model fitting, get weird lines rather than points
Model fitting, get weird lines rather than points

Time:04-15

Im trying to fit a linear model here, yet instead of points plotted with geom_point i get a line through every datapoint. Does anyone know how to solve this so i get points rather than this jagged line. Data and code below

data snippet

height weight   age gender
    <dbl>  <dbl> <dbl> <fct> 
 1   152.   47.8    63 1     
 2   140.   36.5    63 0     
 3   137.   31.9    65 0     
 4   157.   53.0    41 1     
 5   145.   41.3    51 0     
 6   164.   63.0    35 1     
 7   149.   38.2    32 0     
 8   169.   55.5    27 1     
 9   148.   34.9    19 0     
10   165.   54.5    54 1     
11   154.   49.9    47 0     
12   151.   41.2    66 1

code used;

linear_mod = lm(weight ~ height, data = weight_height)

#plot predictions
#function
plot_predicts <- function(model, df) {

#make predictions
preds <- predict(model, df)

#plot
df %>% 
   mutate(predicted=preds) %>% 
   dplyr::select(weight, height, predicted) %>% 
   pivot_longer(-height) %>% 
   ggplot(aes(x=height, y=value, colour=name))  
   geom_point(data=. %>% filter(name=="height"))  
   geom_line(data=. %>% filter(name!="height"))
}

plot_predicts(linear_mod, weight_height)

the data snipet might not be enough to re-run the graph i get so i have attatched it below enter image description here

what i'm looking to get (with a linear line though); enter image description here

CodePudding user response:

What about this?

library(tidyverse)

df %>% 
  ggplot(aes(x=height, y=weight))  
  geom_point() 
  geom_smooth(method = "lm", se=TRUE)

enter image description here

CodePudding user response:

You are complicating the linear fitting and prediction too much, ggplot2 has layer geom_smooth that does all the work automatically.

weight_height <- read.table(text = "
height weight   age gender
 1   152.   47.8    63 1     
 2   140.   36.5    63 0     
 3   137.   31.9    65 0     
 4   157.   53.0    41 1     
 5   145.   41.3    51 0     
 6   164.   63.0    35 1     
 7   149.   38.2    32 0     
 8   169.   55.5    27 1     
 9   148.   34.9    19 0     
10   165.   54.5    54 1     
11   154.   49.9    47 0     
12   151.   41.2    66 1
", header = TRUE)
weight_height$gender <- factor(weight_height$gender)

suppressPackageStartupMessages({
  library(dplyr)
  library(ggplot2)
})

#plot predictions function
plot_predicts <- function(df) {
  ggplot(df, aes(x = height, y = weight))  
    geom_point()  
    geom_smooth(formula = y ~ x, method = "lm", se = FALSE)
}

plot_predicts(weight_height)

Created on 2022-04-14 by the reprex package (v2.0.1)

  • Related