You are on page 1of 8

R version 3.1.

3 (2015-03-09) -- "Smooth Sidewalk"


Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin13.4.0 (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
Natural language support but running in an English locale
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
[R.app GUI 1.65 (6912) x86_64-apple-darwin13.4.0]
[History restored from /Users/AlexG/.Rapp.history]
> getwd
function ()
.Internal(getwd())
<bytecode: 0x7f95a29342a8>
<environment: namespace:base>
> getwd()
[1] "/Users/AlexG"
> setwd(/Users/AlexG/Documents/ST221)
Error: unexpected '/' in "setwd(/"
> setwd(""/Users/AlexG/Documents/ST221")
+ setwd("/Users/AlexG/Documents/ST221")
Error: unexpected string constant in:
"setwd(""/Users/AlexG/Documents/ST221")
setwd(""
> setwd("/Users/AlexG/Documents/ST221")
> data1 <- read.csv(file="courseworkdata3.csv", head=TRUE, sep=",")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'courseworkdata3.csv': No such file or directory
> heights <- data1[, 'height']
Error: object 'data1' not found
> yeasts <- data1[, 'yeast']
Error: object 'data1' not found
> seeds <- data1[, 'seeds']
Error: object 'data1' not found
> salts <- data1[, 'salt']
Error: object 'data1' not found
> yeastsA <- c(1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0)
> yeastsB <- c(0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0)
> yeastsC <- c(0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0)
> yeastsD <- c(0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1)
>
> m1 <- lm(heights ~ yeastsA + yeastsB + yeastsC + yeastsD + seeds + salts)
Error in eval(expr, envir, enclos) : object 'heights' not found
> coefficients(m1)
Error in coefficients(m1) : object 'm1' not found
>
> #1(c)
> anova(m1) #Generates the
Error in anova(m1) : object 'm1' not found
> getwd()
[1] "/Users/AlexG/Documents/ST221"
> setwd("/Users/AlexG/Documents/ST221/Coursework 2")
> data1 <- read.csv(file="courseworkdata3.csv", head=TRUE, sep=",")
> heights <- data1[, 'height']
> yeasts <- data1[, 'yeast']
> seeds <- data1[, 'seeds']
> salts <- data1[, 'salt']
> yeastsA <- c(1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0)
> yeastsB <- c(0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0)
> yeastsC <- c(0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0)
> yeastsD <- c(0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1)
> m1 <- lm(heights ~ yeastsA + yeastsB + yeastsC + yeastsD + seeds + salts)
> coefficients(m1)
(Intercept) yeastsA yeastsB yeastsC yeastsD seeds
30.235201 3.241137 1.224206 2.215300 NA -1.646760
salts
-1.225751
> #1(c)
> anova(m1) #Generates the
Analysis of Variance Table
Response: heights
Df Sum Sq Mean Sq F value Pr(>F)
yeastsA 1 16.5675 16.5675 7.2070 0.02291 *
yeastsB 1 1.0838 1.0838 0.4714 0.50793
yeastsC 1 5.9512 5.9512 2.5889 0.13870
seeds 1 7.0225 7.0225 3.0549 0.11107
salts 1 8.6645 8.6645 3.7692 0.08089 .
Residuals 10 22.9880 2.2988
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
> #2(a)
> data2 <- read.csv(file="courseworkdata4.csv", head=TRUE, sep=",")
> profit <- data2[, profit ]
> men <- data2[, men ]
> women <- data2[, women ]
> boys <- data2[, boys ]
> girls <- data2[, girls ]
>
> m3 <- lm(profit ~ men + women + boys + girls)
> summary(m3)
Call:
lm(formula = profit ~ men + women + boys + girls)
Residuals:
Min 1Q Median 3Q Max
-164.87 -69.95 16.46 51.73 159.75
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1115.702 125.976 -8.856 2.41e-07 ***
men 9.617 3.051 3.152 0.006576 **
women 10.862 2.534 4.286 0.000649 ***
boys 27.145 12.644 2.147 0.048561 *
girls 19.917 10.257 1.942 0.071186 .
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 97.3 on 15 degrees of freedom
Multiple R-squared: 0.9303, Adjusted R-squared: 0.9117
F-statistic: 50.03 on 4 and 15 DF, p-value: 1.688e-08
> #betahat <- c(-1115.702, 9.617, 10.862, 19.917, 27.145)
> #data2m <- as.matrix(data2)
> #Z <- cbind(c(rep.int(1, 20)), (data2m[, c(2:5)]))
> #ssq <- (1/15)*((t(Z%*%betahat))%*%(Z%*%betahat))
>
> (1/15)*sum((profit-fitted(m3))^2)
[1] 9467.148
>
> #2(b)
> mmen <- lm(profit ~ men)
> mwomen <- lm(profit ~ women)
> mboys <- lm(profit ~ boys)
> mgirls <- lm(profit ~ girls)
>
> summary(mmen)
Call:
lm(formula = profit ~ men)
Residuals:
Min 1Q Median 3Q Max
-356.55 -93.87 12.38 90.34 329.45
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -864.146 179.766 -4.807 0.000141 ***
men 21.056 2.697 7.806 3.47e-07 ***
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 160.6 on 18 degrees of freedom
Multiple R-squared: 0.772, Adjusted R-squared: 0.7593
F-statistic: 60.93 on 1 and 18 DF, p-value: 3.47e-07
> summary(mwomen)
Call:
lm(formula = profit ~ women)
Residuals:
Min 1Q Median 3Q Max
-292.230 -70.239 -3.667 109.009 212.795
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -854.457 139.668 -6.118 8.86e-06 ***
women 18.883 1.889 9.999 8.95e-09 ***
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 131.4 on 18 degrees of freedom
Multiple R-squared: 0.8474, Adjusted R-squared: 0.839
F-statistic: 99.98 on 1 and 18 DF, p-value: 8.947e-09
> summary(mboys)
Call:
lm(formula = profit ~ boys)
Residuals:
Min 1Q Median 3Q Max
-486.71 -271.25 -96.31 339.62 482.52
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 425.85 183.31 2.323 0.0321 *
boys 20.47 40.34 0.508 0.6180
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 334 on 18 degrees of freedom
Multiple R-squared: 0.01411, Adjusted R-squared: -0.04066
F-statistic: 0.2576 on 1 and 18 DF, p-value: 0.618
> summary(mgirls)
Call:
lm(formula = profit ~ girls)
Residuals:
Min 1Q Median 3Q Max
-471.67 -222.46 -58.59 259.69 477.22
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 189.36 154.03 1.229 0.2348
girls 63.65 27.55 2.310 0.0329 *
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 295.4 on 18 degrees of freedom
Multiple R-squared: 0.2287, Adjusted R-squared: 0.1858
F-statistic: 5.337 on 1 and 18 DF, p-value: 0.03294
>
> #2(d)
> deviance(mmen)
[1] 464442.4
> deviance(mwomen)
[1] 310741
> deviance(mboys)
[1] 2007946
> deviance(mgirls)
[1] 1570915
>
> anova(m3)
Analysis of Variance Table
Response: profit
Df Sum Sq Mean Sq F value Pr(>F)
men 1 1572237 1572237 166.0729 1.622e-09 ***
women 1 254630 254630 26.8961 0.0001106 ***
boys 1 32110 32110 3.3917 0.0853855 .
girls 1 35696 35696 3.7705 0.0711863 .
Residuals 15 142007 9467
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
> summary(m3)
Call:
lm(formula = profit ~ men + women + boys + girls)
Residuals:
Min 1Q Median 3Q Max
-164.87 -69.95 16.46 51.73 159.75
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1115.702 125.976 -8.856 2.41e-07 ***
men 9.617 3.051 3.152 0.006576 **
women 10.862 2.534 4.286 0.000649 ***
boys 27.145 12.644 2.147 0.048561 *
girls 19.917 10.257 1.942 0.071186 .
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 97.3 on 15 degrees of freedom
Multiple R-squared: 0.9303, Adjusted R-squared: 0.9117
F-statistic: 50.03 on 4 and 15 DF, p-value: 1.688e-08
> anova(m1)
Analysis of Variance Table
Response: heights
Df Sum Sq Mean Sq F value Pr(>F)
yeastsA 1 16.5675 16.5675 7.2070 0.02291 *
yeastsB 1 1.0838 1.0838 0.4714 0.50793
yeastsC 1 5.9512 5.9512 2.5889 0.13870
seeds 1 7.0225 7.0225 3.0549 0.11107
salts 1 8.6645 8.6645 3.7692 0.08089 .
Residuals 10 22.9880 2.2988
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
> anova(m3)
Analysis of Variance Table
Response: profit
Df Sum Sq Mean Sq F value Pr(>F)
men 1 1572237 1572237 166.0729 1.622e-09 ***
women 1 254630 254630 26.8961 0.0001106 ***
boys 1 32110 32110 3.3917 0.0853855 .
girls 1 35696 35696 3.7705 0.0711863 .
Residuals 15 142007 9467
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
> qf(4, 15, 0.95)
[1] NaN
Warning message:
In qf(4, 15, 0.95) : NaNs produced
> qf(0.95, 4, 15)
[1] 3.055568
> summary(m3)
Call:
lm(formula = profit ~ men + women + boys + girls)
Residuals:
Min 1Q Median 3Q Max
-164.87 -69.95 16.46 51.73 159.75
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1115.702 125.976 -8.856 2.41e-07 ***
men 9.617 3.051 3.152 0.006576 **
women 10.862 2.534 4.286 0.000649 ***
boys 27.145 12.644 2.147 0.048561 *
girls 19.917 10.257 1.942 0.071186 .
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 97.3 on 15 degrees of freedom
Multiple R-squared: 0.9303, Adjusted R-squared: 0.9117
F-statistic: 50.03 on 4 and 15 DF, p-value: 1.688e-08
> qt(0.99, 15)
[1] 2.60248
> qt(0.95, 15)
[1] 1.75305
> mmen <- lm(profit ~ men)
> mwomen <- lm(profit ~ women)
> mboys <- lm(profit ~ boys)
> mgirls <- lm(profit ~ girls)
>
> summary(mmen)
Call:
lm(formula = profit ~ men)
Residuals:
Min 1Q Median 3Q Max
-356.55 -93.87 12.38 90.34 329.45
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -864.146 179.766 -4.807 0.000141 ***
men 21.056 2.697 7.806 3.47e-07 ***
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 160.6 on 18 degrees of freedom
Multiple R-squared: 0.772, Adjusted R-squared: 0.7593
F-statistic: 60.93 on 1 and 18 DF, p-value: 3.47e-07
> summary(mwomen)
Call:
lm(formula = profit ~ women)
Residuals:
Min 1Q Median 3Q Max
-292.230 -70.239 -3.667 109.009 212.795
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -854.457 139.668 -6.118 8.86e-06 ***
women 18.883 1.889 9.999 8.95e-09 ***
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 131.4 on 18 degrees of freedom
Multiple R-squared: 0.8474, Adjusted R-squared: 0.839
F-statistic: 99.98 on 1 and 18 DF, p-value: 8.947e-09
> summary(mboys)
Call:
lm(formula = profit ~ boys)
Residuals:
Min 1Q Median 3Q Max
-486.71 -271.25 -96.31 339.62 482.52
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 425.85 183.31 2.323 0.0321 *
boys 20.47 40.34 0.508 0.6180
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 334 on 18 degrees of freedom
Multiple R-squared: 0.01411, Adjusted R-squared: -0.04066
F-statistic: 0.2576 on 1 and 18 DF, p-value: 0.618
> summary(mgirls)
Call:
lm(formula = profit ~ girls)
Residuals:
Min 1Q Median 3Q Max
-471.67 -222.46 -58.59 259.69 477.22
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 189.36 154.03 1.229 0.2348
girls 63.65 27.55 2.310 0.0329 *
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1 1
Residual standard error: 295.4 on 18 degrees of freedom
Multiple R-squared: 0.2287, Adjusted R-squared: 0.1858
F-statistic: 5.337 on 1 and 18 DF, p-value: 0.03294
>
> deviance(mmen)
[1] 464442.4
> deviance(mwomen)
[1] 310741
> deviance(mboys)
[1] 2007946
> deviance(mgirls)
[1] 1570915
>

You might also like