Chapter 1 Problems

Harold Nelson

2023-01-18

Problem 1

Create two new vectors, one containing all the odd numbers between 1 and 19, and the other containing all even numbers between 2 and 20. Add the two vectors element-wise to produce a single vector and then create a new vector that contains only numbers greater than 15. Finally, compute the mean value of this vector. If you do all the calculations right, the mean will equal 29.

Solution 1

odds = seq(from = 1, to = 19, by = 2)
print(odds)
##  [1]  1  3  5  7  9 11 13 15 17 19
evens = seq(from = 2, to = 20, by = 2)
print(evens)
##  [1]  2  4  6  8 10 12 14 16 18 20

Solution 2

base = 1:20
print(base)
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
evens = base[c(F,T)]
print(evens)
##  [1]  2  4  6  8 10 12 14 16 18 20
odds = base[c(T,F)]
print(odds)
##  [1]  1  3  5  7  9 11 13 15 17 19

Wrap up Problem 1

single_vector = odds + evens
new_vector = single_vector[single_vector > 15]
mean(new_vector)
## [1] 29

Problem 2

Starting with the cover_data data frame, create a new data frame containing only the rows of data for plot number 3 and print the results to the screen.

Solution

Create the dataframe.

cover <- c(63, 86, 23, 77, 68, 91, 43, 76, 69, 12, 31, 78)

plots <- 1:4
plot_codes <- rep(plots, times=3)
years <- rep(2014:2016, each=4)

cover2 <- c(59, 98, 28, 71, 62, 90, 48, 77, 74, 15, 38, 75)
cover3 <- c(91, 91, 33, 68, 59, 88, 44, 81, 72, 23, 44, 67)

cover_data <- data.frame(plot_codes, years, cover, cover2, cover3)

summary(cover_data)
##    plot_codes       years          cover           cover2          cover3     
##  Min.   :1.00   Min.   :2014   Min.   :12.00   Min.   :15.00   Min.   :23.00  
##  1st Qu.:1.75   1st Qu.:2014   1st Qu.:40.00   1st Qu.:45.50   1st Qu.:44.00  
##  Median :2.50   Median :2015   Median :68.50   Median :66.50   Median :67.50  
##  Mean   :2.50   Mean   :2015   Mean   :59.75   Mean   :61.25   Mean   :63.42  
##  3rd Qu.:3.25   3rd Qu.:2016   3rd Qu.:77.25   3rd Qu.:75.50   3rd Qu.:82.75  
##  Max.   :4.00   Max.   :2016   Max.   :91.00   Max.   :98.00   Max.   :91.00
cover_data$plot_codes = factor(cover_data$plot_codes)
cover_data$years = factor(cover_data$years)

summary(cover_data)
##  plot_codes  years       cover           cover2          cover3     
##  1:3        2014:4   Min.   :12.00   Min.   :15.00   Min.   :23.00  
##  2:3        2015:4   1st Qu.:40.00   1st Qu.:45.50   1st Qu.:44.00  
##  3:3        2016:4   Median :68.50   Median :66.50   Median :67.50  
##  4:3                 Mean   :59.75   Mean   :61.25   Mean   :63.42  
##                      3rd Qu.:77.25   3rd Qu.:75.50   3rd Qu.:82.75  
##                      Max.   :91.00   Max.   :98.00   Max.   :91.00

Solve the Problem

new_df = cover_data[cover_data$plot_codes == 3,]
new_df
##    plot_codes years cover cover2 cover3
## 3           3  2014    23     28     33
## 7           3  2015    43     48     44
## 11          3  2016    31     38     44

Problem 3

From the Camp 32 dataset, extract a vector containing the CBI values from the untreated plots (treatment code U) and another containing the CBI values for the thinned/unburned plots (treatemnt code T). Conduct a two-sample t-test comparing the mean CBI for these two treatment types.

Get the Data

Camp32 <- read.csv("~/Library/CloudStorage/Dropbox/GDSWR/gdswr_data/Chapter1/Camp32.csv")

summary(Camp32)
##      DATE             PLOT_ID               LAT             LONG       
##  Length:36          Length:36          Min.   :48.84   Min.   :-115.2  
##  Class :character   Class :character   1st Qu.:48.85   1st Qu.:-115.2  
##  Mode  :character   Mode  :character   Median :48.85   Median :-115.2  
##                                        Mean   :48.85   Mean   :-115.2  
##                                        3rd Qu.:48.85   3rd Qu.:-115.2  
##                                        Max.   :48.86   Max.   :-115.2  
##       DNBR            CBI         PLOT_CODE        
##  Min.   :-34.0   Min.   :0.780   Length:36         
##  1st Qu.: 85.5   1st Qu.:1.302   Class :character  
##  Median :298.0   Median :2.125   Mode  :character  
##  Mean   :333.2   Mean   :1.906                     
##  3rd Qu.:561.0   3rd Qu.:2.485                     
##  Max.   :771.0   Max.   :2.700

Get the Vectors

untreated = Camp32$CBI[Camp32$PLOT_CODE == "U"]
thinned = Camp32$CBI[Camp32$PLOT_CODE == "T"]

Run the Test

t.test(untreated, thinned)
## 
##  Welch Two Sample t-test
## 
## data:  untreated and thinned
## t = -1.5267, df = 16.476, p-value = 0.1458
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.46910931  0.07577598
## sample estimates:
## mean of x mean of y 
##  2.270000  2.466667

Problem 4

Generate a histogram plot of CBI values and compare it with the histogram of DNBR values.

hist(Camp32$CBI, main = "CBI Values")

hist(Camp32$DNBR, main = "DNBR Values")

Problem 5

Generate a new scatterplot based on the observed values of CBI on the x-axis and the predicted values of CBI from the linear regression on the y-axis. Make the plot have an aspect ratio of 1 (equal scales of x and y axes). To do this, you will need to specify an additional argument. Consult the help page for the base plot() function to find the name of the argument to use.

Solution

camp32_lm <- lm(formula = CBI ~ DNBR, 
                data = Camp32)


plot(Camp32$CBI,fitted(camp32_lm), asp = 1)