#plot library
library(ggplot2)
\[E[Y] = 3 - 2 * E[X] = 3 - 2 * 0 = 3\]
\[E[Z] = 2 - E[X] + E[U] = 2 - 0 + 0 = 2\]
\[Var[Y] = (-2)^2 * Var[X] = 4 * 1 = 4\]
\[Var[Z] = (-1)^2 * Var[X] + Var[U] = 1 + .25 = 1.25\]
\[Y \sim N(3, 4)\] \[Z \sim N(2, 1.25)\]
#set seed
set.seed(2023)
#Draw 50 independent realizations of X and U
X = rnorm(50, mean = 0, sd = 1)
U = rnorm(50, mean = 0, sd = 0.5)
#Create 50 realizations of Y and Z
Y = 3 - 2 * X
Z = 2 - X + U
#Create a scatterplot of Y vs X
plot(X, Y, main="Scatterplot of Y vs X", xlab="X", ylab="Y")
#Compute correlation coefficient of Y and X
cor(Y, X)
## [1] -1
#Fit the simple linear regression with response Y and predictor X
lm(Y~X)
##
## Call:
## lm(formula = Y ~ X)
##
## Coefficients:
## (Intercept) X
## 3 -2
#Create scatterplot of Z vs X
plot(X, Z, main="Scatterplot of Z vs X", xlab="X", ylab="Z")
#Compute correlation coefficient of Z and X
cor(Z, X)
## [1] -0.8684625
#Fit the simple linear regression with response Z and predictor X
lm(Z~X)
##
## Call:
## lm(formula = Z ~ X)
##
## Coefficients:
## (Intercept) X
## 1.976 -1.026
#Load dataset record.txt
library(ggplot2)
setwd('/Users/robertpapshev/Downloads')
Diamonds <- read.csv("DiamondRings.csv", header = TRUE)
#Compute correlation coefficient between Carats and Price
cor(Diamonds$Carats, Diamonds$Price)
## [1] 0.9890707
#Fit a simple linear regression
fit <- lm(Price ~ Carats, data = Diamonds)
summary(fit)
##
## Call:
## lm(formula = Price ~ Carats, data = Diamonds)
##
## Residuals:
## Min 1Q Median 3Q Max
## -85.159 -21.448 -0.869 18.972 79.370
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -259.63 17.32 -14.99 <2e-16 ***
## Carats 3721.02 81.79 45.50 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 31.84 on 46 degrees of freedom
## Multiple R-squared: 0.9783, Adjusted R-squared: 0.9778
## F-statistic: 2070 on 1 and 46 DF, p-value: < 2.2e-16
\[Price = -225.809 + 7756.425 * Carats\] ##### On average, an increase in 1 carat will increase price by $7756.43. The R-squared value is 0.848, which means that approximately 84.8% of the variability in Price can be explained by the number of Carats.
#Create a scatterplot and add the estimated regression line into the plot
setwd('/Users/robertpapshev/Downloads')
plot(Diamonds$Carats, Diamonds$Price)
abline(fit, col="red")
#Compute fitted values and residuals, using estimated regression line
fitted.values(fit)
## 1 2 3 4 5 6 7 8
## 372.9483 335.7381 372.9483 410.1586 670.6303 335.7381 298.5278 447.3688
## 9 10 11 12 13 14 15 16
## 521.7893 298.5278 410.1586 782.2611 335.7381 484.5791 596.2098 819.4713
## 17 18 19 20 21 22 23 24
## 186.8971 707.8406 670.6303 745.0508 410.1586 335.7381 372.9483 335.7381
## 25 26 27 28 29 30 31 32
## 372.9483 410.1586 372.9483 410.1586 372.9483 298.5278 372.9483 931.1020
## 33 34 35 36 37 38 39 40
## 931.1020 298.5278 335.7381 335.7381 596.2098 596.2098 372.9483 968.3123
## 41 42 43 44 45 46 47 48
## 670.6303 1042.7328 410.1586 670.6303 670.6303 298.5278 707.8406 298.5278
resid(fit)
## 1 2 3 4 5 6
## -17.9483176 -7.7380691 -22.9483176 -85.1585661 -28.6303057 6.2619309
## 7 8 9 10 11 12
## 23.4721795 37.6311854 -38.7893116 24.4721795 51.8414339 40.7389488
## 13 14 15 16 17 18
## 0.2619309 13.4209369 -1.2098087 40.5287002 36.1029250 -44.8405542
## 19 20 21 22 23 24
## 79.3696943 -25.0508027 57.8414339 9.2619309 -20.9483176 -3.7380691
## 25 26 27 28 29 30
## -19.9483176 27.8414339 -54.9483176 8.8414339 -26.9483176 16.4721795
## 31 32 33 34 35 36
## -22.9483176 -13.1020453 -12.1020453 -0.5278205 3.2619309 2.2619309
## 37 38 39 40 41 42
## -1.2098087 -43.2098087 -27.9483176 -23.3122938 -15.6303057 43.2672091
## 43 44 45 46 47 48
## 32.8414339 7.3696943 4.3696943 -11.5278205 -14.8405542 17.4721795
#Compute the 25th and 75th percentiles of variable Carats on the data set
quantile(Diamonds$Carats, c(0.25, 0.75))
## 25% 75%
## 0.16 0.25
#Use the estimated regression line to estimate the mean of variable Price at each of these two percentiles
a = fit$coefficients[1]
b = fit$coefficients[2]
estimated_price_25th = a + b * quantile(Diamonds$Carats, 0.25)
estimated_price_75th = a + b * quantile(Diamonds$Carats, 0.75)
estimated_price_25th
## (Intercept)
## 335.7381
estimated_price_75th
## (Intercept)
## 670.6303