Question 1
data('mtcars')
cor.test(mtcars$mpg , mtcars$wt)
##
## Pearson's product-moment correlation
##
## data: mtcars$mpg and mtcars$wt
## t = -9.559, df = 30, p-value = 1.294e-10
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.9338264 -0.7440872
## sample estimates:
## cor
## -0.8676594
# The more a car weighs, the less MPG it gets
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
# Load necessary package
library(ggplot2)
# Create scatter plot
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
labs(title = "The more a car weighs, the less MPG it gets",
x = "Weight (1000 lbs)",
y = "Miles Per Gallon") +
theme_minimal()

# Load the plotly package
library(plotly)
# Create the scatter plot with a trendline
plot_ly(mtcars, x = ~wt, y = ~mpg, type = 'scatter', mode = 'markers', name = 'Data') %>%
add_trace(x = ~wt, y = fitted(lm(mpg ~ wt, data = mtcars)),
mode = 'lines', name = 'Trendline', line = list(color = 'blue'))