--- title: "Linear regression" author: "Laurent DELSOL" output: word_document params: n: 30 X_law: 'N(0,1)' x0: 0 a: 3 b: -3 s: 1 x: NULL e: NULL --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) x=params$x e=params$e a=params$a b=params$b s=params$s if (is.null(x)) x=rnorm(30) if (is.null(e)) e=rnorm(30) y=a*x+b+s*e ``` ## Model and data generation We generated `r params$n` data points using R, following the model: Y= `r params$a` X + `r params$b`+ `r params$s`$\epsilon$ with $X \sim$ `r params$X_law` and $\epsilon\sim$ N(0,1). ```{r modele} ``` This yields `r params$n` observations corresponding to a linear model with the equation $$Y=`r a` X+`r b`.$$ ## Scatter plot ```{r nuage, fig.height=5,echo=FALSE} plot(x,y) ``` ## Correlation ```{r} r=cor(x,y) r ``` Please consult the following sections only if the linear relationship is confirmed by the scatter plot and the correlation value. ## Estimated regression line equation ```{r} res=lm(y~x) summary(res) a0=res$coefficients[2] b0=res$coefficients[1] ``` Estimated regression line: Y=`r a0` X+`r b0`. Coefficient of determination: R^2^=`r cor(x,y)^2` (percentage of total variance explained by the linear model) ```{r fig.height=5,echo=FALSE} plot(x,y,main='Regression line(s)') abline(b,a,col='red') abline(b0,a0,col='blue') ``` The true line (in red) and its estimate (in blue). ## Estimation: When X=`r params$x0`, the mean value of Y is estimated as `r predict(res,data.frame(x=params$x0))`.