Use the formula of partioned matrix
a11 = 1;
b = cbind(2,6,5,9)
C = matrix(c(6,2,7,2,2,5,4,1,7,4,8,3,2,1,3,7),nrow=4,byrow=TRUE)
det_A = a11*det(C - 1/a11*t(b)%*%b)
mean.X= 1*.2+3*.15+7*.3+11*.05+17*.1+19*.2;
var.X = 1^2*.2+3^2*.15+7^2*.3+11^2*.05+17^2*.1+19^2*.2 - mean.X^2
MD.X = abs(1-mean.X)*.2+abs(3-mean.X)*.15+abs(7-mean.X)*.3+abs(11-mean.X)*.05+abs(17-mean.X)*.1+abs(19-mean.X)*.2;
c(mean.X,var.X,MD.X)
## [1] 8.80 45.96 5.94
DF = data.frame(X=c(1,3,7,11,17,19),F=cumsum(c(.2,.15,.3,.05,.1,.2)));
DF
## X F
## 1 1 0.20
## 2 3 0.35
## 3 7 0.65
## 4 11 0.70
## 5 17 0.80
## 6 19 1.00
n = 5
d = 1:n #an example
# First Method using the diag function
D = diag(d)
# Second Method
vec = numeric(n*n) #constructing the vector of zeros of length n
fill = seq(1,n*n,n+1) #getting the locations to put the values
vec[fill] = d #replacing the 0's by the given values
D = matrix(vec,nrow=5) #defining the matrix
#First Method
inv.D1 = solve(D)
inv.D1
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 0.0 0.0000000 0.00 0.0
## [2,] 0 0.5 0.0000000 0.00 0.0
## [3,] 0 0.0 0.3333333 0.00 0.0
## [4,] 0 0.0 0.0000000 0.25 0.0
## [5,] 0 0.0 0.0000000 0.00 0.2
#Second Method
eigen.D = diag(D) # The eigen values of the diagonal matrix are the diagonal elements
eigen_inv.D = 1/eigen.D # The eigen values of the inverse are the reciprocals of the eigen values of the matrix D
inv.D2 = diag(eigen_inv.D) # As the inverse of a diagonal matrix is also a diagonal matrix
# First Method
inv.D2[1:2,1:2] #selecting rows and columns
## [,1] [,2]
## [1,] 1 0.0
## [2,] 0 0.5
# Second Method
inv.D2[-(3:n),-(3:n)] #eleminating rows and columns
## [,1] [,2]
## [1,] 1 0.0
## [2,] 0 0.5
data=read.table(header = TRUE, text =
"Judge-1 Judge-2
25 34
34 31
62 51
48 49
74 80
31 27
82 75
67 71
15 20
45 41")
# converting data-frame to a matrix
data.matrix = as.matrix(data)
# Spearman Correlation
cor(data[,1],data[,2],method="spearman")
## [1] 0.9515152
#generating a sample of size 120 from exp(mean=12)
n1 = 120;
mean = 12;
samples_exp = rexp(n1,1/mean);
samples_exp
## [1] 3.868065271 7.473183272 3.013327410 1.824546181 19.629496626
## [6] 9.751811584 13.536340512 4.426716210 20.671429624 2.456048850
## [11] 14.907640691 22.902480460 1.465701196 4.226328213 1.501106003
## [16] 26.671213722 10.273111760 5.186653920 6.359012615 16.691922582
## [21] 0.559893368 24.078964299 9.013481908 5.490860598 11.027924134
## [26] 49.944173676 3.156476749 6.672240844 9.136236894 16.308433782
## [31] 6.861529045 3.373345893 4.161959086 3.168780840 22.124590936
## [36] 2.820222840 3.037444363 19.672006634 10.765823309 7.942635203
## [41] 14.019091330 21.279099484 2.608846737 3.977929741 5.930999642
## [46] 2.457569780 3.433507049 24.323598615 19.684049762 4.089008398
## [51] 6.898885030 4.967367668 8.984296202 2.005150163 2.350840164
## [56] 4.117067823 1.026359556 4.462291438 16.454677976 19.857503576
## [61] 4.148158757 11.718839786 4.748538796 5.782021981 9.505161244
## [66] 10.721571408 7.629320176 25.073826557 11.064776122 0.628753983
## [71] 21.512933648 35.599851785 1.328337450 9.855592723 7.093426460
## [76] 1.442049785 1.907839358 17.912229386 16.052861206 3.337722001
## [81] 2.528216114 4.477674295 0.607334387 0.009479234 5.684661962
## [86] 8.079217857 15.853277429 20.891179203 9.939577837 4.289229350
## [91] 32.053683466 5.669996890 34.020052133 7.615519663 8.542113415
## [96] 6.493012235 3.002844673 7.684742276 8.946283880 1.189000374
## [101] 24.635233908 6.260283882 11.688767239 38.081799142 3.773452235
## [106] 14.678166568 18.284419254 6.538682453 1.079784783 18.439564177
## [111] 11.454632636 12.654920116 37.930454344 1.092585262 3.068642121
## [116] 14.645697478 11.068109504 4.594621584 9.462605595 33.732950637
#generating a sample of size 150 from log-normal(mu=2,sigma^2=1)
n2 = 150;
mu = 2;
sigma = 1;
samples_lnorm = rlnorm(n2,mu,sigma)
samples_lnorm
## [1] 2.0420153 37.3464143 3.5780743 3.2396343 27.9839794 62.7747725
## [7] 4.3651654 12.4768217 3.6596256 3.5540025 9.0334797 2.3154185
## [13] 31.8517242 14.8456580 1.6983070 1.8838684 4.9831713 9.8608055
## [19] 5.2852074 28.8061281 9.4084966 42.2703588 3.5687750 2.6804460
## [25] 14.7795133 3.1985742 4.8118915 40.1491616 6.6060421 18.8704999
## [31] 9.1505552 15.3641533 1.5299432 3.7346155 6.7935536 7.4849567
## [37] 11.2807615 31.1092882 1.4624091 15.1796771 12.8265120 6.7242762
## [43] 4.6408259 22.9792565 31.3910676 13.3020074 4.8948239 44.5541946
## [49] 48.2549711 18.2138651 20.1454947 11.9798126 7.1938720 14.9641707
## [55] 2.9312338 13.8222126 7.0111499 2.4196800 5.1336913 10.1630574
## [61] 16.1534580 5.3420488 0.7526039 14.3359811 8.3306150 6.1594483
## [67] 1.9752783 0.5790556 12.1056108 3.6220766 39.3959678 15.7535058
## [73] 2.1037727 5.1665560 1.2627044 12.6498842 4.1964033 81.7708869
## [79] 5.4341440 4.0366680 1.7239649 3.6261921 3.8145681 3.9974021
## [85] 6.4668358 5.4845769 2.3154829 2.9582081 23.7671993 5.8260556
## [91] 11.9017884 8.9829641 16.3792527 15.0196348 11.7025820 2.0466986
## [97] 1.9682191 2.8613034 13.8880314 7.5518416 2.6284437 3.5933812
## [103] 3.5336831 4.2144651 4.8412496 4.3110131 2.2270920 12.1647214
## [109] 8.4505290 34.9840796 3.8829804 38.8727483 14.7136428 9.0319235
## [115] 34.3147510 3.8705318 5.3719649 9.8543146 6.1820428 43.6842738
## [121] 16.0606533 2.5839256 10.5097524 2.1903332 4.5379337 4.9088511
## [127] 5.4805083 2.8162260 5.3826818 2.8403094 7.8109393 39.6806159
## [133] 1.6177185 0.9928882 9.7766588 11.4771857 0.8304193 4.8046291
## [139] 4.4737218 3.2933064 8.8875244 4.8938758 27.1782245 1.1192905
## [145] 17.0178307 30.6506449 3.1139850 6.6659892 2.3270794 1.3039154
# Measures of Central Tendency, Dispersion, Skewness and Kurtosis
#For Exponential Samples
e.mean = mean(samples_exp)
e.sd = sd(samples_exp)
e.sk = mean((samples_exp-e.mean)^3)/e.sd^(3)
e.kur = mean((samples_exp-e.mean)^4)/e.sd^(4)
c(mean=e.mean,sd=e.sd,skewness=e.sk,kurtosis=e.kur)
## mean sd skewness kurtosis
## 10.557397 9.565913 1.504506 5.270559
#For Log-Normal Samples
ln.mean = mean(samples_lnorm)
ln.sd = sd(samples_lnorm)
ln.sk = mean((samples_lnorm-ln.mean)^3)/ln.sd^(3)
ln.kur = mean((samples_lnorm-ln.mean)^4)/ln.sd^(4)
c(mean=ln.mean,sd=ln.sd,skewness=ln.sk,kurtosis=ln.kur)
## mean sd skewness kurtosis
## 11.426194 13.040966 2.316483 9.516496
# Plotting Boxplots in the same graph
par(mfrow=c(1,2))
boxplot(samples_exp,main="Exponential")
boxplot(samples_lnorm,main="Log-Normal")
par(mfrow=c(1,1))
x = seq(-2,2,0.1) #defining a sequence of numbers from -2 to 2
y = 5*x^2 + 2*x -6
plot(x,y,type="l",main="Plot of the function f(x)")
abline(h=0,col="red")
# Problem 7
#Drawing 50 samples from N(10,4)
x = rnorm(50,10,2)
y = exp(-x/2)
#Fitting a linear regression line of y on x
fit=lm(y~x)
fit
##
## Call:
## lm(formula = y ~ x)
##
## Coefficients:
## (Intercept) x
## 0.057795 -0.004597
#Plot of residual vs fitted
#Method 1
res.fit = residuals(fit) #residuals
fitted.fit = predict(fit) #predicted/fitted values of y
plot(x=res.fit,y=fitted.fit,main="Residual vs Fitted Plot")
#Method 2
y.fit = fit$coefficients[1] + fit$coefficients[2]*x #Fitted values of y
res.y = y-y.fit #Residuals / error term
plot(x=res.y,y=y.fit,main="Residuals vs Fitted Plot [2]")
a = 1:16 #defining a vector which may form the elements of the matrix
M = matrix(1:16,nrow=4); #creating a 4*4 matrix
M
## [,1] [,2] [,3] [,4]
## [1,] 1 5 9 13
## [2,] 2 6 10 14
## [3,] 3 7 11 15
## [4,] 4 8 12 16
upper.a = upper.tri(M) #Upper Triangular Matrix with logical entries
M.upper = M*upper.a
lower.a = lower.tri(M) #Lower Triangular Matrix with logical entries
M.lower = M*lower.a
D = diag(M)/2 #diagonals elements of the matrix M
Diag.M = diag(D) #Forming a diagonal Matrix with diagonal elements D
M1 = M.upper+Diag.M
M2 = M.lower+Diag.M
M1;M2;M1+M2
## [,1] [,2] [,3] [,4]
## [1,] 0.5 5 9.0 13
## [2,] 0.0 3 10.0 14
## [3,] 0.0 0 5.5 15
## [4,] 0.0 0 0.0 8
## [,1] [,2] [,3] [,4]
## [1,] 0.5 0 0.0 0
## [2,] 2.0 3 0.0 0
## [3,] 3.0 7 5.5 0
## [4,] 4.0 8 12.0 8
## [,1] [,2] [,3] [,4]
## [1,] 1 5 9 13
## [2,] 2 6 10 14
## [3,] 3 7 11 15
## [4,] 4 8 12 16
x = seq(-3,3,0.1)
Phi.x = pnorm(x) #Find the cdf at points x
data.frame(x,Phi.x) #Generating the table
## x Phi.x
## 1 -3.0 0.001349898
## 2 -2.9 0.001865813
## 3 -2.8 0.002555130
## 4 -2.7 0.003466974
## 5 -2.6 0.004661188
## 6 -2.5 0.006209665
## 7 -2.4 0.008197536
## 8 -2.3 0.010724110
## 9 -2.2 0.013903448
## 10 -2.1 0.017864421
## 11 -2.0 0.022750132
## 12 -1.9 0.028716560
## 13 -1.8 0.035930319
## 14 -1.7 0.044565463
## 15 -1.6 0.054799292
## 16 -1.5 0.066807201
## 17 -1.4 0.080756659
## 18 -1.3 0.096800485
## 19 -1.2 0.115069670
## 20 -1.1 0.135666061
## 21 -1.0 0.158655254
## 22 -0.9 0.184060125
## 23 -0.8 0.211855399
## 24 -0.7 0.241963652
## 25 -0.6 0.274253118
## 26 -0.5 0.308537539
## 27 -0.4 0.344578258
## 28 -0.3 0.382088578
## 29 -0.2 0.420740291
## 30 -0.1 0.460172163
## 31 0.0 0.500000000
## 32 0.1 0.539827837
## 33 0.2 0.579259709
## 34 0.3 0.617911422
## 35 0.4 0.655421742
## 36 0.5 0.691462461
## 37 0.6 0.725746882
## 38 0.7 0.758036348
## 39 0.8 0.788144601
## 40 0.9 0.815939875
## 41 1.0 0.841344746
## 42 1.1 0.864333939
## 43 1.2 0.884930330
## 44 1.3 0.903199515
## 45 1.4 0.919243341
## 46 1.5 0.933192799
## 47 1.6 0.945200708
## 48 1.7 0.955434537
## 49 1.8 0.964069681
## 50 1.9 0.971283440
## 51 2.0 0.977249868
## 52 2.1 0.982135579
## 53 2.2 0.986096552
## 54 2.3 0.989275890
## 55 2.4 0.991802464
## 56 2.5 0.993790335
## 57 2.6 0.995338812
## 58 2.7 0.996533026
## 59 2.8 0.997444870
## 60 2.9 0.998134187
## 61 3.0 0.998650102
#lower tail cdf value for the probability 0.5 and d.f.=3 for a t-distribution
qt(0.5,df=3,lower.tail = TRUE)
## [1] 0
#Fitting a linear regression line on categorical co-variate
library(ISwR)
data = bp.obese
sex = as.character(data[1])
bp = data[3]
lm(bp~sex,data=bp.obese)
##
## Call:
## lm(formula = bp ~ sex, data = bp.obese)
##
## Coefficients:
## (Intercept) sex
## 127.955 -1.644
samples = rexp(20,1/10^3);
n.pos = sum(samples>2000) #No. of samples which exceeds a lifetime of 2000
p = n.pos/20 #proportion of samples which exceeds a lifetime of 2,000
p
## [1] 0.25
p = 0.5 #probabilty of getting a head
M=qbinom(0.90,10^3,p,lower.tail = TRUE) #Value for which no. of heads obtained is less than or equal to 0.90
M
## [1] 520
N.failures = rnbinom(1,M,p) #no. of tails before M heads
N = N.failures +M
N
## [1] 1048
#Try to get the average values yourself