You are on page 1of 13

Code. Demanda de Video games en Japan.

Manuel Castillo.

library(PerformanceAnalytics)

library(xts) # Time Series

library(zoo) # formato de base de datos

library(tseries) # Time Series theory

library(quantmod) # Datos.

library(ggplot2) #supports visualization of layered graphics

library(tidyverse) # graph

library(broom) # Summary of Regression.

library(forecast)

bike=read.csv("Bike.csv", header=TRUE, sep=",",dec=",")

bike <- read.csv(file ="Bike.csv")

# En ocasiones hay que importalo directamente

View(Bike)

# Ahora, la herramienta fuerte. Seleccionar subsets de Gas.

bike <- Bike %>%

select(c(Time,Rented
,Morning,Afternoon,Temperature,Humidity,WindSpeed,Rainfall,Snowfall,Holiday))

head(bike)
# Scatterplot with regression line,Rented=f(Temperature)

ggplot(data = bike, aes(x = Temperature, y = Rented)) +

geom_point() +

geom_smooth(method = "lm", se = FALSE)

# Scatterplot with regression line,Rented=f(Humidity)

ggplot(data = bike, aes(x = Humidity, y = Rented)) +

geom_point() +

geom_smooth(method = "lm", se = FALSE)

# Scatterplot with regression line,Rented=f(WindSpeed)

ggplot(data = bike, aes(x = WindSpeed, y = Rented)) +

geom_point() +

geom_smooth(method = "lm", se = FALSE)

# Scatterplot with regression line,Rented=f(Rainfall)

ggplot(data = bike, aes(x = Rainfall, y = Rented)) +

geom_point() +

geom_smooth(method = "lm", se = FALSE)

# Scatterplot with regression line,Rented=f(Snowfall)


ggplot(data = bike, aes(x = Snowfall, y = Rented)) +

geom_point() +

geom_smooth(method = "lm", se = FALSE)

# Linear model

lm(Rented ~ Morning + Afternoon + Temperature + Humidity + WindSpeed + Rainfall + Snowfall +


Holiday, data = bike)

mod_bike <- lm(Rented ~ Morning + Afternoon + Temperature + Humidity + WindSpeed + Rainfall +


Snowfall + Holiday, data = bike)

# Show the full output

summary(mod_bike)

ERented <- fitted.values(mod_bike) # Recuerde que tambien es valido: Erented<- predict(mod<-


bike)

plot.ts(ERented) # plot(ERented) Esta es la grafica de puntos.

errorRented <- residuals(mod_bike)

hist(errorRented)

plot.ts(errorRented)
# Ahora voy a darles otro formtatp alarchivo para poder seguir adelante.

bike_data <- data.frame(bike)

bike_data

head(bike_data)

attach(bike_data)

Rented

Morning

RentAndRente=cbind(Rented,ERented)

ts.plot(RentAndRente)

#anova(model) Cuidado es una prueba individual

anova(mod_bike)

# PRUEBA F CALCuLADA

# predicción de Rented cuándo Morning=0 + Afternoon=1+ Temperature=o, Humidity=60,


WindSpeed=1.5, Rainfall=2.5, Snowfall=0 particular.
FRented<- data.frame(Morning=0, Afternoon=1, Temperature=0, Humidity=60 , WindSpeed=2 ,
Rainfall=2.5 , Snowfall=0, Holiday = 0)

FRented

PredictRented<-forecast(mod_bike,FRented)

PredictRented

library(haven)

library(stargazer)

stargazer(mod_bike,title = "Ejemplo de detección de heteroestacidad",type = "text")

residuos<-mod_bike$residuals

hist(residuos)

plot.ts(residuos)

################# TRES PRUEBAS IMPORTANTES ################################

###Es necesario hacer las pruebas estadisticas. PRUEBA DE NORMALIDAD.

library(lmtest)

library(nortest)
# En la prueba de normalidad siguiente la HO:Los errores son normales Vs Ha: No son normales

shapiro.test(residuos) # Esta herramienta es para muestras pequeñas.

# Shapiro-Wilk normality test El Resutado es el siguiente:

# data: residuos

# W = 0.97994, p-value = 3.241e-13 Es menor a 0.05 Por lo tanto Se rechaza Ho.

# Ahora la prueba para muestars pequeñas.

lillie.test(residuos) # Esta prueba es para muestras grandes. El resultado:

# Lilliefors (Kolmogorov-Smirnov) normality test

# data: residuos

# D = 0.067141, p-value < 2.2e-16 Por lo tanto se rechaza la HO. Conclusion no son normales
los errores.

qqnorm(residuos)

qqline(residuos)

##### 2) PRUEBA DE HOMOCEDASTICIDAD

# En la prueba de HOMOCEDASTICITY

# HO:Los errores SON HOMOCEDASTICOS Vs HA: No son HOMOCEDASTICOS


# Despues de calcular el modelo lineal lm(), podemos llevar a cabo la Prueba de Breusch–Pagan,

# para esto, usamos la siguiente sintaxis:

bptest(mod_bike) # Los reultados son los siguientes:

# studentized Breusch-Pagan test

# data: mod_bike

# BP = 393.2, df = 8, p-value < 2.2e-16 Por lo tanto, no rechazamos Ho Entonces hay


heterocedasticidad

# Se tiene otra prueba: Golfed-Quand

gqtest(mod_bike,order.by =~ Morning + Afternoon + Temperature + Humidity + WindSpeed +


Rainfall + Snowfall + Holiday, data = bike)

