You are on page 1of 7

>

> wash=c(4,5,6,5,7,9,8,12,10,12,11,9,6,6,4,4,13,15,12,12,12,13,10,13)
> mean(wash)
[1] 9.083333
>
> deter=factor(c(rep(1,12), rep(2,12)))
> deter
[1] 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2
Levels: 1 2
> water=factor(c(rep(1,4), rep(2,4), rep(3,4), rep(1,4), rep(2,4), rep(3,4)))
> water
[1] 1 1 1 1 2 2 2 2 3 3 3 3 1 1 1 1 2 2 2 2 3 3 3 3
Levels: 1 2 3
> water=factor(rep(gl(3,4),2))
> water
[1] 1 1 1 1 2 2 2 2 3 3 3 3 1 1 1 1 2 2 2 2 3 3 3 3
Levels: 1 2 3
>
> ?tapply
starting httpd help server ... done
>
> tapply(wash, water, mean)
1
2
3
5.00 11.00 11.25
>
> tapply(wash, deter, mean)
1
2
8.166667 10.000000
>
> tapply(wash, water:deter, mean)
1:1 1:2 2:1 2:2 3:1 3:2
5.0 5.0 9.0 13.0 10.5 12.0
>
> lm.deter=lm(wash~deter+water+deter*water)
> lm.deter
Call:
lm(formula = wash ~ deter + water + deter * water)
Coefficients:
(Intercept)
2:water3
5.000e+00
.500e+00

deter2

water2

-1.201e-15

4.000e+00

water3 deter2:water2 deter


5.500e+00

4.000e+00

> anova(lm.deter)
Analysis of Variance Table
Response: wash
Df
deter
1
water
2
deter:water 2
Residuals 18
--Signif. codes:
>
>
>

Sum Sq Mean Sq
20.167 20.167
200.333 100.167
16.333 8.167
37.000 2.056
0 ***

0.001

F value Pr(>F)
9.8108 0.005758 **
48.7297 5.44e-08 ***
3.9730 0.037224 *
**

0.01

* 0.05

0.1

>
>
> summary(lm.deter)
Call:
lm(formula = wash ~ deter + water + deter * water)
Residuals:
Min
1Q Median
-2
-1
0

3Q
1

Max
3

Coefficients:
Estimate Std. Error t value
(Intercept)
5.000e+00 7.169e-01 6.975
deter2
-1.201e-15 1.014e+00 0.000
water2
4.000e+00 1.014e+00 3.946
water3
5.500e+00 1.014e+00 5.425
deter2:water2 4.000e+00 1.434e+00 2.790
deter2:water3 1.500e+00 1.434e+00 1.046
--Signif. codes: 0 *** 0.001 ** 0.01 *

Pr(>|t|)
1.63e-06
1.000000
0.000948
3.73e-05
0.012094
0.309302
0.05

***
***
***
*
0.1

Residual standard error: 1.434 on 18 degrees of freedom


Multiple R-squared: 0.8649,
Adjusted R-squared: 0.8273
F-statistic: 23.04 on 5 and 18 DF, p-value: 3.049e-07
>
>
>
> women=read.csv("C:\\Users\\Sitanshu\\Downloads\\Women-in-work-force.csv")
> women
City X1972 X1968
1
N.Y. 0.45 0.42
2
L.A. 0.50 0.50
3
Chicago 0.52 0.52
4 Philadelphia 0.45 0.45
5
Detroit 0.46 0.43
6 San Francisco 0.55 0.55
7
Boston 0.60 0.45
8
Pitt. 0.49 0.34
9
St. Louis 0.35 0.45
10 Connecticut 0.55 0.54
11 Wash., D.C. 0.52 0.42
12
Cinn. 0.53 0.51
13
Baltimore 0.57 0.49
14
Newark 0.53 0.54
15 Minn/St. Paul 0.59 0.50
16
Buffalo 0.64 0.58
17
Houston 0.50 0.49
18
Patterson 0.57 0.56
19
Dallas 0.64 0.63
>
> y=women[,1]
> y=women[,2]
> x=women[,3]
> y
[1] 0.45 0.50 0.52 0.45 0.46 0.55 0.60 0.49 0.35 0.55 0.52 0.53 0.57 0.53 0.59
0.64 0.50 0.57 0.64
> x
[1] 0.42 0.50 0.52 0.45 0.43 0.55 0.45 0.34 0.45 0.54 0.42 0.51 0.49 0.54 0.50

0.58 0.49 0.56 0.63


