r - Input distance from data point to regression (using residuals?) into a new dataframe column -


i want able calculate distance of each individual data point on plot linear regression on plot, , store distances new variable (column) in original dataframe. based on this answer, distance value can found using residuals of linear regression. however, not know how apply each individual point, , not know how store values within dataframe (if possible).

i created example data...

ex.age <- c(50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70) ex.score <- c(10,9,9,10,8,7,9,6,8,7,6,8,6,5,6,4,5,6,3,5,3) ex.df <- data.frame(ex.age,ex.score) 

when graphed, looks this... graph

i want able calculate distance each point regression line, , store in new column, ex.df$reg.dev.

how able accomplish that?

thank you.

all need grab residuals lm().

ex.age <- c(50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70) ex.score <- c(10,9,9,10,8,7,9,6,8,7,6,8,6,5,6,4,5,6,3,5,3) ex.df <- data.frame(ex.age,ex.score)  ex_model <- lm(ex.score ~ ex.age, data = ex.df) ex.df$reg.dev <- ex_model$residuals 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -