26/9/2019

Let’s print the system date

Sys.Date()
## [1] "2019-09-26"

Let’s do the following steps: 1. Load plotly library 2. Load airquality dataset and save them into data variable 3. Omit NA’s 4. Atach dataset

library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
data <- airquality[complete.cases(airquality),]
attach(data)

Next step is plot Ozone, Temp and Solar.R into a 3D plot

plot_ly(x = Temp,
        y = Ozone,
        z = Solar.R,
        type = "scatter3d",
        mode = "markers",
        color = Ozone)

Now we are goint to make a liner regressión with Y = Temp, X1 = Ozone and X2 = Solar.R

fit <- lm(Temp ~ Ozone + Solar.R)
summary(fit)
## 
## Call:
## lm(formula = Temp ~ Ozone + Solar.R)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -21.576  -5.137   1.625   4.741  12.511 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 68.497103   1.528467  44.814  < 2e-16 ***
## Ozone        0.194294   0.020977   9.262 2.22e-15 ***
## Solar.R      0.006039   0.007658   0.789    0.432    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.862 on 108 degrees of freedom
## Multiple R-squared:  0.4909, Adjusted R-squared:  0.4815 
## F-statistic: 52.07 on 2 and 108 DF,  p-value: < 2.2e-16

As final step, let’s plot the regressión residuals (histogram) and predicted vs real values (scatterplot)

plot_ly(x = fit$residuals, type = "histogram", mode = "markers")
## Warning: 'histogram' objects don't have these attributes: 'mode'
## Valid attributes include:
## 'type', 'visible', 'showlegend', 'legendgroup', 'opacity', 'name', 'uid', 'ids', 'customdata', 'selectedpoints', 'hoverinfo', 'hoverlabel', 'stream', 'transforms', 'uirevision', 'x', 'y', 'text', 'hovertext', 'orientation', 'histfunc', 'histnorm', 'cumulative', 'nbinsx', 'xbins', 'nbinsy', 'ybins', 'autobinx', 'autobiny', 'hovertemplate', 'marker', 'offsetgroup', 'alignmentgroup', 'selected', 'unselected', '_deprecated', 'error_x', 'error_y', 'xcalendar', 'ycalendar', 'xaxis', 'yaxis', 'idssrc', 'customdatasrc', 'hoverinfosrc', 'xsrc', 'ysrc', 'textsrc', 'hovertextsrc', 'hovertemplatesrc', 'key', 'set', 'frame', 'transforms', '_isNestedKey', '_isSimpleKey', '_isGraticule', '_bbox'

plot_ly(
  x = data$Temp,
  y = predict(fit, data),
  mode = "markers",
  type = "scatter"
)