dat <- filter(growth.data, binomial == "Morone saxatilis")
dat <- filter(dat, lab_reared == "lab")


#LAB DATA FOR M.SAX
plot1 <- ggplot(dat, aes(x=temperature_C_from.pub.or.fb.or.slb, y=ln_Growth_Rate, col=Locality))+
  geom_jitter()+
  geom_smooth(method = "lm", se=F) +
    theme(legend.position="none")

plot1
## `geom_smooth()` using formula = 'y ~ x'

dat1 <- filter(dat, Locality == "MD")
aline<-lm(ln_Growth_Rate~temperature_C_from.pub.or.fb.or.slb, data=dat1)
summary(aline)
## 
## Call:
## lm(formula = ln_Growth_Rate ~ temperature_C_from.pub.or.fb.or.slb, 
##     data = dat1)
## 
## Residuals:
##         1         2         3         4         5         6 
##  0.007607  0.065918 -0.055819  0.024185 -0.016208 -0.025683 
## 
## Coefficients:
##                                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                         -2.615010   0.095832  -27.29 1.07e-05 ***
## temperature_C_from.pub.or.fb.or.slb  0.101930   0.004266   23.89 1.82e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.0475 on 4 degrees of freedom
## Multiple R-squared:  0.993,  Adjusted R-squared:  0.9913 
## F-statistic: 570.9 on 1 and 4 DF,  p-value: 1.819e-05
dat2 <- filter(dat, Locality == "NY")
bline<-lm(ln_Growth_Rate~temperature_C_from.pub.or.fb.or.slb, data=dat2)
summary(bline)
## 
## Call:
## lm(formula = ln_Growth_Rate ~ temperature_C_from.pub.or.fb.or.slb, 
##     data = dat2)
## 
## Residuals:
##        1        2        3        4        5        6 
##  0.03152  0.12086  0.08226 -0.06086 -0.04017 -0.13361 
## 
## Coefficients:
##                                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                         -2.658849   0.215247  -12.35 0.000247 ***
## temperature_C_from.pub.or.fb.or.slb  0.104815   0.009582   10.94 0.000397 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1067 on 4 degrees of freedom
## Multiple R-squared:  0.9677, Adjusted R-squared:  0.9596 
## F-statistic: 119.7 on 1 and 4 DF,  p-value: 0.0003966
dat3 <- filter(dat, Locality == "SC")
cline<-lm(ln_Growth_Rate~temperature_C_from.pub.or.fb.or.slb, data=dat3)
summary(cline)
## 
## Call:
## lm(formula = ln_Growth_Rate ~ temperature_C_from.pub.or.fb.or.slb, 
##     data = dat3)
## 
## Residuals:
##         1         2         3         4         5         6 
## -0.027222  0.001601 -0.133214 -0.015356  0.115488  0.058703 
## 
## Coefficients:
##                                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                         -3.331675   0.190077  -17.53 6.22e-05 ***
## temperature_C_from.pub.or.fb.or.slb  0.121608   0.008461   14.37 0.000136 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09422 on 4 degrees of freedom
## Multiple R-squared:  0.981,  Adjusted R-squared:  0.9763 
## F-statistic: 206.6 on 1 and 4 DF,  p-value: 0.0001362
dat4 <- filter(dat, Locality == "Stuttgart, Arkansas")
dline<-lm(ln_Growth_Rate~temperature_C_from.pub.or.fb.or.slb, data=dat4)
summary(cline)
## 
## Call:
## lm(formula = ln_Growth_Rate ~ temperature_C_from.pub.or.fb.or.slb, 
##     data = dat3)
## 
## Residuals:
##         1         2         3         4         5         6 
## -0.027222  0.001601 -0.133214 -0.015356  0.115488  0.058703 
## 
## Coefficients:
##                                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                         -3.331675   0.190077  -17.53 6.22e-05 ***
## temperature_C_from.pub.or.fb.or.slb  0.121608   0.008461   14.37 0.000136 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.09422 on 4 degrees of freedom
## Multiple R-squared:  0.981,  Adjusted R-squared:  0.9763 
## F-statistic: 206.6 on 1 and 4 DF,  p-value: 0.0001362
# create predictions at the source temperature
coef(aline)
##                         (Intercept) temperature_C_from.pub.or.fb.or.slb 
##                          -2.6150096                           0.1019296
a_at_source_temp<-coef(aline)[1]+coef(aline)[2]*13.23084
b_at_source_temp<-coef(bline)[1]+coef(bline)[2]*10.01667
c_at_source_temp<-coef(cline)[1]+coef(cline)[2]*18.04834
d_at_source_temp<-coef(cline)[1]+coef(cline)[2]*16.7325

