---
title: "STAT3340 Ass't 1, Fall 2023, Due ??? 11:59 PM "

author: "Your name"
date: "Your Banner # B00??????"

output:
  pdf_document: default
  word_document: default
---

1. Find the equation of the line which passes through the points (1,1) and (4,5).

   Suppose that you found the slope to be (2/7) and the intercept to be -(1/3), then you would enter your answer as
   $y=- \frac{1}{3} + \frac{2}{7} x$

\
\
\
\
\
\
\


2. Some data gives the summaries: $n = 10$, $\sum_{i=1}^{10}  x_iy_i = 100$, $\sum_{i=1}^{10} x_i = 20$ and $\sum_{i=1}^{10} y_i = 10$. 
   Suppose that the response $y$ is temperature in degrees Celsius. 

   +  $\bar x = 20/10=2$, $\bar y = 10/10=1$, so $S_{xy}=  \sum_{i=1}^{10}  x_iy_i - n \bar x \bar y = 100-10(2)(1) = 80$.


   + 2a) (behaviour of sums under linear transformation) If the response was converted to temperature in degrees Fahrenheit ($y' = 32 +1.8y$), what is $\sum_{i=1}^{10} y'_i$?

$$
\begin{aligned}
  \sum_{i=1}^{10} y'_i & =   \sum_{i=1}^{10} (32+1.8 y_i)  \\
                     & =  ?\\
		     & =?
		     \end{aligned}		     
$$
 (The final numerical answer is sufficient.)

\
\
\
\
\
\
\

   + 2b) (Behaviour of sums of squares under linear transformation of $y$.) If the response was converted to temperature in degrees Fahrenheit, what is $S_{xy'}$?

$$
\begin{aligned}
S_{xy'} & = \sum_{i=1}^{10} (x_i y'_i ) -  (\sum_{i=1}^{10} x_i  \sum_{i=1}^{10} y'_i)/ n  \\
        & = \sum_{i=1}^{10} (x_i (32+1.8 y_i ) - ( \sum_{i=1}^{10} x_i  \sum_{i=1}^{10} y'_i)/n  \\
        & = 32 \sum_{i=1}^{10} x_i + ? \\
 	& = 32 (20) + ? \\
       & = ?
\end{aligned}		     
$$
(The final numerical answer is sufficient.)

\
\
\
\
\
\
\



3.  Where $f(x)=log(x)+e^{x^2}$, derive $f'(x)$, the derivative of $f(x)$.

(Note:  throughout this course $log(x)=log_e(x)=ln(x)$, the natural logarithm.)
\
\
\
\
\
\
\

4.  Where $f(x)=arcsin(\sqrt{x})$, derive $f'(x)$. 

(Note: $arcsin$ is the inverse $sin$ function.  It will be clear later why this derivative is of interest.)
\
\
\
\
\
\
\


5. Where $f(\beta_1) = \sum_{i=1}^n (y_i - \beta_0 - \beta_1 x_i)^2$,
\
find the derivative of $f(\beta_1)$ with respect to $\beta_1$, holding  $\beta_0$, $x_1, x_2, \ldots , x_n$, and $y_1, y_2, \ldots , y_n$ fixed.

\
\
\
\
\
\
\



6.  Where $f(x)=a \times x^b$, simplify $log f(x)$.

\
\
\
\
\
\
\

7. Where $X$ is the $4 \times 3$ matrix

$$ X =  \left [ \begin{array}{c c c}
      1 \,\,+ 2 \,\, +3 \\
      1 \,\,+ 2 \,\, -3 \\
      1 \,\, -2 \,\, +3 \\
      1 \,\, -2 \,\, -3 \\
      \end{array} \right ] $$
      
 calculate $X^T X$, where $X^T$ is the transpose of $X$.
 \
 \
 \
 \
 \
 \
 \

8.  If $X$ is a matrix with $n$ rows and $p$ columns, what are the dimensions (number of rows and number of columns) of $X^T X$?
 \
 \
 \
 \


9.(Inverse of a diagonal matrix.) 

Where  $$ M =  \left [ \begin{array}{c c c}
      1 \,\, 0 \,\, 0 \\
      0 \,\, 16 \,\, 0 \\
      0 \,\, 0 \,\, 36   \end{array} \right ] $$
      
 calculate $M^{-1}$, the inverse of $M$.
 \
 \
 \
 \
 \
 \
 \
 
10. (Inverse of a $2 \times 2$ matrix.) 

Where   $$ M =  \left [ \begin{array}{c  c}
      1 \,\, 2 \\
      3 \,\, 4   \end{array} \right ] $$
      
 calculate  $M^{-1}$, the inverse of $M$.

 \
 \
 \
 \
 \
 \
 \

11.  Where   $$ M =  \left [ \begin{array}{c  c}
      2 \,\, 1 \\
      1 \,\, 3   \end{array} \right ] $$

and

$$ v =  \left [ \begin{array}{c}
      1  \\
      2  \end{array} \right ] $$

calculate the product $M v$.

 \
 \
 \
 \
 \
 \
 \


12. A random sample of 11 elementary school students is selected,
  and each student is measured on a creativity score (x) using a
  well-defined testing instrument and on a task score (y) using a new
  instrument. The task score is the mean time taken to perform several
  hand-eye coordination tasks. The data are as follows.
  
```{r}
x=c(35,37,50,69,84,40,29,42,51,45,31)
y=c(3.9,3.9,6.1,4.3,8.8,2.1,5.7,3.0,7.1,7.3,3.3)
plot(x,y,xlab="Creativity score",ylab="Task score")
```

In R, the "lm" command is used to fit linear regression models.  The following fits the model $y = \beta_0 + \beta_1 x + \epsilon$.

```{r}
lm.out=lm(y~x)
summary(lm.out)
```
The command "fitted" can be used to extract predicted values, also known
as fitted values.  The command "resid" can be used to calculate residuals.

```{r}
fitted=fitted(lm.out)
residuals=resid(lm.out)
```
  
   + 12a) A residual plot makes a plot of the residuals (y axis) vs fitted values (x axis).

The syntax of the plot command will be something like the following.  Correct the
following command to make the residual plot.

```{r}
plot(rnorm(1:10),rnorm(1:10),xlab="Fitted values",ylab="Residuals") #correct the
#plot formula, to plot residuals on y axis and fitted values on x axis
```

   + 12b) Calculate the mean of the residuals to verify it is zero (apart from rounding error).  Modify the
   following code to do this.

```{r}
set.seed(43)  #drop this line
data=rnorm(10); data #drop this line
mean(data) #edit this line to calculate the mean of the residuals. As is, this one averages 10 observations from the standard normal distribution. 
#you should instead be calculating  the mean of the residuals.
round(mean(data),5)  #round your result to five decimal digits.
```

   + 12c) calculate the correlation of the residuals with the fitted values to verify it is also zero (to machine precision).
   The R command to calculate the correlation between $u$ and $v$ is cor(u,v).


```{r}
u=rnorm(10); v=rnorm(10)
cor(u,v)  #edit this line to calculate the correlation of residuals with predicted values.
round(cor(u,v),5) #round the correlation to 5 decimal digits.

```
