How To Draw The Regression Line In R
Besprinkle plot based on a model
You can create a scatter plot based on a theoretical model and add information technology to the plot with the lines office. Consider the example of the following block of code equally illustration.
# Data. Model: Y = Ten ^ 2 ready.seed(54) x <- seq(0, ten, by = 0.05) y <- x ^ ii + rnorm(length(ten), sd = 20) # Scatter plot and underlying model plot(x, y, pch = 16) lines(x, x ^ two, col = 2, lwd = 3) # Text text(2, 70, expression(Y == X ^ 2))
Scatter plot with linear regression
You can add a regression line to a scatter plot passing a lm object to the abline function. Recall that coef returns the coefficients of an estimated linear model.
# Data. Model: Y = 10 ^ 2 prepare.seed(54) x <- seq(0, x, by = 0.05) y <- x ^ 2 + rnorm(length(ten), sd = xx) # Scatter plot and linear regression line plot(ten, y, pch = xvi) abline(lm(y ~ 10), col = iv, lwd = 3) # Text coef <- round(coef(lm(y ~ ten)), two) text(2, lxx, paste("Y = ", coef[i], "+", coef[2], "x")) Scatter plot with LOWESS regression curve
The LOWESS smoother uses locally-weighted polynomial regression. This not-parametric regression tin can be estimated with lowess function.
# Data. Model: Y = X ^ 2 fix.seed(54) x <- seq(0, 10, by = 0.05) y <- x ^ 2 + rnorm(length(x), sd = xx) # Besprinkle plot and LOWESS regression curve plot(10, y, pch = 16) lines(lowess(x, y), col = 3, lwd = 3)
Source: https://r-charts.com/correlation/scatter-plot-regression-line/
Posted by: mayoincents1958.blogspot.com

0 Response to "How To Draw The Regression Line In R"
Post a Comment