Professional Documents
Culture Documents
ABA
ABA
> name<-c("ramu","raju","ravi","vamsi","naveen","kishor","chatu","rani","sita","lakshmi")
> t1<-c(15,17,20,12,14,9,15,19,25,19)
> t2<-c(NA,19,24,16,19,20,18,16,20,NA)
> gd<-c(1,1,1,1,0,1,0,1,1,0)
> dt<-data.frame(name,t1,t2,gd)
> str(dt)
$ t1 : num 15 17 20 12 14 9 15 19 25 19
$ t2 : num NA 19 24 16 19 20 18 16 20 NA
$ gd : num 1 1 1 1 0 1 0 1 1 0
> dt['gd']<-as.factor(dt$gd)
> str(dt)
$ t1 : num 15 17 20 12 14 9 15 19 25 19
$ t2 : num NA 19 24 16 19 20 18 16 20 NA
> dt$t2[is.na(dt$t2)]<-mean(dt$t2,na.rm = T)
> str(dt)
$ t1 : num 15 17 20 12 14 9 15 19 25 19
$ t2 : num 19 19 24 16 19 20 18 16 20 19
> boxplot(dt$t1,dt$t2)
pg. 1
> out<-boxplot.stats(dt$t2)$out
> out
[1] 24
> dt$t2[dt$t2%in%out]<-median(dt$t2)
> boxplot(dt$t1,dt$t2)
pg. 2
EXP 2
> #exp 2
> x=c(60,60,60)
> p=c(1/3,1/3,1/3)
> chisq.test(x=x,p=p)
data: x
X-squared = 0, df = 2, p-value = 1
> x=c(70,50,30)
> p=x/sum(x)
>p
> chisq.test(x=x,p=p)
data: x
X-squared = 0, df = 2, p-value = 1
> p1=c(1/2,1/6,1/5)
> p1=c(1/2,1/6,1/3)
> chisq.test(x=x,p=p1)
data: x
pg. 3
EXP 3
> #exp3
> dt<-mtcars
> cor(dt$mpg,dt$wt)
[1] -0.8676594
> cor(dt$hp,dt$cyl)
[1] 0.8324475
> dt1<-mtcars[,c(1,3:7)]
> dt11<-cor(dt1)
> library(corrplot)
Warning message:
pg. 4
EXP 4
> #EXP 4
> dt
Call:
Coefficients:
(Intercept) hp
30.09886 -0.06823
> summary(dt)
Call:
Residuals:
Coefficients:
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
pg. 5
Multiple R-squared: 0.6024, Adjusted R-squared: 0.5892
> dt
Call:
Coefficients:
wt qsec vs am gear
carb
-0.19942
> summary(dt)
Call:
Residuals:
Coefficients:
pg. 6
disp 0.01334 0.01786 0.747 0.4635
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
pg. 7
EXP 5
> #exp 5
> str(mtcars)
$ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
$ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
$ vs : num 0 0 1 1 0 1 0 1 1 1 ...
$ am : num 1 1 1 0 0 0 0 0 0 0 ...
> dt<-as.factor(mtcars$am)
> str(dt)
> am<-as.factor(mtcars$am)
> str(am)
> mt<-mtcars[,c(3:7)]
> dt<-sample(c(1,2),32,32)
> str(dt)
> dt<-as.factor(dt)
> md
pg. 8
Call: glm(formula = dt ~ mtcars$mpg, family = "binomial")
Coefficients:
(Intercept) mtcars$mpg
-0.202813 -0.002415
> summary(md)
Call:
Deviance Residuals:
Coefficients:
AIC: 47.858
pg. 9
> library(nnet, lib.loc = "C:/Program Files/R/R-4.1.1/library")
> dt<-sample(1:4,32,32)
> md<-multinom(dt~mtcars$mpg)
# weights: 12 (6 variable)
converged
> md
Call:
multinom(formula = dt ~ mtcars$mpg)
Coefficients:
(Intercept) mtcars$mpg
2 1.5037549 -0.1088401
3 0.6804832 -0.0142661
4 -3.4490522 0.1509189
AIC: 91.23868
> summary(md)
Call:
multinom(formula = dt ~ mtcars$mpg)
Coefficients:
(Intercept) mtcars$mpg
2 1.5037549 -0.1088401
3 0.6804832 -0.0142661
4 -3.4490522 0.1509189
pg. 10
Std. Errors:
(Intercept) mtcars$mpg
2 2.309482 0.12598808
3 1.744554 0.08717987
4 2.173418 0.09592167
AIC: 91.23868
> str(dt)
> str(dt)
> md<-polr(dt~mtcars$mpg)
> md<-polr(dt~mtcars$mpg,Hess = T)
> summary(md)
Call:
Coefficients:
Intercepts:
AIC: 89.79075
pg. 11
EXP 6
> #EXP 6
> dt<-trees
> library(psych)
> md<-mediate(x='Height',m='Volume',y='Girth',data=dt)
> print(md,short = F)
Mediation/Moderation Analysis
data = dt)
The DV (Y) was Girth . The IV (X) was Height . The mediating variable(s) = Volume .
Total effect(c) of Height on Girth = 0.26 S.E. = 0.08 t = 3.27 df= 29 with p = 0.0028
Direct effect (c') of Height on Girth removing Volume = -0.05 S.E. = 0.03 t = -1.61 df= 28
with p = 0.12
Mean bootstrapped indirect effect = 0.3 with standard error = 0.08 Lower CI = 0.16 Upper CI =
0.46
Full output
data = dt)
Girth se t df Prob
pg. 12
R = 0.97 R2 = 0.94 F = 222.47 on 2 and 28 DF p-value: 6.5e-18
Girth se t df Prob
Volume se t df Prob
Girth se t df Prob
pg. 13
EXP-7
> #exp 7
> dt<-trees
> md
Call:
Coefficients:
> summary(md)
Call:
Residuals:
Coefficients:
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
pg. 14
Residual standard error: 4.482 on 27 degrees of freedom
pg. 15
EXP 8
> #exp9
> dt<-attitude
> str(dt)
> md<-prcomp(rating)
> md<-prcomp(dt)
> md
[7] 4.668443
Rotation (n x k) = (7 x 7):
PC6 PC7
pg. 16
rating -0.47248738 0.5341317
> install.packages("factoextra")
WARNING: Rtools is required to build R packages but is not currently installed. Please download and
install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
also installing the dependencies ‘lazyeval’, ‘carData’, ‘crosstalk’, ‘viridis’, ‘car’, ‘DT’, ‘ellipse’,
‘flashClust’, ‘leaps’, ‘scatterplot3d’, ‘dendextend’, ‘FactoMineR’
downloaded 169 KB
downloaded 1.7 MB
downloaded 400 KB
downloaded 2.9 MB
pg. 17
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.1/car_3.0-12.zip'
downloaded 1.3 MB
downloaded 1.7 MB
downloaded 70 KB
downloaded 36 KB
downloaded 100 KB
downloaded 330 KB
downloaded 3.7 MB
pg. 18
downloaded 3.6 MB
downloaded 407 KB
C:\Users\reddy\AppData\Local\Temp\RtmpsXJTpT\downloaded_packages
> library(factoextra)
%+%, alpha
pg. 19
Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
Warning message:
> fviz_eig(md)
pg. 20
EXP 10
> #exp 10
> library(NbClust)
> dt<-USArrests
> str(dt)
$ Murder : num 13.2 10 8.1 8.8 9 7.9 3.3 5.9 15.4 17.4 ...
$ Assault : int 236 263 294 190 276 204 110 238 335 211 ...
$ Rape : num 21.2 44.5 31 19.5 40.6 38.7 11.1 15.8 31.9 25.8 ...
> dt$Assault<-as.numeric(dt$Assault)
> dt$UrbanPop<-as.numeric(dt$UrbanPop)
> str(dt)
$ Murder : num 13.2 10 8.1 8.8 9 7.9 3.3 5.9 15.4 17.4 ...
$ Assault : num 236 263 294 190 276 204 110 238 335 211 ...
$ Rape : num 21.2 44.5 31 19.5 40.6 38.7 11.1 15.8 31.9 25.8 ...
*** : The Hubert index is a graphical method of determining the number of clusters.
significant increase of the value of the measure i.e the significant peak in Hubert
pg. 21
In the plot of D index, we seek a significant knee (the significant peak in Dindex
the measure.
*******************************************************************
*******************************************************************
$All.index
pg. 22
8 0.3533 136.8055 9.0419 8.8535 404.9117 1.012627e+15 24055494 14949.328 308.3085
137.4044 0.4099 1.0192 0.3837 0.3122 13.2186 4.5589
Ratkowsky Ball Ptbiserial Frey McClain Dunn Hubert SDindex Dindex SDbw
$All.CriticalValues
pg. 23
2 0.4943 32.7341 0.0007
$Best.nc
Ratkowsky Ball PtBiserial Frey McClain Dunn Hubert SDindex Dindex SDbw
$Best.partition
1 1 1 2 1 2 3 1 1 2
3 3 1 3 3 3 3 1 3 1
pg. 24
Massachusetts Michigan Minnesota Mississippi Missouri Montana Nebraska
Nevada New Hampshire New Jersey
2 1 3 1 2 3 3 1 3 2
New Mexico New York North Carolina North Dakota Ohio Oklahoma Oregon
Pennsylvania Rhode Island South Carolina
1 1 1 3 3 2 2 3 2 1
3 2 2 3 3 2 2 3 3 2
Warning message:
> md<-kmeans(dt,3)
> clusplot(dt,md$cluster,shade = T)
Warning messages:
pg. 25
4: In doTryCatch(return(expr), name, parentenv, handler) :
pg. 26
EXP 11
> #exp 11
> dt<-USArrests
> head(dt)
> md
Call:
0.8 2.1 2.2 2.6 2.7 3.2 3.3 3.4 3.8 4 4.3
0.02 0.04 0.04 0.04 0.02 0.02 0.02 0.02 0.02 0.02 0.02
4.4 4.9 5.3 5.7 5.9 6 6.3 6.6 6.8 7.2 7.3
0.02 0.02 0.02 0.02 0.02 0.04 0.02 0.02 0.02 0.02 0.02
0.02 0.02 0.02 0.02 0.02 0.04 0.02 0.02 0.02 0.02 0.02
0.02 0.02 0.02 0.02 0.02 0.04 0.02 0.04 0.02 0.02
Group means:
pg. 27
2.1 70.0 53.5 8.65
pg. 28
11.3 300.0 67.0 27.80
Proportion of trace:
> summary(md)
N 1 -none- numeric
pg. 29
> partimat(Murder~.,data =dt)
pg. 30