---
title: "Homework Chapter 10.1"
author: "Dr. Paul Pearson"
date: "2024-11-01"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, comment = "", warning = FALSE, message = FALSE)

library(ggplot2) 
# ggsave(ggplot_object) can save svg files without xml annotations

library(BrailleR) 
# VI(ggplot_object) for annotations for in text graph annotations
# MakeAccessibleSVG(ggplot_object) to save accessible standalone html (=svg + xml annotations)

library(tactileR) 
# Use braille in graphs for embossed printing
# brl_begin(file='tactile_histogram_SWL.pdf', pt=14, paper='special', font='SWL')
# base or lattice plot code here
# brl_end()

library(infer)
#library(tibble)

library(tidyr) # for reshaping data, etc.

library(car) # for fancy scatterplots

set.seed(1)
options(digits = 4)
```



# Define helper functions

```{r}
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

```{r, echo=FALSE}
hike <- read.table(text=
"distance_mi	time_min
0.6097186336	20.32467532
0.9050679996	30.35714286
1.013197929	40.98678108
1.510114442	60.29637592
1.509591285	45.40584416
1.80488472	60.28733766
2.107534689	60.43194981
2.211915266	90.18752081
2.560801906	89.99002023
2.512130856	60.29637592
2.812892859	89.99458874
3.009500625	89.99458874
3.208011736	90.12263075
3.453380132	90.16378711
3.803841346	119.8133117
4.009197009	149.9107143
4.010004961	120.1477273
4.557401918	120.1099707
4.61306372	149.9107143
5.013472219	149.9107143
5.513155229	180.0081169
5.567220817	149.9107143
5.800632131	180.0081169
6.01152509	180.0081169
7.010177463	179.882711
7.41944808	419.1152597
9.516062599	299.5616883", header=TRUE, check.names=FALSE)

print(round(hike,2), row.names=FALSE)
```


```{r, fig.cap="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."}
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"
             )
```


# HW 10.1.3 about glycemic loads and mean rating

```{r}
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)
```

```{r, echo=FALSE, fig.cap="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."}
make_graphs( df = foods, 
             explanatory = GlycemicLoad, 
             response = MeanRating, 
             title = "HW 10.1 #3 glycemic load vs mean rating", 
             xlab_string = "glycemic load", 
             ylab_string = "mean rating",
             filename = "HW_10.1.3"
             )
```







# HW 10.1.4 on housing sq ft vs prices by location


```{r}
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)
```

## House sq ft vs price

```{r, echo=FALSE, fig.cap="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."}
make_graphs( df = houses, 
             explanatory = sqft, 
             response = price, 
             title = "HW 10.1 #4 house sq ft vs price", 
             xlab_string = "square feet", 
             ylab_string = "price/1000",
             filename = "HW_10.1.4"
             )
```



## House sq ft vs price by location

```{r, echo=FALSE, fig.cap="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."}
# time series plot
sp <- ggplot(houses, aes(sqft, price)) +
  geom_point(size = 4) + 
  ggtitle("HW 10.1 #4: House price versus sq ft") +
  xlab("sq ft") + 
  ylab("Price/1000") +
  theme_bw(base_size = 18) +
  # https://stackoverflow.com/questions/42132442/ggplot-adding-regression-line-of-different-subsets
  geom_smooth(data =  subset(houses, location == "lakefront"), 
              method='lm',
              se=FALSE, 
              size=2,
            color = "black") +
  geom_smooth(data =  subset(houses, location == "notlakefront"), 
              method='lm',
              se=FALSE, 
              size=2,
            color = "black")


sp # generates both graph and VI(ts) automatically

#lm(price ~ sqft, houses)
```


## Plot with two lines (one line per group)

```{r}
model_lakefront <- lm( price ~ sqft, data = lakefront)
print(model_lakefront)  

model_notlakefront <- lm( price ~ sqft, data = notlakefront)
print(model_notlakefront)  


# 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()
```




# HW 10.1.5 cricket chirps per minute

