basepl<- read.csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vR1rmwvUzt8Udd9cpwqLwjcf72FJZ4NCEwb9hLE6VRxcncx2kU9ALtc24VcxlGh4mIvHXtpsWmyI7hb/pub?output=csv",T, sep = ",")
attach(basepl) 
problin<- lm(ins~retire+age+hstatusg+hhincome+educyear+married+hisp)

summary(problin$fitted.values)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -0.1557  0.3055  0.4074  0.3871  0.4736  1.1972
summary(problin)
## 
## Call:
## lm(formula = ins ~ retire + age + hstatusg + hhincome + educyear + 
##     married + hisp)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.1233 -0.4065 -0.2291  0.5298  1.0341 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.1270857  0.1605628   0.792  0.42871    
## retire       0.0408508  0.0182197   2.242  0.02502 *  
## age         -0.0028955  0.0024189  -1.197  0.23138    
## hstatusg     0.0655583  0.0194531   3.370  0.00076 ***
## hhincome     0.0004921  0.0001375   3.579  0.00035 ***
## educyear     0.0233686  0.0028672   8.150 5.15e-16 ***
## married      0.1234699  0.0193618   6.377 2.07e-10 ***
## hisp        -0.1210059  0.0336660  -3.594  0.00033 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.4671 on 3198 degrees of freedom
## Multiple R-squared:  0.08262,    Adjusted R-squared:  0.08061 
## F-statistic: 41.14 on 7 and 3198 DF,  p-value: < 2.2e-16
#####
logit<- glm(ins~ retire+age+hstatusg+hhincome+educyear+married+hisp, family=(binomial(link=logit)))
logit
## 
## Call:  glm(formula = ins ~ retire + age + hstatusg + hhincome + educyear + 
##     married + hisp, family = (binomial(link = logit)))
## 
## Coefficients:
## (Intercept)       retire          age     hstatusg     hhincome     educyear  
##   -1.715578     0.196930    -0.014596     0.312265     0.002304     0.114263  
##     married         hisp  
##    0.578636    -0.810306  
## 
## Degrees of Freedom: 3205 Total (i.e. Null);  3198 Residual
## Null Deviance:       4280 
## Residual Deviance: 3990  AIC: 4006
exp(logit$coefficients)
## (Intercept)      retire         age    hstatusg    hhincome    educyear 
##   0.1798597   1.2176584   0.9855105   1.3665173   1.0023063   1.1210464 
##     married        hisp 
##   1.7836040   0.4447220
##efectos marginales 
logitvec <- mean(dlogis(predict(logit, type = "link")))
logitvec * coef(logit)
##   (Intercept)        retire           age      hstatusg      hhincome 
## -0.3725232720  0.0427616020 -0.0031692953  0.0678057706  0.0005002078 
##      educyear       married          hisp 
##  0.0248111432  0.1256458981 -0.1759510453
eta=(-1.715578+  ( 0.196930 *0) +(62*-0.014596) +(0*0.312265) +(0.002304*0)+(0.114263*12)+(0.578636 *0)+(-0.810306* 0) )
PX=(exp(eta)/(1+exp(eta)))
###Si está retirado la probabilidad de tener un seguro médico aumenta en 4.2%

logitvec * coef(logit)
##   (Intercept)        retire           age      hstatusg      hhincome 
## -0.3725232720  0.0427616020 -0.0031692953  0.0678057706  0.0005002078 
##      educyear       married          hisp 
##  0.0248111432  0.1256458981 -0.1759510453
####Probit 
probit<- glm(ins~ retire+age+hstatusg+hhincome+educyear+married+hisp, family=(binomial(link=probit)))
(probit$coefficients)*1.6
##  (Intercept)       retire          age     hstatusg     hhincome     educyear 
## -1.710947627  0.189364238 -0.014191073  0.316385757  0.001972254  0.113198684 
##      married         hisp 
##  0.579740240 -0.756982952
(problin$coefficients)*2.5
##  (Intercept)       retire          age     hstatusg     hhincome     educyear 
##  0.317714239  0.102127043 -0.007238866  0.163895854  0.001230219  0.058421574 
##      married         hisp 
##  0.308674700 -0.302514837
probit$coefficients
##  (Intercept)       retire          age     hstatusg     hhincome     educyear 
## -1.069342267  0.118352649 -0.008869421  0.197741098  0.001232659  0.070749177 
##      married         hisp 
##  0.362337650 -0.473114345
head(predict(probit, type = "response"))
##         1         2         3         4         5         6 
## 0.2205740 0.2285447 0.3573040 0.1809490 0.1810033 0.4634186
###INDIVIDUO1
###probabilidad de los individuos 
z1=( -1.069342  -(0.008869 *62)+(0.070749*12))
dnorm(z1)
## [1] 0.2965418
###
dnorm(0.008869421)# interpretación de la maginitud del beta,
## [1] 0.3989266
###INDIVIDUO2
z2= (-1.069342267 -0.008869421*59 +0.070749177*12)
dnorm(z2)
## [1] 0.3025694
## INDIVIDUO3
z3=(-1.069342267 +0.118352649 -0.008869421*60 +0.197741098 + 0.070749177 *13  )
dnorm(z3)
## [1] 0.3731415
## INDIVIDUO4
z4= (-1.069342267- 0.008869421*62 +0.070749177*10)
dnorm(z4)
## [1] 0.2632669
###Interpretacion resultados
#Para poder hayar la probabilidad de exito, necesitamos la informacion de las x de ese individuo, solo debemos reemplazar donde existe un valor de x, esto quiere decir donde sea diferente a 0. Para el individuo 1, 2 y 4 tomamos el intercepto, mas el coeficiente de la edad, por la edad del individuo, mas el coeficeinte de la educacion por la educacion. Para el individuo #3 es difernete, ya que el si esta retirado, tambien tiene hstatusg y educacion, así que se le incluyen estas x. 
#Individuo1: 0.2965418 Para el individuo #1 la probabilidad de tener un seguro medico es de 0,29 
#Individuo2: 0.3025694 Para el individuo #2 la probabilidad de tener un seguro medico es de 0,30 
#Individuo3: 0.3731415 Para el individuo #3 la probabilidad de tener un seguro medico es de 0,37
#Individuo4: 0.2632669 Para el individuo #4 la probabilidad de tener un seguro medico es de 0,26

##Conclusion: Podemos observar que el individuo 3 es el que tiene mayor probabilidad de tener un seguro medico, esto se debe a que se tomaron mas variables. 

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00