# Goldfeld-Quandt test

# data: mod_bike

# GQ = 0.619, df1 = 705, df2 = 705, p-value = 1

# alternative hypothesis: variance increases from segment 1 to 2

# Alternative hypothesis: variance increases from segment 1 to 2

########### 3) PRUEBA DE AUTOCORRELACIÓN DE LOS ERRORES.

# HO: No Hay Autocorrelación


# El estadístico de Durbin Watson es una prueba para autocorrelación.

# El La estadístico DW varía de cero a cuatro. 0<DW<4.

# con un valor de 2.0 indicando autocorrelación cero.

# Valores debajo de 2.0 significa que hay autocorrelación positiva.

# y superior a 2.0 indica autocorrelación negativa

dwtest(mod_bike)

# Los resultados son los siguientes:

# data: mod_bike

# DW = 0.57243, p-value < 2.2e-16

# alternative hypothesis: true autocorrelation is greater than 0

################################################################################

###############################################################################

library(PerformanceAnalytics)

library(xts) # Time Series

library(zoo) # formato de base de datos

library(tseries) # Time Series theory

library(quantmod) # Datos.

library(ggplot2) #supports visualization of layered graphics

library(tidyverse) # graph
library(broom) # Summary of Regression.

library(forecast)

bike=read.csv("VideoGame.csv", header=TRUE, sep=",",dec=",")

Video <- read.csv(file ="VideoGame.csv")

# En ocasiones hay que importalo directamente

View(VideoG)

# Ahora, la herramienta fuerte. Seleccionar subsets de Gas.

VideoG <- VideoG %>%

select(c(Year,NINTENDO, MICROSOFT, SONY, NA_Sales, EU_Sales, JP_Sales))

head(VideoG)

# Scatterplot with regression line,Rented=f(Temperature)

ggplot(data = VideoG, aes(x = NA_Sales, y = JP_Sales)) +

geom_point() +

geom_smooth(method = "lm", se = FALSE)

# Scatterplot with regression line,Rented=f(Humidity)

ggplot(data = VideoG, aes(x = EU_Sales, y = JP_Sales)) +


geom_point() +

geom_smooth(method = "lm", se = FALSE)

# Linear model

lm(JP_Sales ~ NINTENDO + MICROSOFT + SONY + NA_Sales + EU_Sales, data = VideoG)

mod_video <- lm(JP_Sales ~ NINTENDO + MICROSOFT + SONY + NA_Sales + EU_Sales, data =


VideoG)

# Show the full output

summary(mod_video)

VideoGE <- fitted.values(mod_video)

plot.ts(VideoGE) # plot(ERented) Esta es la grafica de puntos.

errorVg <- residuals(mod_video)

hist(errorVg)

plot.ts(errorVg)

################# TRES PRUEBAS IMPORTANTES ################################


###Es necesario hacer las pruebas estadisticas. PRUEBA DE NORMALIDAD.

library(lmtest)

library(nortest)

# En la prueba de normalidad siguiente la HO:Los errores son normales Vs Ha: No son normales

shapiro.test(errorVg) # Esta herramienta es para muestras pequeñas.

# Shapiro-Wilk normality test El Resutado es el siguiente:

# Shapiro-Wilk normality test

# data: errorVg

# W = 0.78636, p-value < 2.2e-16

# Ahora la prueba para muestars pequeñas.

lillie.test(errorVg) # Esta prueba es para muestras grandes. El resultado:

# Lilliefors (Kolmogorov-Smirnov) normality test

# data: errorVg

# D = 0.19629, p-value < 2.2e-16 Por lo tanto se rechaza la HO. Conclusion no son normales los
errores.

qqnorm(residuos)
qqline(residuos)

##### 2) PRUEBA DE HOMOCEDASTICIDAD

# En la prueba de HOMOCEDASTICITY

# HO:Los errores SON HOMOCEDASTICOS Vs HA: No son HOMOCEDASTICOS

# Despues de calcular el modelo lineal lm(), podemos llevar a cabo la Prueba de Breusch–Pagan,

# para esto, usamos la siguiente sintaxis:

bptest(mod_video) # Los reultados son los siguientes:

# studentized Breusch-Pagan test

# data: mod_video

# BP = 186.51, df = 5, p-value < 2.2e-16 Por lo tanto, no rechazamos Ho Entonces hay


heterocedasticidad

# Se tiene otra prueba: Goldfeld-Quand

gqtest(mod_video,order.by = ~ NINTENDO + MICROSOFT + SONY + NA_Sales + EU_Sales, data =


VideoG)

# Goldfeld-Quandt test

# data: mod_video

# GQ = 1.961, df1 = 727, df2 = 726, p-value < 2.2e-16


# alternative hypothesis: variance increases from segment 1 to 2

# Alternative hypothesis: variance increases from segment 1 to 2

########### 3) PRUEBA DE AUTOCORRELACIÓN DE LOS ERRORES.

# HO: No Hay Autocorrelación

# El estadístico de Durbin Watson es una prueba para autocorrelación.

# El La estadístico DW varía de cero a cuatro. 0<DW<4.

# con un valor de 2.0 indicando autocorrelación cero.

# Valores debajo de 2.0 significa que hay autocorrelación positiva.

# y superior a 2.0 indica autocorrelación negativa

dwtest(mod_video)

# Los resultados son los siguientes:

# data: mod_bike

# DW = 0.57243, p-value < 2.2e-16

# alternative hypothesis: true autocorrelation is greater than 0

You might also like