######################################################
#            REGRESIÓN Y ANÁLISIS DE DATOS           #
######################################################

##--Data.
data(airquality)
names(airquality)

airquality$Ozone
airquality$Temp

##-- Positions where Ozone value Na.
ind<-which(is.na(airquality$Ozone))



######################################################
# 1.- Analisys of univariant distribution            #
######################################################

##-- Ozone and Temperature time series.

windows()
par(mfrow=c(1,2))
plot(ts(airquality$Ozone,frequency=30), type="o", ylab="Ozone")
plot(ts(airquality$Temp,frequency=30), type="o", ylab="Temperature")


##-- Regression model.
windows()
lm(airquality$Ozone[-ind]~airquality$Temp[-ind])
plot(lm(airquality$Ozone[-ind]~airquality$Temp[-ind]))
windows()
plot(airquality$Temp[-ind],airquality$Ozone[-ind],main="Regression model")
abline(lm(airquality$Ozone[-ind]~airquality$Temp[-ind]),lwd=2,col="blue")


##-- Ozone and Temperature empirical distribution functions 
#- without positions before were difined (ind).

windows()
par(mfrow=c(1,2))
plot(ecdf(airquality$Ozone[-ind]),main="Ozone")
plot(ecdf(airquality$Temp[-ind]),main="Temperature")

 
##-- Ozone and Temperature density functions 
#- without positions before were difined (ind).
 
par(mfrow=c(1,2))
plot(density(airquality$Ozone[-ind]),main="Ozono")
plot(density(airquality$Temp[-ind]),main="Temperatura")
 

##-- Shapiro-Wilks test is applicated to Ozone and Temperature.
shapiro.test(airquality$Ozone[-ind])
shapiro.test(airquality$Temp[-ind])


##-- Ozone and Temperature box plots
windows()
par(mfrow=c(1,2))
boxplot(airquality$Ozone[-ind],main="Ozone")
boxplot(airquality$Temp[-ind],main="Temperature")


##-- Ozone and Temperature descriptions
summary(airquality$Ozone[-ind])
summary(airquality$Temp[-ind])

#######################################################
############    AUXILIAR FUNCTIONS    #################
#######################################################

#---GAUSIANN KERNEL
K<-function(mas){
#--Gausiann kernel

return(1/sqrt(2*pi)*exp(-mas^2/2))
}

#--FUNCTION WILL BE USE TO LOCAL LINEAR ESTIMATOR

S<-function(t,mas,bandwidth,exponent){
#-- t vector of points where S will be evaluated.
#-- mas values of the independ variable.
#-- bandwidth window 
#-- r exponent.

    return(diag((t(K(outer(mas,t,"-")/bandwidth)))%*%(outer(mas,t,"-")^exponent)))
}

#-- LOCAL LINEAR ESTIMATOR

EstLL<-function(t,mas,y,bandwidth){
#-- This function returns the estimation which was caluted by no-parametric estimator
#-- of local-linear regresion.
#-- t vector of points where S will be evaluated.
#-- x values of the independ variable.
#-- y values of the depend variable.
#-- bandwidth is the window
        
    return( ( t( K( outer(mas,t,"-") /bandwidth) ) * ( S(t,mas,bandwidth,2) - t(outer(mas,t,"-")) * S(t,mas,bandwidth,1) ) /apply( t( K( outer(mas,t,"-") /bandwidth) ) * ( S(t,mas,bandwidth,2) - t(outer(mas,t,"-")) * S(t,mas,bandwidth,1) ),1,sum ) )%*%y)
}

#-- NADARAYA WATSON 

nw<-function(t,mas,y,bandwidth){
#-- This function returns the estimation which was caluted by no-parametric estimator
#-- of Nayadara-Watson regresion.
#-- t vector of points where S will be evaluated.
#-- x values of the independ variable.
#-- y values of the depend variable.
#-- bandwidth is the window

return((K(outer(t,mas,"-")/bandwidth)/apply(K(outer(t,mas,"-")/bandwidth),1,sum))%*%y)

}



########################################################
################### MAIN FUNCTION ######################
########################################################

main<-function(t,mas,y,bandwidth){