```{r, echo = FALSE}
crickets <- read.table(text=
"temperature_Celsius	chirps_per_second	cricket_type
12.71682406	0.9277767901	Karschi
13.0057562	0.9808195347	Karschi
13.10031581	1.072372765	Karschi
14.28231093	1.073306982	Karschi
14.43006032	1.031664275	Karschi
15.41702624	1.03220145	Karschi
18.49546685	1.266741726	Karschi
18.79538319	1.254587474	Karschi
19.01029139	1.687087312	Karschi
18.86845198	1.232590918	Karschi
19.104851	1.503980851	Karschi
19.29922353	1.384453023	Karschi
19.43580963	1.8043315	Karschi
19.71948846	1.667468763	Karschi
19.74312836	1.804798608	Karschi
19.81404807	1.897286055	Karschi
19.86920784	1.391900805	Karschi
20.00316729	1.275097775	Karschi
20.02680719	1.437495767	Karschi
20.19753982	1.64094739	Karschi
20.52324514	1.575292721	Karschi
20.63553467	1.534021755	Karschi
20.81217728	1.758658687	Karschi
20.94876338	1.577316857	Karschi
21.15561253	1.786814938	Karschi
21.42156143	1.556296983	Karschi
21.44520133	1.811338125	Karschi
21.61068065	1.64878443	Karschi
21.80505318	1.483998995	Karschi
22.31987772	1.811805233	Karschi
22.31987772	1.719784894	Karschi
22.41443732	1.582455049	Karschi
22.41443732	1.374124739	Karschi
22.51425025	2.039494588	Karschi
22.93451518	1.742361796	Karschi
22.93451518	1.629165881	Karschi
23.02907478	1.698297912	Karschi
23.12363439	1.791096764	Karschi
23.36003342	1.700166345	Karschi
23.86119935	1.704417031	Karschi
24.09287039	1.726324411	Karschi
24.30562951	1.817877641	Karschi
24.40018912	1.980431336	Karschi
24.92026697	1.865055581	Karschi
25.53490443	2.306940051	Karschi
25.72402365	2.491447836	Karschi
26.01295579	2.326870005	Karschi
10.49992656	1.324144149	Fultoni
10.49992656	1.180274787	Fultoni
11.13482679	1.34049294	Fultoni
11.59681802	1.343762698	Fultoni
12.04440017	1.360838102	Fultoni
13.0057562	1.421146976	Fultoni
13.50219415	1.50670565	Fultoni
13.49431418	1.438040727	Fultoni
14.01439203	1.601528638	Fultoni
14.01439203	1.480123726	Fultoni
14.52816591	1.658095456	Fultoni
14.50295001	1.563381459	Fultoni
15.03203354	1.668870088	Fultoni
14.991508	1.590302469	Fultoni
15.0151479	1.528504038	Fultoni
15.51158585	1.721964733	Fultoni
15.51158585	1.631501422	Fultoni
15.51158585	1.527232465	Fultoni
15.99113815	1.791719575	Fultoni
16.5162817	1.714471537	Fultoni
16.50446174	1.567974691	Fultoni
17.01103108	1.854000684	Fultoni
17.04480237	1.76727424	Fultoni
17.00680967	1.682455155	Fultoni
18.01741549	1.915970388	Fultoni
18.01741549	1.840765949	Fultoni
18.01741549	1.781256349	Fultoni
20.04369283	2.089267574	Fultoni
21.01968309	2.156706338	Fultoni
23.02907478	2.397714767	Fultoni
23.50862709	2.416243397	Fultoni
15.5108055	1.5625	Fultoni
16.01178782	1.725543478	Fultoni
16.01178782	1.684782609	Fultoni
16.01178782	1.611413043	Fultoni
16.01178782	1.566576087	Fultoni
16.51277014	1.774456522	Fultoni
16.51277014	1.815217391	Fultoni
16.51277014	1.595108696	Fultoni
17.01375246	1.729619565	Fultoni
17.01375246	1.815217391	Fultoni
14.00785855	1.50951087	Fultoni", header=TRUE, check.names=FALSE)

print(crickets, row.names=TRUE)

karschi <- subset(crickets, cricket_type == "Karschi")
fultoni <- subset(crickets, cricket_type == "Fultoni")
```


## Crickets temperature vs chirps/sec

```{r, echo=FALSE, fig.cap="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."}
make_graphs( df = crickets, 
             explanatory = temperature_Celsius, 
             response = chirps_per_second, 
             title = "HW 10.1 #5 crickets temp vs chirps/second", 
             xlab_string = "temperature (Celsius)", 
             ylab_string = "chirps per second",
             filename = "HW_10.1.5"
             )
```




## Plot with two lines (one line per group)

```{r}
model_karschi <- lm( chirps_per_second ~ temperature_Celsius, data = karschi)
print(model_karschi)  

model_fultoni <- lm( chirps_per_second ~ temperature_Celsius, data = fultoni)
print(model_fultoni)

# 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()


# 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()

```




