Skip to content

Instantly share code, notes, and snippets.

/Chapter1.R Secret

Created July 12, 2015 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5ceeecd87ace0ba749cf to your computer and use it in GitHub Desktop.
Save anonymous/5ceeecd87ace0ba749cf to your computer and use it in GitHub Desktop.
### Project: inch through a calculus text with R as my graphing calculator. Noted: R is not a graphing calculator.
## Ch 1.2
### Problem 21
q21 <- read.table(text = "
income ulcer_rate
4000 14.1
6000 13.0
8000 13.4
12000 12.5
16000 12.0
20000 12.4
30000 10.5
45000 9.4
60000 8.2
",header = TRUE, sep = "")
plot.window(c(0,15), c(0,80000))
plot(q21, ylim = c(0,15), xlim = c(0,80000))
#### Brute Force linear model
rise <- q21[nrow(q21),2] - q21[1,2]
run <- q21[nrow(q21),1] - q21[1,1]
slope <- rise/run
y_intercept <- -1 * (slope * q21[nrow(q21),1] - q21[nrow(q21),2])
b <- c(y_intercept, -1 * (slope * q21[1,1] - q21[1,2])) ## just checking
part_b <- function(x) {return (slope * x + y_intercept)}
abline(a = y_intercept, b = slope, col = "lightblue") ## linear model
abline(lsfit(q21$income, q21$ulcer_rate), col = "pink") ## least squares
abline(lm(q21$ulcer_rate ~ q21$income), col = "grey40") ## also draws least squares
#### the tilde you can think of as "depends on" -- population depends on year.
part_b(80000) ## ulcer rate at $80K?
### Problem 22
q22 <- read.table(text = "
temperature chirps_per_min
50 20
55 46
60 79
65 91
70 113
75 140
80 173
85 198
90 211
",header = TRUE, sep = "")
plot(q22, ylim=c(0,300), xlim = c(32,110))
abline(lsfit(q22$temperature, q22$chirps_per_min))
abline(lm(q22$chirps_per_min ~ q22$temperature))
#### Sanity check: Calculate my own slope using two points closest to the line
slope <- ((q22[2,2] - q22[6,2]) / (q22[2,1] - q22[6,1]))
new.temp <- data.frame(temperature = 100)
model <- lm(chirps_per_min ~ temperature, data = q22)
predict(model, newdata = new.temp)
points(100,264.7) ## Draw the result on the line, just to see.
### Problem 24
q23 <- read.table(text = "
Year Height_in_feet
1900 10.83
1904 11.48
1908 12.17
1912 12.96
1920 13.42
1924 12.96
1928 13.77
1932 14.15
1936 14.27
1948 14.1
1952 14.92
1956 14.96
1960 15.42
1964 16.73
1968 17.71
1972 18.04
1976 18.04
1980 18.96
1984 18.85
1988 19.77
1992 19.02
1996 19.42
",header = TRUE, sep = "")
plot(q23)
model <- lm(Height_in_feet ~ Year, data = q23)
abline(model)
new.data <- data.frame(Year = c(2000, 2010, 2100))
predict(model, newdata = new.data)
### Problem 24
## /* 'Reduction in emissions (%)' 'Cost per car (in $)' */
q24 <- read.table(text = "reductions cost.per.car
50 45
55 55
60 62
65 70
70 80
75 90
80 100
85 200
90 375
95 600
",header = TRUE, sep = "")
plot(q24$cost.per.car, q24$reductions, ylim = c(0,100), xlim = c(0,700))
#### Looks exponential, but how do I model that?
### Problem 25
q25 <- read.table(text = "year pop.in.millions
1900 1650
1910 1750
1920 1860
1930 2070
1940 2300
1950 2560
1960 3040
1970 3710
1980 4450
1990 5280
2000 6070",header = TRUE, sep = "")
plot(q25, ylim=c(0, 8000))
#### Looks cubic, but how do you model that?
model <- lm(pop.in.millions ~ poly(year, degree=3), data=q25)
abline(model)
### Problem 26
q26 <- read.table(text = "
Planet d T
Mercury 0.387 0.241
Venus 0.723 0.615
Earth 1 1
Mars 1.523 1.881
Jupiter 5.203 11.861
Saturn 9.541 29.457
Uranus 19.19 84.008
Neptune 30.086 164.784
",header = TRUE, sep = "")
plot(q26$d, q26$T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment