Define helper functions

make_graphs <-  function(df, explanatory, response, title_string, xlab_string, ylab_string, filename) {

  formula_string <- paste(substitute(response), "~", substitute(explanatory))
  # winner: https://win-vector.com/2019/07/06/programming-over-lm-in-r/
  # https://stackoverflow.com/questions/27261232/passing-argument-to-lm-in-r-within-function
  model <- eval(bquote(   lm( as.formula(formula_string), data = df)   ))
  print(model)  
  
  # ggplot scatterplot without line
  sp <- ggplot(df, aes( x = {{ explanatory }}, y = {{ response }} )) +
    geom_point(size = 4) + 
    ggtitle( title_string ) +
    xlab( xlab_string ) + 
    ylab( ylab_string ) +
    theme_bw(base_size = 18)
  
  print(sp) # generates both graph and VI(ts) automatically 
  
  # ggplot scatterplot with line
  sp <- sp + geom_smooth(method='lm', se=FALSE, size=2, color = "black")

  print(sp) # generates both graph and VI(ts) automatically
  
  MakeAccessibleSVG.ggplot(sp, file = paste0("reader-", filename))

  # tactile scatterplot with no line  
  brl_begin(paste0( 'tactile-', filename, '-scatterplot.pdf'), 
          pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
    # to get the variable values, use data[[substitute(varname)]] instead of data$varname
    plot(df[[substitute(explanatory)]],
         df[[substitute(response)]],
         main = title_string,
         xlab = xlab_string,
         ylab = ylab_string
         )
  brl_end()
  

  # tactile scatterplot with line  
  brl_begin(paste0( 'tactile-', filename, '-scatterplot-and-line.pdf'), 
          pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
    # to get the variable values, use data[[substitute(varname)]] instead of data$varname
    plot(df[[substitute(explanatory)]],
         df[[substitute(response)]],
         main = title_string,
         xlab = xlab_string,
         ylab = ylab_string
         )
    abline( model, col = "black", lwd = 3 )
  brl_end()
  

  # tactile scatterplot with line and ellipses and boxplots 
  brl_begin(paste0( 'tactile-', filename, '-scatterplot-and-line-and-ellipses-and-boxplots.pdf'), 
         pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
  scatterplot( as.formula(formula_string), data = df,
      main = title_string,
      xlab = xlab_string,
      ylab = ylab_string,
             pch = 16, cex = 2, col = "black",
             smooth = FALSE, grid = FALSE, frame = TRUE,
             ellipse = list(levels=c(.25, .75), robust=TRUE, fill=FALSE, fill.alpha=0, lwd = 3) )
  brl_end()


}

HW 10.1.1 hiking distance vs time

 distance_mi time_min
        0.61    20.32
        0.91    30.36
        1.01    40.99
        1.51    60.30
        1.51    45.41
        1.80    60.29
        2.11    60.43
        2.21    90.19
        2.56    89.99
        2.51    60.30
        2.81    89.99
        3.01    89.99
        3.21    90.12
        3.45    90.16
        3.80   119.81
        4.01   149.91
        4.01   120.15
        4.56   120.11
        4.61   149.91
        5.01   149.91
        5.51   180.01
        5.57   149.91
        5.80   180.01
        6.01   180.01
        7.01   179.88
        7.42   419.12
        9.52   299.56
make_graphs( df = hike, 
             explanatory = distance_mi, 
             response = time_min, 
             title = "HW 10.1 #1 hiking distance vs time", 
             xlab_string = "distance (miles)", 
             ylab_string = "time (minutes)",
             filename = "HW_10.1.1"
             )

Call:
lm(formula = as.formula(formula_string), data = df)

Coefficients:
(Intercept)  distance_mi  
      -8.32        34.70  
This scatter plot has title ‘Hiking distance versus time’ with x-axis ‘distance (miles)’ that has values from 0 to 10, and y-axis ‘time (minutes)’ with values from 0 to 400.  There are 27 big solid circle points. The points are concentrated tightly around a straight path (except for one outlier) that is increasing at a constant rate of about 30 minutes for every mile.

This scatter plot has title ‘Hiking distance versus time’ with x-axis ‘distance (miles)’ that has values from 0 to 10, and y-axis ‘time (minutes)’ with values from 0 to 400. There are 27 big solid circle points. The points are concentrated tightly around a straight path (except for one outlier) that is increasing at a constant rate of about 30 minutes for every mile.

This chart has title 'HW 10.1 #1 hiking distance vs time'.
It has x-axis 'distance (miles)' with labels 2.5, 5.0 and 7.5.
It has y-axis 'time (minutes)' with labels 100, 200, 300 and 400.
The chart is a set of 27 big solid circle points of which about 100% can be seen.
It has size set to 4.
This chart has title 'HW 10.1 #1 hiking distance vs time'.
It has x-axis 'distance (miles)' with labels 2.5, 5.0 and 7.5.
It has y-axis 'time (minutes)' with labels 0, 100, 200, 300 and 400.
It has 2 layers.
Layer 1 is a set of 27 big solid circle points of which about 100% can be seen.
Layer 1 has size set to 4.
Layer 2 is a 'lm' smoothed curve.Layer 2 has colour set to black.
Layer 2 has size set to 2.
This chart has title 'HW 10.1 #1 hiking distance vs time'.
It has x-axis 'distance (miles)' with labels 2.5, 5.0 and 7.5.
It has y-axis 'time (minutes)' with labels 0, 100, 200, 300 and 400.
It has 2 layers.
Layer 1 is a set of 27 big solid circle points of which about 100% can be seen.
Layer 1 has size set to 4.
Layer 2 is a 'lm' smoothed curve.Layer 2 has colour set to black.
Layer 2 has size set to 2.
This scatter plot has title ‘Hiking distance versus time’ with x-axis ‘distance (miles)’ that has values from 0 to 10, and y-axis ‘time (minutes)’ with values from 0 to 400.  There are 27 big solid circle points. The points are concentrated tightly around a straight path (except for one outlier) that is increasing at a constant rate of about 30 minutes for every mile.

This scatter plot has title ‘Hiking distance versus time’ with x-axis ‘distance (miles)’ that has values from 0 to 10, and y-axis ‘time (minutes)’ with values from 0 to 400. There are 27 big solid circle points. The points are concentrated tightly around a straight path (except for one outlier) that is increasing at a constant rate of about 30 minutes for every mile.

png 
  2 

HW 10.1.3 about glycemic loads and mean rating

foods <- read.table(text=
"MeanRating GlycemicLoad
4.01    22
3.73    14
3.73    12
3.71    7
3.68    14
3.6 21
3.51    17
3.29    16
3.26    24
3.22    0
3.03    0
2.97    7
2.73    15
2.64    26
2.56    22
2.57    22
2.54    0
2.5 29
2.47    3
2.18    0
2.16    0
2.13    15
2.07    11
1.94    0
1.93    10
1.88    1
1.87    8
1.84    0
1.77    12
1.74    0
1.74    20
1.66    4
1.63    7
1.6 2
1.53    0", header=TRUE, check.names=FALSE)

Call:
lm(formula = as.formula(formula_string), data = df)

Coefficients:
 (Intercept)  GlycemicLoad  
      2.1944        0.0349  
This scatter plot has title ‘Glycemic load versus mean rating’ with x-axis ‘glycemic load’ that has values from 0 to 30, and y-axis ‘mean rating’ with values from 1.5 to 4.  There are 35 big solid circle points. The points are quite spread out horizontally and vertically but they do show a general slight increase in mean rating as glycemic load increases.

This scatter plot has title ‘Glycemic load versus mean rating’ with x-axis ‘glycemic load’ that has values from 0 to 30, and y-axis ‘mean rating’ with values from 1.5 to 4. There are 35 big solid circle points. The points are quite spread out horizontally and vertically but they do show a general slight increase in mean rating as glycemic load increases.

This chart has title 'HW 10.1 #3 glycemic load vs mean rating'.
It has x-axis 'glycemic load' with labels 0, 10, 20 and 30.
It has y-axis 'mean rating' with labels 1.5, 2.0, 2.5, 3.0, 3.5 and 4.0.
The chart is a set of 35 big solid circle points of which about 97% can be seen.
It has size set to 4.
This chart has title 'HW 10.1 #3 glycemic load vs mean rating'.
It has x-axis 'glycemic load' with labels 0, 10, 20 and 30.
It has y-axis 'mean rating' with labels 1.5, 2.0, 2.5, 3.0, 3.5 and 4.0.
It has 2 layers.
Layer 1 is a set of 35 big solid circle points of which about 97% can be seen.
Layer 1 has size set to 4.
Layer 2 is a 'lm' smoothed curve.Layer 2 has colour set to black.
Layer 2 has size set to 2.
This chart has title 'HW 10.1 #3 glycemic load vs mean rating'.
It has x-axis 'glycemic load' with labels 0, 10, 20 and 30.
It has y-axis 'mean rating' with labels 1.5, 2.0, 2.5, 3.0, 3.5 and 4.0.
It has 2 layers.
Layer 1 is a set of 35 big solid circle points of which about 94% can be seen.
Layer 1 has size set to 4.
Layer 2 is a 'lm' smoothed curve.Layer 2 has colour set to black.
Layer 2 has size set to 2.
This scatter plot has title ‘Glycemic load versus mean rating’ with x-axis ‘glycemic load’ that has values from 0 to 30, and y-axis ‘mean rating’ with values from 1.5 to 4.  There are 35 big solid circle points. The points are quite spread out horizontally and vertically but they do show a general slight increase in mean rating as glycemic load increases.

This scatter plot has title ‘Glycemic load versus mean rating’ with x-axis ‘glycemic load’ that has values from 0 to 30, and y-axis ‘mean rating’ with values from 1.5 to 4. There are 35 big solid circle points. The points are quite spread out horizontally and vertically but they do show a general slight increase in mean rating as glycemic load increases.

png 
  2 

HW 10.1.4 on housing sq ft vs prices by location

houses <- read.table(text=
"sqft   price   location
900.4245641 121.4827342 notlakefront
1332.911007 202.2002959 notlakefront
1664.055598 182.3960118 notlakefront
2236.790998 241.6676939 notlakefront
2272.71276  213.7819329 notlakefront
2407.247765 328.0687403 notlakefront
3165.875711 314.1584852 notlakefront
1597.275542 412.0484081 lakefront
1736.981082 532.1023415 lakefront
1904.070236 497.9171293 lakefront
2695.003193 643.0897783 lakefront
3258.721474 754.0413121 lakefront
3351.031211 901.4073519 lakefront", header=TRUE, check.names=FALSE)

notlakefront <- subset(houses, location == "notlakefront")
lakefront <- subset(houses, location == "lakefront")


print(houses, row.names=FALSE)
   sqft price     location
  900.4 121.5 notlakefront
 1332.9 202.2 notlakefront
 1664.1 182.4 notlakefront
 2236.8 241.7 notlakefront
 2272.7 213.8 notlakefront
 2407.2 328.1 notlakefront
 3165.9 314.2 notlakefront
 1597.3 412.0    lakefront
 1737.0 532.1    lakefront
 1904.1 497.9    lakefront
 2695.0 643.1    lakefront
 3258.7 754.0    lakefront
 3351.0 901.4    lakefront

House sq ft vs price


Call:
lm(formula = as.formula(formula_string), data = df)

Coefficients:
(Intercept)         sqft  
    -57.346        0.214  
This scatter plot has title ‘Housing price versus square footage of home’ with x-axis ‘square footage’ that has values from 500 to 3500, and y-axis ‘price / 1000’ with values from 100 to 900 (in units thousands of dollars).  There are 12 big solid circle points. The points are spread out but do show a general increase in price as square footage increases.

This scatter plot has title ‘Housing price versus square footage of home’ with x-axis ‘square footage’ that has values from 500 to 3500, and y-axis ‘price / 1000’ with values from 100 to 900 (in units thousands of dollars). There are 12 big solid circle points. The points are spread out but do show a general increase in price as square footage increases.

This chart has title 'HW 10.1 #4 house sq ft vs price'.
It has x-axis 'square feet' with labels 1000, 1500, 2000, 2500 and 3000.
It has y-axis 'price/1000' with labels 250, 500 and 750.
The chart is a set of 13 big solid circle points of which about 100% can be seen.
It has size set to 4.
This chart has title 'HW 10.1 #4 house sq ft vs price'.
It has x-axis 'square feet' with labels 1000, 1500, 2000, 2500 and 3000.
It has y-axis 'price/1000' with labels 250, 500 and 750.
It has 2 layers.
Layer 1 is a set of 13 big solid circle points of which about 100% can be seen.
Layer 1 has size set to 4.
Layer 2 is a 'lm' smoothed curve.Layer 2 has colour set to black.
Layer 2 has size set to 2.
This chart has title 'HW 10.1 #4 house sq ft vs price'.
It has x-axis 'square feet' with labels 1000, 1500, 2000, 2500 and 3000.
It has y-axis 'price/1000' with labels 250, 500 and 750.
It has 2 layers.
Layer 1 is a set of 13 big solid circle points of which about 100% can be seen.
Layer 1 has size set to 4.
Layer 2 is a 'lm' smoothed curve.Layer 2 has colour set to black.
Layer 2 has size set to 2.
This scatter plot has title ‘Housing price versus square footage of home’ with x-axis ‘square footage’ that has values from 500 to 3500, and y-axis ‘price / 1000’ with values from 100 to 900 (in units thousands of dollars).  There are 12 big solid circle points. The points are spread out but do show a general increase in price as square footage increases.

This scatter plot has title ‘Housing price versus square footage of home’ with x-axis ‘square footage’ that has values from 500 to 3500, and y-axis ‘price / 1000’ with values from 100 to 900 (in units thousands of dollars). There are 12 big solid circle points. The points are spread out but do show a general increase in price as square footage increases.

png 
  2 

House sq ft vs price by location

This scatter plot has title ‘Housing price versus square footage of home’ with x-axis ‘square footage’ that has values from 500 to 3500, and y-axis ‘price / 1000’ with values from 100 to 900 (in units thousands of dollars).  There are 12 big solid circle points. The points are spread out but do show a general increase in price as square footage increases.

This scatter plot has title ‘Housing price versus square footage of home’ with x-axis ‘square footage’ that has values from 500 to 3500, and y-axis ‘price / 1000’ with values from 100 to 900 (in units thousands of dollars). There are 12 big solid circle points. The points are spread out but do show a general increase in price as square footage increases.

This chart has title 'HW 10.1 #4: House price versus sq ft'.
It has x-axis 'sq ft' with labels 1000, 1500, 2000, 2500 and 3000.
It has y-axis 'Price/1000' with labels 250, 500 and 750.
It has 3 layers.
Layer 1 is a set of 13 big solid circle points of which about 100% can be seen.
Layer 1 has size set to 4.
Layer 2 is a 'lm' smoothed curve.Layer 2 has colour set to black.
Layer 2 has size set to 2.
Layer 3 is a 'lm' smoothed curve.Layer 3 has colour set to black.
Layer 3 has size set to 2.

Plot with two lines (one line per group)

model_lakefront <- lm( price ~ sqft, data = lakefront)
print(model_lakefront)  

Call:
lm(formula = price ~ sqft, data = lakefront)

Coefficients:
(Intercept)         sqft  
     88.112        0.221  
model_notlakefront <- lm( price ~ sqft, data = notlakefront)
print(model_notlakefront)  

Call:
lm(formula = price ~ sqft, data = notlakefront)

Coefficients:
(Intercept)         sqft  
    59.7405       0.0848  
# tactile scatterplot with line  
brl_begin(paste0( 'tactile-HW_10.1.4-scatterplot-and-two-lines.pdf'), 
        pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
  plot(houses$sqft,
       houses$price,
       main = "HW 10.1 #4: House price versus sq ft by location",
       xlab = "sq ft",
       ylab = "Price/1000"
       )
  abline( model_lakefront, col = "black", lwd = 3 )
  abline( model_notlakefront, col = "black", lwd = 3 )
brl_end()
png 
  2 

HW 10.1.5 cricket chirps per minute

   temperature_Celsius chirps_per_second cricket_type
1                12.72            0.9278      Karschi
2                13.01            0.9808      Karschi
3                13.10            1.0724      Karschi
4                14.28            1.0733      Karschi
5                14.43            1.0317      Karschi
6                15.42            1.0322      Karschi
7                18.50            1.2667      Karschi
8                18.80            1.2546      Karschi
9                19.01            1.6871      Karschi
10               18.87            1.2326      Karschi
11               19.10            1.5040      Karschi
12               19.30            1.3845      Karschi
13               19.44            1.8043      Karschi
14               19.72            1.6675      Karschi
15               19.74            1.8048      Karschi
16               19.81            1.8973      Karschi
17               19.87            1.3919      Karschi
18               20.00            1.2751      Karschi
19               20.03            1.4375      Karschi
20               20.20            1.6409      Karschi
21               20.52            1.5753      Karschi
22               20.64            1.5340      Karschi
23               20.81            1.7587      Karschi
24               20.95            1.5773      Karschi
25               21.16            1.7868      Karschi
26               21.42            1.5563      Karschi
27               21.45            1.8113      Karschi
28               21.61            1.6488      Karschi
29               21.81            1.4840      Karschi
30               22.32            1.8118      Karschi
31               22.32            1.7198      Karschi
32               22.41            1.5825      Karschi
33               22.41            1.3741      Karschi
34               22.51            2.0395      Karschi
35               22.93            1.7424      Karschi
36               22.93            1.6292      Karschi
37               23.03            1.6983      Karschi
38               23.12            1.7911      Karschi
39               23.36            1.7002      Karschi
40               23.86            1.7044      Karschi
41               24.09            1.7263      Karschi
42               24.31            1.8179      Karschi
43               24.40            1.9804      Karschi
44               24.92            1.8651      Karschi
45               25.53            2.3069      Karschi
46               25.72            2.4914      Karschi
47               26.01            2.3269      Karschi
48               10.50            1.3241      Fultoni
49               10.50            1.1803      Fultoni
50               11.13            1.3405      Fultoni
51               11.60            1.3438      Fultoni
52               12.04            1.3608      Fultoni
53               13.01            1.4211      Fultoni
54               13.50            1.5067      Fultoni
55               13.49            1.4380      Fultoni
56               14.01            1.6015      Fultoni
57               14.01            1.4801      Fultoni
58               14.53            1.6581      Fultoni
59               14.50            1.5634      Fultoni
60               15.03            1.6689      Fultoni
61               14.99            1.5903      Fultoni
62               15.02            1.5285      Fultoni
63               15.51            1.7220      Fultoni
64               15.51            1.6315      Fultoni
65               15.51            1.5272      Fultoni
66               15.99            1.7917      Fultoni
67               16.52            1.7145      Fultoni
68               16.50            1.5680      Fultoni
69               17.01            1.8540      Fultoni
70               17.04            1.7673      Fultoni
71               17.01            1.6825      Fultoni
72               18.02            1.9160      Fultoni
73               18.02            1.8408      Fultoni
74               18.02            1.7813      Fultoni
75               20.04            2.0893      Fultoni
76               21.02            2.1567      Fultoni
77               23.03            2.3977      Fultoni
78               23.51            2.4162      Fultoni
79               15.51            1.5625      Fultoni
80               16.01            1.7255      Fultoni
81               16.01            1.6848      Fultoni
82               16.01            1.6114      Fultoni
83               16.01            1.5666      Fultoni
84               16.51            1.7745      Fultoni
85               16.51            1.8152      Fultoni
86               16.51            1.5951      Fultoni
87               17.01            1.7296      Fultoni
88               17.01            1.8152      Fultoni
89               14.01            1.5095      Fultoni

Crickets temperature vs chirps/sec


Call:
lm(formula = as.formula(formula_string), data = df)

Coefficients:
        (Intercept)  temperature_Celsius  
             0.7512               0.0482  
This scatter plot has title ‘African crickets: temperature versus chirps per second’ with x-axis ‘temperature (Celsius)’ that has values from 10 to 30, and y-axis ‘chirps per second’ with values from 0.5 to 2.5.  There are 89 big solid circle points. The points are somewhat spread out but do show a clear increase in chirps per second as temperature increases.

This scatter plot has title ‘African crickets: temperature versus chirps per second’ with x-axis ‘temperature (Celsius)’ that has values from 10 to 30, and y-axis ‘chirps per second’ with values from 0.5 to 2.5. There are 89 big solid circle points. The points are somewhat spread out but do show a clear increase in chirps per second as temperature increases.

This chart has title 'HW 10.1 #5 crickets temp vs chirps/second'.
It has x-axis 'temperature (Celsius)' with labels 10, 15, 20 and 25.
It has y-axis 'chirps per second' with labels 1.0, 1.5, 2.0 and 2.5.
The chart is a set of 89 big solid circle points of which about 100% can be seen.
It has size set to 4.
This chart has title 'HW 10.1 #5 crickets temp vs chirps/second'.
It has x-axis 'temperature (Celsius)' with labels 10, 15, 20 and 25.
It has y-axis 'chirps per second' with labels 1.0, 1.5, 2.0 and 2.5.
It has 2 layers.
Layer 1 is a set of 89 big solid circle points of which about 100% can be seen.
Layer 1 has size set to 4.
Layer 2 is a 'lm' smoothed curve.Layer 2 has colour set to black.
Layer 2 has size set to 2.
This chart has title 'HW 10.1 #5 crickets temp vs chirps/second'.
It has x-axis 'temperature (Celsius)' with labels 10, 15, 20 and 25.
It has y-axis 'chirps per second' with labels 1.0, 1.5, 2.0 and 2.5.
It has 2 layers.
Layer 1 is a set of 89 big solid circle points of which about 100% can be seen.
Layer 1 has size set to 4.
Layer 2 is a 'lm' smoothed curve.Layer 2 has colour set to black.
Layer 2 has size set to 2.
This scatter plot has title ‘African crickets: temperature versus chirps per second’ with x-axis ‘temperature (Celsius)’ that has values from 10 to 30, and y-axis ‘chirps per second’ with values from 0.5 to 2.5.  There are 89 big solid circle points. The points are somewhat spread out but do show a clear increase in chirps per second as temperature increases.

This scatter plot has title ‘African crickets: temperature versus chirps per second’ with x-axis ‘temperature (Celsius)’ that has values from 10 to 30, and y-axis ‘chirps per second’ with values from 0.5 to 2.5. There are 89 big solid circle points. The points are somewhat spread out but do show a clear increase in chirps per second as temperature increases.

png 
  2 

Plot with two lines (one line per group)

model_karschi <- lm( chirps_per_second ~ temperature_Celsius, data = karschi)
print(model_karschi)  

Call:
lm(formula = chirps_per_second ~ temperature_Celsius, data = karschi)

Coefficients:
        (Intercept)  temperature_Celsius  
            -0.2192               0.0882  
model_fultoni <- lm( chirps_per_second ~ temperature_Celsius, data = fultoni)
print(model_fultoni)

Call:
lm(formula = chirps_per_second ~ temperature_Celsius, data = fultoni)

Coefficients:
        (Intercept)  temperature_Celsius  
             0.2904               0.0875  
# tactile scatterplot with line  
brl_begin(paste0( 'tactile-HW_10.1.5-scatterplot-and-line-karschi-only.pdf'), 
        pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
  plot(karschi$temperature_Celsius,
       karschi$chirps_per_second,
       main = "HW 10.1 #5: Karschi",
       xlab = "temperature (Celsius)",
       ylab = "chirps per second"
       )
  abline( model_karschi, col = "black", lwd = 3 )
brl_end()
png 
  2 
# tactile scatterplot with line  
brl_begin(paste0( 'tactile-HW_10.1.5-scatterplot-and-line-fultoni-only.pdf'), 
        pt=12, paper='special', font='SWL') #, font='BRL' or 'SWL')
  plot(fultoni$temperature_Celsius,
       fultoni$chirps_per_second,
       main = "HW 10.1 #5: Fultoni",
       xlab = "temperature (Celsius)",
       ylab = "chirps per second"
       )
  abline( model_fultoni, col = "black", lwd = 3 )
brl_end()
png 
  2