R Regression: Simple Linear Regression

From OnnoWiki
Jump to navigation Jump to search
# Ref: http://www.sthda.com/english/articles/40-regression-analysis/167-simple-linear-regression-in-r/ 

if(!require(devtools)) install.packages("devtools")
devtools::install_github("kassambara/datarium")
data("marketing", package = "datarium")
head(marketing, 3)

data("swiss")
head(swiss, 3)

data("Boston", package = "MASS")
head(Boston, 3)

install.packages("tidyverse")
install.packages("ggpubr")
library(tidyverse)
library(ggplot2)
library(ggpubr)
theme_set(theme_pubr())

# Load the package
data("marketing", package = "datarium")
head(marketing, 4)

# Plot SMOOTH
ggplot(marketing, aes(x = youtube, y = sales)) +
  geom_point() +
  stat_smooth()

# Calculate Correlation
cor(marketing$sales, marketing$youtube)

# Computation
model <- lm(sales ~ youtube, data = marketing)
model

# PLOT Model
ggplot(marketing, aes(youtube, sales)) +
  geom_point() +
  stat_smooth(method = lm)

# model summary
summary(model)
# confidence interval
confint(model)
# Residual standard error (RSE).
sigma(model)*100/mean(marketing$sales)


Referensi

Pranala Menarik