Activity 1.1 - vector basics

SUBMISSION INSTRUCTIONS:

  1. Complete this activity, rendering periodically to html to view your output. Make sure you have this .qmd file in a folder with the Activity subdirectories!
  2. When complete, publish to either Posit Connect Cloud or Rpubs
  3. Submit a link to your rendered html and your .qmd to D2L.

Question 1

Consider the 3-dimensional vector \(w = c(-5, 2, -3)\).

A)

Find \(||w||_1\), the L1 (aka taxicab or Manhattan) norm of this vector. Show your work!

\(||w||_1\) = 5 + 2 + 3 = 10

B)

Find \(||w||_2\), the L2 (aka Euclidean) norm of this vector. Show your work!

sqrt(5^2 + 2^2 +3^2)
[1] 6.164414

xC)

Create the vector in R. Use R to verify your computations above.

Q1 <- c(-5, 2, -3)
##L1 
sum(abs(Q1))
[1] 10
##L2
sqrt(sum(Q1^2))
[1] 6.164414

Question 2

Consider the iris data set, which comes packaged with a standard R installation. In the code below I have selected only the numeric variables from this data set:

library(tidyverse)
(iris_use <- iris 
  %>% select(-Species)
) %>% head()
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1          5.1         3.5          1.4         0.2
2          4.9         3.0          1.4         0.2
3          4.7         3.2          1.3         0.2
4          4.6         3.1          1.5         0.2
5          5.0         3.6          1.4         0.2
6          5.4         3.9          1.7         0.4

A)

How many vectors are there in this new data set, and what is the dimension of each vector?

150 vectors, 4 dimensional

B)

Find the mean and standard deviation vectors.

apply(iris_use, 2, FUN = mean)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
    5.843333     3.057333     3.758000     1.199333 
apply(iris_use, 2, FUN = sd)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
   0.8280661    0.4358663    1.7652982    0.7622377 

C)

Mean-center and scale the data set. Verify that the mean and standard deviation vectors of the scaled data set are \(\vec 0\) and \(\vec 1\), respectively.

iris_cenetered <- scale(iris_use, center = TRUE, scale = TRUE)
apply(iris_cenetered, 2, FUN = mean)
 Sepal.Length   Sepal.Width  Petal.Length   Petal.Width 
-2.318423e-15 -1.684023e-15 -1.577997e-15 -8.829974e-16 
apply(iris_cenetered, 2, FUN = sd)
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
           1            1            1            1 

D)

Find the centered/scaled vectors for the first two irises. Find the L1 and L2 distances between these two irises “from scratch,” then verify your answer using dist.

1 = –0.898, 1.016, -1.336, -1.311

2 = -1.139, -0.132, -1.336, -1.311

L1= 0.241+1.147+0+0 = 1.388

L2 = sqrt(0.2412 + 1.1472 + 02+02​) = sqrt(0.0581+ 1.3156)​ = sqrt(1.37) ​= 1.172

iris_dist <- dist(iris_cenetered, method = "euclidean")
head(iris_dist)
[1] 1.1722914 0.8427840 1.0999999 0.2592702 1.0349769 0.6591230
distance_df <- (iris_dist
                %>% as.matrix                             
                %>% data.frame                             
                %>% rownames_to_column('FlowerA')          
                %>% pivot_longer(cols = -FlowerA,
                                 names_to = 'FlowerB',
                                 values_to = 'Distance')  
)

head(distance_df)
# A tibble: 6 × 3
  FlowerA FlowerB Distance
  <chr>   <chr>      <dbl>
1 1       X1         0    
2 1       X2         1.17 
3 1       X3         0.843
4 1       X4         1.10 
5 1       X5         0.259
6 1       X6         1.03 

Question 3

Reconsider the USairpollution data. If you don’t have the HSAUR2 package installed, run install.packages(HSAUR2) once in your console to install the package. Do not insert this code in your .qmd, as you only need to install the package once, not every time you compile the html.

library(HSAUR2)
data("USairpollution")

A)

GOAL: Find the cities that are most and least similar with respect to their pollution. Use dist to find L2 distances between the cities. Then use wrangling approaches (i.e., DSCI 210/325 techniques) to find the two cities that are most similar, and two cities that are most dissimilar.

library(HSAUR2)
library(tidyverse)

data("USairpollution")

d <- dist(USairpollution, method = "euclidean")

d_df <- as.matrix(d) %>%
  as_tibble(rownames = "city1") %>%
  pivot_longer(-city1, names_to = "city2", values_to = "distance") %>%
  filter(city1 != city2) %>%    
  filter(city1 < city2)                   

d_df %>% slice_min(distance, n = 1)
# A tibble: 1 × 3
  city1   city2       distance
  <chr>   <chr>          <dbl>
1 Atlanta Kansas City     28.3
d_df %>% slice_max(distance, n = 1)
# A tibble: 1 × 3
  city1      city2   distance
  <chr>      <chr>      <dbl>
1 Charleston Chicago    4673.

B)

Assess how scaling the data before computing distance impacts your answer to the previous question.

USairpollution_scaled <- scale(USairpollution)

d_scaled <- dist(USairpollution_scaled, method = "euclidean")

d_scaled_df <- as.matrix(d_scaled) %>%
  as_tibble(rownames = "city1") %>%
  pivot_longer(-city1, names_to = "city2", values_to = "distance") %>%
  filter(city1 != city2) %>%
  filter(city1 < city2)


d_scaled_df %>% slice_min(distance, n = 1)
# A tibble: 1 × 3
  city1        city2       distance
  <chr>        <chr>          <dbl>
1 Jacksonville New Orleans    0.523
d_scaled_df %>% slice_max(distance, n = 1)
# A tibble: 1 × 3
  city1   city2   distance
  <chr>   <chr>      <dbl>
1 Chicago Phoenix     10.2

Scaling the data makes every variable count equally…because unscaled distances are dominated by large scale variables (like population)

C)

Which city is Minneapolis most similar to? Most dissimilar to?

minneapolis_dist <- d_scaled_df %>%
  filter(city1 == "Minneapolis" | city2 == "Minneapolis")

minneapolis_dist %>% slice_min(distance, n = 1)
# A tibble: 1 × 3
  city1     city2       distance
  <chr>     <chr>          <dbl>
1 Milwaukee Minneapolis     1.23
minneapolis_dist %>% slice_max(distance, n = 1)
# A tibble: 1 × 3
  city1   city2       distance
  <chr>   <chr>          <dbl>
1 Chicago Minneapolis     7.50

Similar: Milwaukee

Dissimilar: Chicago