Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

ASSIGNMENT 1

ANJAN SADHUKHAN
January 2024

1 QUESTION 1:
Consider the dataset about Amazon books stored given in the class web- page.
Use R to compute the multiple correlation between Amazon Price and (NumPages,
Height, Width and Thickness). You may use direct computation or a package.

1.1 SOLUTION:
The required R code is as follows:

dir()

## [1] "amazon books.txt" "ANJ..pdf" "ANJAN.pdf"


## [4] "main.Rtex" "output.aux" "output.fdb_latexmk"
## [7] "output.fls" "output.log"

x = read.delim('amazon books.txt') #read.delim function is used to read manual symbol sepera


dim(x)#measures number of rows and columns

## [1] 325 13

any(is.na(x))#check whether any blank place is present in the matrix

## [1] TRUE

d = na.omit(x)#omit those blank spaces


dim(d)

## [1] 311 13

names(d)

## [1] "Title" "Author" "List.Price" "Amazon.Price" "Hard..Paper"


## [6] "NumPages" "Publisher" "Pub.year" "ISBN.10" "Height"
## [11] "Width" "Thick" "Weight..oz."

1
dat = d[,c(4,6,10,11,12)]
names(dat)

## [1] "Amazon.Price" "NumPages" "Height" "Width" "Thick"

names(dat) = c('ap','p','h','w','t')
names(dat)

## [1] "ap" "p" "h" "w" "t"

fit = lm(ap~p+h+w+t,data = dat)


summary(fit)

##
## Call:
## lm(formula = ap ~ p + h + w + t, data = dat)
##
## Residuals:
## Min 1Q Median 3Q Max
## -29.614 -3.612 -1.424 1.310 106.933
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -39.793067 5.530803 -7.195 4.85e-12 ***
## p 0.024552 0.006095 4.028 7.09e-05 ***
## h 3.557642 0.754097 4.718 3.63e-06 ***
## w 4.442789 0.774608 5.736 2.33e-08 ***
## t -10.519479 2.725880 -3.859 0.000139 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 9.912 on 306 degrees of freedom
## Multiple R-squared: 0.275,Adjusted R-squared: 0.2655
## F-statistic: 29.02 on 4 and 306 DF, p-value: < 2.2e-16

y = summary(fit)$r.sq
y

## [1] 0.2749882

1-sum(fit$resid^2)/((311-1)*var(dat$ap))#measures multiple correlation

## [1] 0.2749882

Hence the multiple correlation between Amazon Price and (NumPages, Height,
Width and Thickness) is 0.2749882

2
2 QUESTION 2:
Use the same data set once again to compute partial correlation between List
Price and Amazon Price given Weight and Thick. You may use direct compu-
tation or a package.

2.1 SOLUTION:
The required r code is as follows:

dir()
## [1] "amazon books.txt" "ANJ..pdf" "ANJAN.pdf"
## [4] "main.Rtex" "output.aux" "output.fdb_latexmk"
## [7] "output.fls" "output.log"
x = read.delim('amazon books.txt')
dim(x)
## [1] 325 13
any(is.na(x))
## [1] TRUE
d = na.omit(x)
dim(d)
## [1] 311 13
dat = d[,c(3,4,12,13)]
names(dat) = c('lp','ap','t','w')
ordered.data = with(dat,data.frame(w,t,lp,ap))
head(ordered.data)
## w t lp ap
## 1 11.2 0.8 12.95 5.18
## 2 7.2 0.7 15.00 10.20
## 3 4.0 0.3 1.50 1.50
## 4 28.8 1.6 15.99 10.87
## 5 22.4 1.4 30.50 16.77
## 6 32.0 1.7 28.95 16.44
s = cov(ordered.data)
s
## w t lp ap
## w 43.715934 1.59876392 34.6549753 21.92924277
## t 1.598764 0.12603568 0.4962158 -0.02784759
## lp 34.654975 0.49621577 175.9936710 146.13203164
## ap 21.929243 -0.02784759 146.1320316 133.76059427

3
A = s[1:2,1:2]
B = s[1:2,3:4]
C = s[3:4,3:4]
pcov = C - (t(B) %*% solve(A) %*% B) #pcov is the schur's complement
pcov

## lp ap
## lp 139.7197 119.2766
## ap 119.2766 112.5683

pcov[1,2]/sqrt(pcov[1,1]*pcov[2,2]) #measures the partial correlation'

## [1] 0.9510829

Hence, partial correlation between List Price and Amazon Price given Weight
and Thick is 0.9510829

3 QUESTION 3:
Let X1, ..., Xp, Y be p + 1 variables with n observations of each. Based on this
n × (p + 1) data matrix, prove that the following two definitions of multiple
correlation are equivalent:
(i) maxcor(Y, a0 + a1 X1 + + ap Xp ) : ai belongsto(R)
(ii) cor(Y, aˆ0 + aˆ1X1 ++aˆpXp ), whereaˆiareobtainedf romlinearregressionof Y onXi′ susingleastsquares.
You may use the “right angled triangle argument” given in class as a moti-
vation, but your proof must be algebraic and self-contained.

3.1 SOLUTION:

You might also like