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:

advertising <- read.csv( "https://raw.githubusercontent.com/utjimmyx/regression/master/advertising.csv" )

write.csv(advertising, file = "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.csv("Advertising_randomized.csv")
## 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)
## ── 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
head(my_data)
##     X  X1     TV radio newspaper sales
## 1 238  23 180.64 12.81     42.19 12.36
## 2  34 190 275.09 25.14     49.34 12.27
## 3 160  51 325.56 33.86      0.18  9.55
## 4  90  99 305.06 11.02     27.79 28.53
## 5  37  22 166.49 26.51     22.52  3.02
## 6  70  33 264.41 44.39     33.55 10.80
glimpse(my_data)
## Rows: 300
## Columns: 6
## $ X         <int> 238, 34, 160, 90, 37, 70, 212, 38, 116, 108, 184, 152, 56, 5…
## $ X1        <int> 23, 190, 51, 99, 22, 33, 159, 166, 40, 83, 163, 149, 92, 90,…
## $ TV        <dbl> 180.64, 275.09, 325.56, 305.06, 166.49, 264.41, 361.52, 126.…
## $ radio     <dbl> 12.81, 25.14, 33.86, 11.02, 26.51, 44.39, 23.58, 18.16, 40.6…
## $ newspaper <dbl> 42.19, 49.34, 0.18, 27.79, 22.52, 33.55, 9.33, 11.06, 21.67,…
## $ sales     <dbl> 12.36, 12.27, 9.55, 28.53, 3.02, 10.80, 12.70, 15.14, 23.46,…
ggplot(data = my_data)

str(my_data)
## 'data.frame':    300 obs. of  6 variables:
##  $ X        : int  238 34 160 90 37 70 212 38 116 108 ...
##  $ X1       : int  23 190 51 99 22 33 159 166 40 83 ...
##  $ TV       : num  181 275 326 305 166 ...
##  $ radio    : num  12.8 25.1 33.9 11 26.5 ...
##  $ newspaper: num  42.19 49.34 0.18 27.79 22.52 ...
##  $ sales    : num  12.36 12.27 9.55 28.53 3.02 ...
ggplot(
  data = my_data,
  mapping = aes(x = TV, y = sales)
)

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

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

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

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

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.