#1 snow<-read.delim2("clipboard") summary(snow) lm.1<-lm(diam~temp, data=snow)# fitting the model anova(lm.1) # Analysis of Variance Table # # Response: diam # Df Sum Sq Mean Sq F value Pr(>F) # temp 1 32.798 32.798 89.111 6.652e-07 *** # Residuals 12 4.417 0.368 # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 plot(lm.1) # Regression diagnostics plots summary(lm.1)# Summary of the model and regression coefficients # Call: # lm(formula = diam ~ temp, data = snow) # # Residuals: # Min 1Q Median 3Q Max # -1.05721 -0.21684 0.07169 0.17846 1.12766 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 10.65537 0.31037 34.33 2.37e-13 *** # temp 0.26101 0.02765 9.44 6.65e-07 *** # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 0.6067 on 12 degrees of freedom # Multiple R-squared: 0.8813, Adjusted R-squared: 0.8714 # F-statistic: 89.11 on 1 and 12 DF, p-value: 6.652e-07 library(ggplot2) #Plotting the scatterplot with regression line ggplot(snow, aes(x=temp, y=diam))+geom_point()+ geom_smooth(method="lm", col=1)+theme_classic() predict(lm.1, newdata=data.frame(temp=-15)) # Prediction from the # regression for a new value of the predictor predict(lm.1, newdata=data.frame(temp=5)) # Beware of extrapolation! #2 beer<-read.delim2("clipboard") summary(beer) cor.test(beer$beer, beer$IQ) # Pearson's product-moment correlation # # data: beer$beer and beer$IQ # t = -0.51757, df = 10, p-value = 0.616 # alternative hypothesis: true correlation is not equal to 0 # 95 percent confidence interval: # -0.6730350 0.4545116 # sample estimates: # cor # -0.1615223 summary(lm(beer$beer~beer$IQ))# The lm gives the same result as Pearson correlation # Call: # lm(formula = beer$beer ~ beer$IQ) # # Residuals: # Min 1Q Median 3Q Max # -2.4630 -1.7059 -1.1746 0.6413 5.5177 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 5.56435 4.95234 1.124 0.287 # beer$IQ -0.02802 0.05413 -0.518 0.616 # # Residual standard error: 2.71 on 10 degrees of freedom # Multiple R-squared: 0.02609, Adjusted R-squared: -0.0713 # F-statistic: 0.2679 on 1 and 10 DF, p-value: 0.616 # The scatterplot should not contain any regression line if not significant. ggplot(beer, aes(x=beer, y=IQ))+geom_point()+theme_classic()