>
> cor(cbind(y,x))
y
x
y 1.0000000 0.6300734
x 0.6300734 1.0000000
>
> reg=lm(y~x)
> summary(reg)
Call:
lm(formula = y ~ x)
Residuals:
Min
1Q
Median
3Q
Max
-0.148529 -0.026490 -0.007891 0.043191 0.101471
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.20331
0.09757 2.084 0.05260 .
x
0.65604
0.19610 3.345 0.00383 **
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 .

0.1

Residual standard error: 0.05657 on 17 degrees of freedom


Multiple R-squared: 0.397,
Adjusted R-squared: 0.3615
F-statistic: 11.19 on 1 and 17 DF, p-value: 0.003835
> reg=lm(y~x+ln(x))
Error in eval(expr, envir, enclos) : could not find function "ln"
> reg=lm(y~x+log(x))
> summary(reg)
Call:
lm(formula = y ~ x + log(x))
Residuals:
Min
1Q Median
3Q
Max
-0.13794 -0.01964 -0.01218 0.02563 0.11206
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -2.0911
1.6064 -1.302 0.2114
x
3.4013
1.9281 1.764 0.0968 .
log(x)
-1.3130
0.9177 -1.431 0.1717
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 .

0.1

Residual standard error: 0.0549 on 16 degrees of freedom


Multiple R-squared: 0.4654,
Adjusted R-squared: 0.3986
F-statistic: 6.964 on 2 and 16 DF, p-value: 0.006672
>
> reg=lm(y~x+log(x))
>
>
>
> new=read.csv("C:\\Users\\Sitanshu\\Desktop\\test.csv")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:

In file(file, "rt") :
cannot open file 'C:\Users\Sitanshu\Desktop\test.csv': No such file or directo
ry
>
> new=read.csv("C:\\Users\\Sitanshu\\Desktop\\Test.csv")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'C:\Users\Sitanshu\Desktop\Test.csv': No such file or directo
ry
>
>
>
> new=read.csv("C:\\Users\\Sitanshu\\Desktop\\Test.csv")
> new
EX ECAB MET GROW YOUNG OLD WEST STATE
1 256 85.5 19.7 6.9 29.6 11.0
0
ME
2 275 94.3 17.7 14.7 26.4 11.2
0
NH
3 327 87.0 0.0 3.7 28.5 11.2
0
VT
4 297 107.5 85.2 10.2 25.1 11.1
0
MA
5 256 94.9 86.2 1.0 25.3 10.4
0
RI
6 312 121.6 77.6 25.4 25.2 9.6
0
CT
7 374 111.5 85.5 12.9 24.0 10.1
0
NY
8 257 117.9 78.9 25.5 24.8 9.2
0
NJ
9 257 103.1 77.9 7.8 25.7 10.0
0
PA
10 336 116.1 68.8 39.9 26.4 8.0
0
DE
11 269 93.4 78.2 31.1 27.5 7.3
0
MD
12 213 77.2 50.9 21.9 28.8 7.3
0
VA
13 308 108.4 73.1 22.2 28.0 8.2
0
MI
14 273 111.8 69.5 21.8 26.9 9.2
0
OH
15 256 110.8 48.1 18.3 27.5 9.6
0
IN
16 287 120.9 76.9 15.5 25.4 9.7
0
IL
17 290 104.3 46.3 14.9 27.4 10.2
0
WI
18 217 85.1 30.9 -7.4 30.0 9.3
0
WV
>
>
>
> y=new[,2]
> y
[1] 85.5 94.3 87.0 107.5 94.9 121.6 111.5 117.9 103.1 116.1 93.4 77.2 108
.4 111.8 110.8 120.9 104.3 85.1
>
> y=new[,1]
> x1=new[,2]
> x2=new[,3]
> x3=new[,4]
> x4=new[.5]
> x5=new[,6]
> x4=new[,5]
> x6=new[,7]]
Error: unexpected ']' in "x6=new[,7]]"
> x6=new[,7]
> x6
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>
>
> new=read.csv("C:\\Users\\Sitanshu\\Desktop\\Test.csv")
> x1=new[,2]
> y=new[,1]
> x2=new[,3]

>
>
>
>
>
>

x3=new[,4]
x4=new[,5]
x5=new[,6]
x6=new[,7]
x6
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1

>
>
>
> cor(cbind(y,x1,x2,x3,x4,x5,x6))
y
x1
x2
x6
y 1.00000000 0.65586251 0.04523511
48726
x1 0.65586251 1.00000000 0.40892636
92617
x2 0.04523511 0.40892636 1.00000000
82668
x3 0.40528659 0.46007220 0.40402333
55095
x4 -0.29319692 -0.58946801 -0.62627957
07582
x5 -0.02339611 -0.04449636 -0.04105316
09663
x6 0.37348726 0.03992617 -0.33082668
00000
>
>
> reg=lm(y~x1+x2+x3+x4+x5+x6)
> summary(reg)

