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   174    90  53.3 41.7       0.18 18.2 
## 2    66   173  81.6 15.2      67.1  16.6 
## 3   155   207 213.  40.6       2.82 12.2 
## 4   106    97 142.   9.07     21.8  10   
## 5   143   193 216.  21.6      32.4   8.68
## 6    59   133  93.0 34.0      68.2  10.6
glimpse(my_data)
## Rows: 250
## Columns: 6
## $ X         <dbl> 174, 66, 155, 106, 143, 59, 126, 157, 78, 58, 179, 5, 216, 1…
## $ X1        <dbl> 90, 173, 207, 97, 193, 133, 122, 131, 64, 56, 97, 139, 188, …
## $ TV        <dbl> 53.27, 81.63, 212.96, 141.93, 215.65, 92.97, 163.97, 271.65,…
## $ radio     <dbl> 41.70, 15.16, 40.57, 9.07, 21.65, 34.03, 39.88, 37.54, 36.45…
## $ newspaper <dbl> 0.18, 67.08, 2.82, 21.85, 32.38, 68.24, 17.00, 11.08, 4.23, …
## $ sales     <dbl> 18.25, 16.64, 12.17, 10.00, 8.68, 10.63, 22.21, 7.55, 20.36,…
library(ggplot2)

ggplot(data = my_data)

str(my_data)
## tibble [250 × 6] (S3: tbl_df/tbl/data.frame)
##  $ X        : num [1:250] 174 66 155 106 143 59 126 157 78 58 ...
##  $ X1       : num [1:250] 90 173 207 97 193 133 122 131 64 56 ...
##  $ TV       : num [1:250] 53.3 81.6 213 141.9 215.7 ...
##  $ radio    : num [1:250] 41.7 15.16 40.57 9.07 21.65 ...
##  $ newspaper: num [1:250] 0.18 67.08 2.82 21.85 32.38 ...
##  $ sales    : num [1:250] 18.25 16.64 12.17 10 8.68 ...
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 = 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.