hw7 by Yves Gardiner

Question #1: Run tibble code, create a graph

set.seed(25)
scatter_490data <- tibble(
  x_var = runif(100, min = 0, max = 25),
  y_var = log2(x_var) + rnorm(100)
)

graph<-ggplot(scatter_490data, aes(x = x_var, y = y_var)) +
  geom_point(shape = 22, size = 2, color = "blue") +
  labs(
    x = "X Variable",
    y = "Y Variable",
    title = "Question 1"
  )+ theme_minimal()

Question 2, parts A - E

part A: find the value of column 2 row 35

Show the code
print(scatter_490data[35,2])
# A tibble: 1 × 1
  y_var
  <dbl>
1  4.49

part B: Filter all rows in which X_var > 10

[1] 52

part C: Filter all rows in which X_var > 10 and y var >5

[1] 6

part D: Filter all rows in which X_var > 15 or y_var <4

[1] 55

part E: add max of x_var with min of y_var

[1] 22.41161

Question 3, parts A-C

setup:

part A:


Call:
lm(formula = Final ~ HW, data = Q3)

Residuals:
    Min      1Q  Median      3Q     Max 
-18.701  -2.901   1.914   4.288  10.332 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 33.18413    6.11797   5.424 3.74e-05 ***
HW           0.63737    0.07899   8.069 2.17e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 7.422 on 18 degrees of freedom
Multiple R-squared:  0.7834,    Adjusted R-squared:  0.7714 
F-statistic: 65.11 on 1 and 18 DF,  p-value: 2.168e-07

The P value of HW is

 2.168e-07

or .0000002167

based on

\(p = 2 \times \left(1 - \text{cdf}\left(|t|, df\right)\right)\)

(I)

based on Pearsons formula, this the probability that the two variables are not correlated and the relationship we are seeing is null and statistically insignificant. the fact that our P value is extremely small is good evidence that a statistically significant relationship exists between HW and Final. IE your HW scores predict your final scores.

(II)

using

\(r = \frac{\sum{(X_i - \bar{X})(Y_i - \bar{Y})}}{\sqrt{\sum{(X_i - \bar{X})^2} \sum{(Y_i - \bar{Y})^2}}}\)

\(R^2\) =.7834 for the model

\(R^2\) (also known as the coefficient of determination)indicates how well the HW variable predicts the variance of the Final data on a scale from 0-1

0 meaning it cannot account for any variability to 1 in which it can predict all variability. Our R squared tells us that our HW data can account for about 78% of the variance of Final data, but about ~22% of final data is not predicted by HW data. In other terms the R^2 metric is helpful for understanding the fitness of a model because it can help us understand how well a model or independent variable tracks the change in a dependent variable. in this case though, our analysis is of a linear regression so HW_DATA predicts FINAL_DATA just as much as FINAL_DATA predicts HW_DATA.

part B: Slope and Intercept


Call:
lm(formula = Final ~ HW, data = Q3)

Residuals:
    Min      1Q  Median      3Q     Max 
-18.701  -2.901   1.914   4.288  10.332 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 33.18413    6.11797   5.424 3.74e-05 ***
HW           0.63737    0.07899   8.069 2.17e-07 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 7.422 on 18 degrees of freedom
Multiple R-squared:  0.7834,    Adjusted R-squared:  0.7714 
F-statistic: 65.11 on 1 and 18 DF,  p-value: 2.168e-07

slope = .637 intercept =33.184

the formula for how our regression line is determined is is

\(y = m(x) + b\)

The intercept equates to our b value of our regression line formula and when our x value is 0 our y value must be 33.184

\(y = .637(0) + 33.184\)

Slope: the slope is our M value in the slope formula, it is our multiplier for our y value, as our x changes we multiply it by our slope and add our intercept to determine our y value in a linear fashion. our slope is what gives our line its angle. within the context of prediction, for every 1 unit increase in HW Score, our Final Score increases by .637 .

part C: predicted final score of hw score = 80

[1] 84.144

Question 4:

Part A: for loop 1,2,3,4,5

[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
1  2  3  4  5  

Part B: nested version

1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  1  2  3  4  5  

Part C penultimate

1  2  3  4  5  2  4  6  8  10  3  6  9  12  15  4  8  12  16  20  5  10  15  20  25  6  12  18  24  30  7  14  21  28  35  8  16  24  32  40  9  18  27  36  45  10  20  30  40  50  11  22  33  44  55  12  24  36  48  60  13  26  39  52  65  14  28  42  56  70  15  30  45  60  75  16  32  48  64  80  17  34  51  68  85  18  36  54  72  90  19  38  57  76  95  20  40  60  80  100  

The Penultimate print is 80 because at the i variable is 20 and the j variable is 4. we are going through the last loop of i in 1:20 and within that the second to last loop of j in 1:5.

Part D selling

[1] "Ecstasy!"

Question 5: Factorial

#elegant solution
findfactorial <- function(n) {
total <-1
for(i in 1:n)
{
 total<-total*i
}
n<-total
return(n)
}

findfactorial(6)
[1] 720

test

#convoluted solution
findF <- function(n) {
  vec1<-c(1:n+1)
  vec2<-c(1:n)
  for(i in 1:n)
  {
    vec1[i+1]<-vec1[i]*vec1[i+1]
    vec2[i]<-vec1[i]
    
  }
  
  print(vec2[n-1])
}
findF(6)
[1] 720