<jupyter_start><jupyter_text>TD 3<jupyter_code>sigmoid <- function(x) {
  return (exp(x)/(1+exp(x)))
}
df <- read.table("titanic.csv", sep=";", header=TRUE, dec=",", na.strings = "")
df$pclass <- factor(df$pclass)
df$survived <- factor(df$survived)
df <- df[complete.cases(df),]
sigmoid()<jupyter_output><empty_output><jupyter_text>Partie 3  Q1 (a) L'odd est défini par $\text{odd}(x)=\frac{\mathbb{P}(Y=1|x)}{\mathbb{P}(Y=0|x)}=\frac{\pi(x)}{1-\pi(x)}$<jupyter_code>df$survived <- relevel(df$survived, ref="0")
surv.null <- glm(survived ~ 1, family = binomial, data = df)
surv.null<jupyter_output><empty_output><jupyter_text>Dans le cas d'une régression logistique avec un seul coefficient on a $\pi(x)=\sigma(\beta_{0})$<jupyter_code>sigmoid(-0.376)/(1-sigmoid(-0.376))
exp(coef(surv.null))<jupyter_output><empty_output><jupyter_text>On a 0.68 fois plus de chance de survivre que de mourir au naufrage du titanic.  Q2<jupyter_code>df$survived <- relevel(df$survived, ref="0")
df$sex<- relevel(df$sex, ref='male')
surv.sex <- glm(survived ~ sex, family = binomial, data = df)
summary(surv.sex)
sigmoid(1.1055)
sigmoid(-1.35431)
a=sigmoid(1.1055)/(1-sigmoid(1.1055))
b=sigmoid(-1.35431)/(1-sigmoid(1.35431))
exp(2.45984)
(0.75/0.25)/(0.20/0.80)<jupyter_output><empty_output><jupyter_text>(a)<jupyter_code>1-pchisq(surv.sex$null.deviance-surv.sex$deviance,surv.sex$df.null-surv.sex$df.residual)<jupyter_output><empty_output><jupyter_text>On rejette l'hypothèse nulle : le modèle est statistiquement valable Pour une variable binaire $OR=\frac{\frac{\pi(1)}{1-\pi(1)}}{\frac{\pi(0)}{1-\pi(0)}}$$log(OR)=log(\frac{\pi(1)}{1-\pi(1)})-log(\frac{\pi(0)}{1-\pi(0)})=logit(\pi(1))-logit(\pi(0))$Ici notre modèle s'écrit $\pi(x)=\sigma(\beta_{0}+\beta_{1}\mathbb{I}_{\text{sex=homme}}(x))$Alors $\pi(1)=\sigma(\beta_{0}+\beta_{1})$ et $\pi(0)=\sigma(\beta_{0})$Alors $log(OR)=logit(\pi(1))-logit(\pi(0))=\beta_{1}$D'où $OR=exp(\beta_{1})$ (a)<jupyter_code>a=sigmoid(-1.35431)/(1-sigmoid(-1.35431))
b=sigmoid(-1.35431+2.45984)/(1-sigmoid(-1.35431+2.45984))
b/a
exp(2.45984)<jupyter_output><empty_output><jupyter_text>(b)on a 11,7 fois plus de chances de survivre au naufrage du titanic lorsqu'on est une femme que lorsqu'on n'en est pas une.  Q3 (a) $\pi(x)=\sigma(\beta_{0}+\beta_{1}\mathbb{I}_{\text{pclass=2}}(x)+\beta_{2}\mathbb{I}_{\text{pclass=3}}(x))$<jupyter_code>df$pclass<- relevel(df$pclass, ref='3')
surv.pclass <- glm(survived ~ pclass, family = binomial, data = df)
summary(surv.pclass)
exp(1.5910)
exp(0.7996)
1-pchisq(surv.pclass$null.deviance-surv.pclass$deviance,surv.pclass$df.null-surv.pclass$df.residual)
1/0.91<jupyter_output><empty_output><jupyter_text>$OR(class2)=\frac{\frac{\pi(class2)}{1-\pi(class2)}}{\frac{\pi(class1)}{1-\pi(class1)}}$$OR(class3)=\frac{\frac{\pi(class3)}{1-\pi(class3)}}{\frac{\pi(class1)}{1-\pi(class1)}}$<jupyter_code>a=sigmoid(0.5527 -0.7913)/(1-sigmoid(0.5527 -0.7913))
b=sigmoid(0.5527 -1.5910)/(1-sigmoid(0.5527 -1.5910))
c=sigmoid(0.5527)/(1-sigmoid(0.5527))
a/c
exp(-0.7913)
b/c
exp(-1.5910)
1/0.20<jupyter_output><empty_output><jupyter_text>(b) on a 0.45 fois plus de chance de survivre au naufrage dans une cabine de classe 2 que dans une cabine de classe 1 -> on a 2.22 fois plus de chance de mourir pendant le naufrage dans une cabine de classe 2 que dans une cabine de classe 1.on a 0.20 fois plus de chance de survivre au naufrage dans une cabine de classe 3 que dans une cabine de classe 1 -> on a 5 fois plus de chance de mourir pendant le naufrage dans une cabine de classe 3 que dans une cabine de classe 1.  Q4 (a) Le modèle s'écrit $\pi(\text{age})=\sigma(\beta_{0}+\beta_{1}\text{age})=\frac{e^{\beta_{0}+\beta_{1}\text{age}}}{1+e^{\beta_{0}+\beta_{1}\text{age}}}$ où $\text{age}$ est l'âge<jupyter_code>df$survived <- relevel(df$survived, ref="0")
surv.age <- glm(survived ~ age, family = binomial, data = df)
summary(surv.age)
exp(-0.008413)
1/exp(10*-0.008413)<jupyter_output><empty_output><jupyter_text>on a 0.99 fois plus de chance de survivre avec 1 an de plus  (b) $OR = exp(10*b_{1})$. En effet, on a :pour x = a : $logit(\pi(a)) = \beta_{0}+\beta_{1}*a$pour x = a+10 : $logit(\pi(a+10)) = \beta_{0}+\beta_{1}*a + \beta_{1}*10$$OR = (\pi(a+10)/(1-\pi(a+10))) / (\pi(a)/(1-\pi(a))) = exp(\beta_{0})*exp(\beta_{1}*a)*exp(\beta_{1}*10) / exp(\beta_{0})*exp(\beta_{1}*a) = exp(\beta_{1}*10)$ (c)<jupyter_code>1-pchisq(surv.age$null.deviance-surv.age$deviance,surv.age$df.null-surv.age$df.residual)<jupyter_output><empty_output><jupyter_text>(e)<jupyter_code>confint(surv.age)
exp(confint(surv.age))<jupyter_output>Waiting for profiling to be done...<jupyter_text>Partie 4<jupyter_code>bornes <- c(quantile(df$age, probs = seq(0, 1, by = 0.2)))
bornes
df$age_discret <- cut(df$age, breaks=bornes)
df$age_discret
pix = table(df$age_discret, df$survived)/rowSums(table(df$age_discret, df$survived))
pix
logit=log((pix[,2])/(1-pix[,2]))
bornes <- c(quantile(df$age, probs = seq(0, 1, by = 0.2)))
df$age_discret <- cut(df$age, breaks=bornes)
pix = table(df$age_discret, df$survived)/rowSums(table(df$age_discret, df$survived))
logit=log((pix[,2])/(1-pix[,2]))
x = (c(0,19,25,31,42)+c(19,25,31,42,80))/2
plot(x, logit)
lines(x, lm(logit~x)$fit)
surv.age.discret <- glm(survived ~ age_discret, family = binomial, data = df)
summary(surv.age.discret)
1-pchisq(surv.age.discret$null.deviance-surv.age.discret$deviance,surv.age.discret$df.null-surv.age.discret$df.residual)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2213054638797213, 'fraction_numerical': 0.06747341400806747, 'mean_word_length': 6.421768707482993, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '37995', 'license': 'MIT', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2426, 'n_words': 369, 'path': 'cours/reg_log_stid/TP/corrections/td3_reglog_2020.ipynb', 'repo_name': 'tvayer/tvayer.github.io', 'size': '5454'}