x3

x4

x5

0.40528659 -0.2931969 -0.02339611 0.373


0.46007220 -0.5894680 -0.04449636 0.039
0.40402333 -0.6262796 -0.04105316 -0.330
1.00000000 -0.2044875 -0.41258234 0.084
-0.20448754 1.0000000 -0.52492921 0.289
-0.41258234 -0.5249292 1.00000000 -0.041
0.08455095 0.2890758 -0.04109663 1.000

Call:
lm(formula = y ~ x1 + x2 + x3 + x4 + x5 + x6)
Residuals:
Min
1Q Median
3Q
Max
-75.031 -26.226 0.296 23.256 87.653
Coefficients:
Estimate Std. Error t value
(Intercept) 356.1818 306.4862 1.162
x1
1.4185
0.4300 3.298
x2
-0.6602
0.3526 -1.872
x3
0.5716
0.4251 1.345
x4
-6.6747
7.4806 -0.892
x5
-1.8551
7.1368 -0.260
x6
35.4723
13.7711 2.576
--Signif. codes: 0 *** 0.001 ** 0.01

Pr(>|t|)
0.25190
0.00202 **
0.06832 .
0.18615
0.37746
0.79622
0.01370 *
* 0.05

0.1

Residual standard error: 39.84 on 41 degrees of freedom


Multiple R-squared: 0.5994,
Adjusted R-squared: 0.5408
F-statistic: 10.22 on 6 and 41 DF, p-value: 6.634e-07
> reg=lm(y~x1+x6)
> summary(reg)
Call:

lm(formula = y ~ x1 + x6)
Residuals:
Min
1Q Median
3Q
Max
-78.863 -26.251 -0.334 22.355 97.849
Coefficients:
Estimate Std. Error t
(Intercept) 102.2957
26.6247
x1
1.6962
0.2641
x6
40.4759
11.6326
--Signif. codes: 0 *** 0.001 **

value
3.842
6.422
3.480
0.01

Pr(>|t|)
0.00038 ***
7.38e-08 ***
0.00113 **
* 0.05

0.1

Residual standard error: 40.26 on 45 degrees of freedom


Multiple R-squared: 0.551,
Adjusted R-squared: 0.531
F-statistic: 27.61 on 2 and 45 DF, p-value: 1.501e-08
> summary(reg)y1 = c(18.2, 20.1, 17.6, 16.8, 18.8, 19.7, 19.1)
Error: unexpected symbol in "summary(reg)y1"
> y1 = c(18.2, 20.1, 17.6, 16.8, 18.8, 19.7, 19.1)
> y2 = c(17.4, 18.7, 19.1, 16.4, 15.9, 18.4, 17.7)
> y3 = c(15.2, 18.8, 17.7, 16.5, 15.9, 17.1, 16.7)
> y=c(y1,y2,y3)
> y
[1] 18.2 20.1 17.6 16.8 18.8 19.7 19.1 17.4 18.7 19.1 16.4 15.9 18.4 17.7 15.2
18.8 17.7 16.5 15.9 17.1 16.7
> n=c(rep(1,7),rep(2,7),rep(3,7))
> n
[1] 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3
> tapply(y,y1,mean)
Error in tapply(y, y1, mean) : arguments must have same length
> tapply(y,y1,mean(y))
Error in match.fun(FUN) :
'mean(y)' is not a function, character or symbol
> lm.aa=lm(y~y1+y2+y3)
Error in model.frame.default(formula = y ~ y1 + y2 + y3, drop.unused.levels = TR
UE) :
variable lengths differ (found for 'y1')
> fit=lm(y)
Error in formula.default(object, env = baseenv()) : invalid formula
> fit=lm(y~n)
> fit
Call:
lm(formula = y ~ n)
Coefficients:
(Intercept)
19.4762

n
-0.8857

> annova(fit)
Error: could not find function "annova"
> anova(fit)
Analysis of Variance Table
Response: y
Df Sum Sq Mean Sq F value Pr(>F)
n
1 10.983 10.9829 8.3514 0.009385 **
Residuals 19 24.987 1.3151

--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05


>
>
> n=c(rep(1,7),rep(2,7),rep(3,7))
> n1=factor(n)
> n1
[1] 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3
Levels: 1 2 3
> fit=lm(y~n1)
> anova(fit)
Analysis of Variance Table

0.1

Response: y
Df Sum Sq Mean Sq F value Pr(>F)
n1
2 11.007 5.5033 3.9683 0.03735 *
Residuals 18 24.963 1.3868
--Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1
> save.image("C:\\Users\\Sitanshu\\Desktop\\anova")
>

You might also like