Quantcast
Channel: r software hub
Viewing all articles
Browse latest Browse all 1015

Regression Diagnostic Plots using R and Plotly

$
0
0

By Riddhiman

Plotly is a platform for making, editing, and sharing customizable and interactive graphs. Embedding Plotly graphs in a R-Markdown document is very easy. Here, we will genarate a R-Markdown document with embedded Plotly charts to visualize regression diagnostic plots similar to the ones generated by using plot() on a fitted lm() object.

R-Studio

First step is to install R-Studio. R-Studio makes it very easy to write R-Markdown documents.

Install Plotly

Run the following command(s) in the console

# Not run
# install.packages("plotly")
# install.packages("MASS")
library(plotly)

Function to generate plots

The following code snippet creates a function that accepts a fitted lm() object and returns plotly charts. Paste the following code snippet(s) as chunks in the R-Markdown document as shown.

RegressionPlots  function(fit){

# Extract fitted values from lm() object
Fitted.Values fitted(fit)

# Extract residuals from lm() object
Residuals resid(fit)

# Extract standardized residuals from lm() object
Standardized.Residuals MASS::stdres(fit)

# Extract fitted values for lm() object
Theoretical.Quantiles qqnorm(Residuals, plot.it = F)$x

# Square root of abs(residuals)
Root.Residuals sqrt(abs(Standardized.Residuals))

# Calculate Leverage
Leverage lm.influence(fit)$hat

# Create data frame
# Will be used as input to plot_ly

regMat data.frame(Fitted.Values,
Residuals,
Standardized.Residuals,
Theoretical.Quantiles,
...read more

Source:: r-bloggers.com


Viewing all articles
Browse latest Browse all 1015

Trending Articles