#Examen PyE Unidad 4
#Elba Maria Ybarra

#2.Grafico Pairs
library(readxl)
rides <- read_excel("rides.xlsx")
View(rides)
names(rides)
## [1] "Rides"      "Price"      "Population"
pairs(rides)

cor(rides)
##                 Rides      Price Population
## Rides       1.0000000 -0.9659529  0.8975653
## Price      -0.9659529  1.0000000 -0.9148968
## Population  0.8975653 -0.9148968  1.0000000
#3.Regresion Lineal
raite <- lm(Rides ~ Population, data = rides)
summary(raite)
## 
## Call:
## lm(formula = Rides ~ Population, data = rides)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -31609.6  -2735.3     84.5   5610.8  14243.7 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -3.137e+05  4.658e+04  -6.736 4.65e-07 ***
## Population   2.820e-01  2.770e-02  10.179 2.24e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 9577 on 25 degrees of freedom
## Multiple R-squared:  0.8056, Adjusted R-squared:  0.7978 
## F-statistic: 103.6 on 1 and 25 DF,  p-value: 2.238e-10
plot(rides$Population, rides$Rides, xlab = "Population", ylab = "Rides")
abline(raite)

#4.Predicciones
viaje <- data.frame(Population = seq(115,152))
predict(raite, viaje)
##         1         2         3         4         5         6         7 
## -313699.6 -313699.3 -313699.1 -313698.8 -313698.5 -313698.2 -313697.9 
##         8         9        10        11        12        13        14 
## -313697.7 -313697.4 -313697.1 -313696.8 -313696.5 -313696.2 -313696.0 
##        15        16        17        18        19        20        21 
## -313695.7 -313695.4 -313695.1 -313694.8 -313694.6 -313694.3 -313694.0 
##        22        23        24        25        26        27        28 
## -313693.7 -313693.4 -313693.1 -313692.9 -313692.6 -313692.3 -313692.0 
##        29        30        31        32        33        34        35 
## -313691.7 -313691.5 -313691.2 -313690.9 -313690.6 -313690.3 -313690.0 
##        36        37        38 
## -313689.8 -313689.5 -313689.2
#5.Intervalos de Confianza
confint(raite)
##                     2.5 %        97.5 %
## (Intercept) -4.096617e+05 -2.178024e+05
## Population   2.249278e-01  3.390327e-01
ic <- predict(raite, viaje, interval = "confidence")
lines(viaje$Population, ic[,2], lty=2)
lines(viaje$Population, ic[,3], lty=2)
ic <- predict(raite, viaje, interval = "prediction")
lines(viaje$Population, ic[,2], lty=2, col = "red")
lines(viaje$Population, ic[,3], lty=2, col = "red")

#6. ANOVA
anova(raite)
## Analysis of Variance Table
## 
## Response: Rides
##            Df     Sum Sq    Mean Sq F value    Pr(>F)    
## Population  1 9504065441 9504065441  103.62 2.238e-10 ***
## Residuals  25 2293089963   91723599                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#7. Grafico de Residuos
residuos <- rstandard(raite)
valores.ajustados <- fitted(raite)
plot(valores.ajustados, residuos)

#8. Grafico Quantil-Quantil
qqnorm(residuos)
qqline(residuos)