Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get trendline equations in R

Tags:

r

trendline

i have a series of data in form of time and temperature value. I want to generate a trendline and find the subsequent equation. This could be done in excel. but, how could i find equation of the trendline in R.

abline function can be used to generate a trendline but where is the underlying equation?? I saw a link where they have used the following code to illustrate the issue

x <- sample(1:100, 100, replace = TRUE)

y <- x + rnorm(100, sd = 4)
mydf <- data.frame(x = x, y = y)
plot(y ~ x, data = mydf)
model <- lm(y ~ x, data = mydf)
abline(model, col = "red")
summary(model)

if you execute it, can make some sense of the result and see the equation, please let me know. else help me to get equation of trendlines with R

Thanks!

like image 596
Jio Avatar asked Jul 22 '14 08:07

Jio


People also ask

How do you find the trendline equation?

How does someone calculate the trend line for a graph? A trend line indicates a linear relationship. The equation for a linear relationship is y = mx + b, where x is the independent variable, y is the dependent variable, m is the slope of the line, and b is the y-intercept.

How do you find the equation of a regression line in R?

The mathematical formula of the linear regression can be written as y = b0 + b1*x + e , where: b0 and b1 are known as the regression beta coefficients or parameters: b0 is the intercept of the regression line; that is the predicted value when x = 0 . b1 is the slope of the regression line.


1 Answers

but where is the underlying equation?

In the “Coefficients”:

> coef(model)
(Intercept)           x
  1.2093273   0.9786051

Where “(Intercept)” is, well, the y-intercept, and “x” is the slope. In other words, you can retrieve the equation like this:

paste('y =', coef(model)[[2]], '* x', '+', coef(model)[[1]])
like image 123
Konrad Rudolph Avatar answered Sep 27 '22 20:09

Konrad Rudolph