R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
advertising <- read.csv( "https://raw.githubusercontent.com/utjimmyx/regression/master/advertising.csv" )

write.csv(advertising, "advertising.csv", row.names = FALSE)
install.packages("readxl")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
library(readxl)
my_data <- read_excel("advertising_randomized.xlsx")

## run the library
install.packages("tidyverse")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
library(tidyverse)

head(my_data)
## # A tibble: 6 × 6
##       X    X1    TV radio newspaper sales
##   <dbl> <dbl> <dbl> <dbl>     <dbl> <dbl>
## 1    79     4  318. 32.2       30.5  9.84
## 2    75   157  128.  3.69      12.6 18.1 
## 3    18    67  360.  6.92      12.4 22.4 
## 4    74   115  230. 53.6       49.0 10.6 
## 5    95   183  227. 26.0       48.5  8.49
## 6    20    43  196.  1.46      22.4 22.2
glimpse(my_data)
## Rows: 250
## Columns: 6
## $ X         <dbl> 79, 75, 18, 74, 95, 20, 48, 156, 123, 100, 145, 87, 147, 95,…
## $ X1        <dbl> 4, 157, 67, 115, 183, 43, 21, 142, 41, 81, 120, 54, 74, 111,…
## $ TV        <dbl> 318.30, 128.23, 359.50, 230.16, 226.56, 196.38, 175.99, 115.…
## $ radio     <dbl> 32.16, 3.69, 6.92, 53.65, 26.01, 1.46, 29.37, 36.88, 15.38, …
## $ newspaper <dbl> 30.49, 12.58, 12.41, 49.02, 48.50, 22.37, 18.10, 43.34, 36.7…
## $ sales     <dbl> 9.84, 18.09, 22.45, 10.56, 8.49, 22.24, 2.54, 14.57, 27.98, …
ggplot(data = my_data)

str(my_data)
## tibble [250 × 6] (S3: tbl_df/tbl/data.frame)
##  $ X        : num [1:250] 79 75 18 74 95 20 48 156 123 100 ...
##  $ X1       : num [1:250] 4 157 67 115 183 43 21 142 41 81 ...
##  $ TV       : num [1:250] 318 128 360 230 227 ...
##  $ radio    : num [1:250] 32.16 3.69 6.92 53.65 26.01 ...
##  $ newspaper: num [1:250] 30.5 12.6 12.4 49 48.5 ...
##  $ sales    : num [1:250] 9.84 18.09 22.45 10.56 8.49 ...
ggplot(
  data =  my_data,
  mapping = aes(x = TV, y = sales)
)

ggplot(
  data =  my_data,
  mapping = aes(x = TV, y = sales)
) +
  geom_point()

#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

ggplot(
  data =  my_data,
  mapping = aes(x = TV, y = sales, color = cut(newspaper, breaks = 3))
) +
  geom_point()

ggplot(
  data =  my_data,
  mapping = aes(x = radio, y = sales)
) +
  geom_point()

ggplot(
  data =  my_data,
  mapping = aes(x = radio, y = sales, color = cut(newspaper, breaks = 3))
) +
  geom_point()

ggplot(
  data =  my_data,
  mapping = aes(x = newspaper, y = sales)
) +
  geom_point()

ggplot(
  data =  my_data,
  mapping = aes(x = newspaper, y = sales, color = cut(newspaper, breaks = 3))
) +
  geom_point()

pairs(my_data[, c("X", "X1", "TV", "radio", "newspaper", "sales")])

.