---
title: "Linear Regression in R example"
output: html_notebook
---



Let's create some data: 

```{r}
predictor.var<-runif(15, -10, 10)
predictor.var
```

```{r}
response.var<-3+2*predictor.var+rnorm(15,0,1)
response.var
```

```{r}
my.data<-data.frame(predictor.var,response.var)
my.data
```


```{r}
plot(my.data)
```


Let's use R to create a linear model: 



```{r}
my.model<-lm(my.data$response.var~my.data$predictor.var)
summary(my.model)
```


What we see is that the linear regression predicts that the intercept is beta_0_hat=3.1178 (true beta_0=3)
and predicts that the slope is beta_1_hat=2.1221 (true beta_1=2). 

Notice also that the R^2 value is 0.8269, which is quite high. This suggest that our fit is very good. 


We can plot the data with the fitted least squares line using the abline() function: 

```{r}
plot(my.data)
abline(my.model)
```


```{r}
response.var.2<-runif(15,-10,10)
```

```{r}
my.data.2<-data.frame(predictor.var,response.var.2)
plot(my.data.2)
```


```{r}
my.model.2<-lm(my.data.2$response.var.2~my.data.2$predictor.var)
summary(my.model.2)
```

```{r}
plot(my.data.2)
abline(my.model.2)
```