g_at_source<-c(a_at_source_temp, b_at_source_temp,c_at_source_temp, d_at_source_temp)
source<-c(13.23084,10.01667,18.04834,16.7325)

growth_a_source<-as.data.frame(cbind(g_at_source, source))
str(growth_a_source)
## 'data.frame':    4 obs. of  2 variables:
##  $ g_at_source: num  -1.27 -1.61 -1.14 -1.3
##  $ source     : num  13.2 10 18 16.7
view(growth_a_source)

plot(growth_a_source$source, growth_a_source$g_at_source,xlab="population source temp", ylab="log lab growth rate")

#convert temp unit
growth_a_source <- transform(growth_a_source, Inverse_t = 1/(8.62e-5 * (source + 273.15))) 
str(growth_a_source)
## 'data.frame':    4 obs. of  3 variables:
##  $ g_at_source: num  -1.27 -1.61 -1.14 -1.3
##  $ source     : num  13.2 10 18 16.7
##  $ Inverse_t  : num  40.5 41 39.8 40
#plot as mte
plot3 <- ggplot(growth_a_source, aes(x=Inverse_t, y=g_at_source))+
  geom_point() +
  xlab("Temperature (1/kT)") +
  ylab("Ln lab growth rate at source temperature") +
  geom_smooth(method = "lm", se=F) +
  ggtitle("slope = -0.3498") +
  theme_classic()
  
plot3
## `geom_smooth()` using formula = 'y ~ x'

#calc slope
mod <- lm(g_at_source ~ Inverse_t, data = growth_a_source)
summary(mod)
## 
## Call:
## lm(formula = g_at_source ~ Inverse_t, data = growth_a_source)
## 
## Residuals:
##   X.Intercept. X.Intercept..1 X.Intercept..2 X.Intercept..3 
##        0.12206       -0.05966        0.01718       -0.07958 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)
## (Intercept)  12.7814     5.1230   2.495     0.13
## Inverse_t    -0.3498     0.1270  -2.754     0.11
## 
## Residual standard error: 0.112 on 2 degrees of freedom
## Multiple R-squared:  0.7914, Adjusted R-squared:  0.687 
## F-statistic: 7.585 on 1 and 2 DF,  p-value: 0.1104
#slope = -0.3498

#compare to wild slope

dat_wild <- filter(growth.data, binomial == "Morone saxatilis")
dat_wild <- filter(dat_wild, lab_reared == "wild")

mod2 <- lm(ln_Growth_Rate ~ Inverse_t, data = dat_wild)
summary(mod2)
## 
## Call:
## lm(formula = ln_Growth_Rate ~ Inverse_t, data = dat_wild)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.35289 -0.17656  0.00962  0.30335  1.11855 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept)  -4.5308     2.9952  -1.513  0.13320   
## Inverse_t     0.2237     0.0728   3.072  0.00267 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.6606 on 111 degrees of freedom
## Multiple R-squared:  0.07837,    Adjusted R-squared:  0.07007 
## F-statistic: 9.439 on 1 and 111 DF,  p-value: 0.002672
#slope = 0.2237

#plot wild slope

plot2 <- ggplot(dat_wild, aes(x=Inverse_t, y=ln_Growth_Rate))+
  geom_point() +
  geom_smooth(method = "lm", se=F) +
  theme_classic() +
   xlab("Temperature (1/kT)") +
  ylab("Ln wild growth rate") +
  ggtitle("slope = 0.2237") +
  theme(legend.position="none") 
  
plot2
## `geom_smooth()` using formula = 'y ~ x'

library(cowplot)
plot_grid(plot3, plot2)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'