0% found this document useful (0 votes)
9 views13 pages

DAR Module2

The document outlines various practical exercises in R, including creating cross-tabulations, performing t-tests, ANOVA, and generating graphical reports. It provides code snippets and outputs for tasks such as creating tables, conducting statistical tests, and generating plots like scatterplots and boxplots. Each practical exercise has a defined aim and includes the corresponding R code and output results.

Uploaded by

zygotemarinator
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views13 pages

DAR Module2

The document outlines various practical exercises in R, including creating cross-tabulations, performing t-tests, ANOVA, and generating graphical reports. It provides code snippets and outputs for tasks such as creating tables, conducting statistical tests, and generating plots like scatterplots and boxplots. Each practical exercise has a defined aim and includes the corresponding R code and output results.

Uploaded by

zygotemarinator
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Module2:-

[Link]. 3A:-

Aim:-Creating cross-Tobulation and two tables using table() in R.

data<-[Link](

Name =c("A","B","C","D"),

Age= c(25,35,30,40),

Score =c(90,85,86,88),

Gender =c("M","F","M","F")

data$AgeGroup<-cut(

data$Age,

breaks =c(0,30,40,100),

labels =c("Young","Middle","Older"),

right =TRUE

print(table(data$Gender))

print(table(data$AgeGroup))

print(table(data$Gender,data$AgeGroup))

output:-

> data<-[Link](
+ Name =c("A","B","C","D"),
+ Age= c(25,35,30,40),
+ Score =c(90,85,86,88),
+ Gender =c("M","F","M","F")
+ )
>
> data$AgeGroup<-cut(
+ data$Age,
+ breaks =c(0,30,40,100),
+ labels =c("Young","Middle","Older"),
+ right =TRUE
+ )
> print(table(data$Gender))
F M
2 2
> print(table(data$AgeGroup))
Young Middle Older
2 2 0
> print(table(data$Gender,data$AgeGroup))

Young Middle Older


F 0 2 0
M 2 0 0

>
[Link].3 B:-

gender<-c("Male","Female","Female","Male","Female")

education<-c("Yes","No","No","Yes","Yes")

tab<-table(gender,education)

tab

Output:-

> gender<-c("Male","Female","Female","Male","Female")
> education<-c("Yes","No","No","Yes","Yes")
>
> tab<-table(gender,education)
> tab
education
gender No Yes
Female 2 1
Male 0 2

>

[Link].4:-

Aim:-Performing One sample T Test using [Link]() in R.

data<-[Link](

Score1=c(85,92,78,88)

[Link](data$Score1,mu=50)

scores<-c(45,52,47,50,49,53,46)

[Link](scores,mu=50)
[Link](scores,mu=50,alternative="greater")

[Link](scores,mu=50,alternative="less")

Output:-

> data<-[Link](
+ Score1=c(85,92,78,88)
+ )
> [Link](data$Score1,mu=50)

One Sample t-test

data: data$Score1
t = 12.1, df = 3, p-value = 0.001215
alternative hypothesis: true mean is not equal to 50
95 percent confidence interval:
76.34741 95.15259
sample estimates:
mean of x
85.75

> scores<-c(45,52,47,50,49,53,46)
> [Link](scores,mu=50)
One Sample t-test

data: scores
t = -1, df = 6, p-value = 0.3559
alternative hypothesis: true mean is not equal to 50
95 percent confidence interval:
46.06067 51.65361
sample estimates:
mean of x
48.85714

> [Link](scores,mu=50,alternative="greater")

One Sample t-test


data: scores
t = -1, df = 6, p-value = 0.822
alternative hypothesis: true mean is greater than 50
95 percent confidence interval:
46.63637 Inf
sample estimates:
mean of x
48.85714
> [Link](scores,mu=50,alternative="less")

One Sample t-test

data: scores
t = -1, df = 6, p-value = 0.178
alternative hypothesis: true mean is less than 50
95 percent confidence interval:
-Inf 51.07792
sample estimates:
mean of x
48.85714

>
[Link].5:-

Aim:-Performing independent two Sample T –test using [Link]() in R.

data<-[Link](

Score1 =c(85,90,78,88,92,84,79,81,91,87),

Gender=c("Male","Male","Female","Female","Male","Female","Male","Female","Male","Female")

[Link](Score1~Gender,data=data)

df<-[Link](

score=c(88,75,90,95,82,70,65,78),

group=c("A","A","A","A","B","B","B","B")

[Link](score~group,data=df)

[Link](score~group,data=df,alternative="greater")

[Link](score~group,data=df,alternative="less")

Output:-

> data<-[Link](
+ Score1 =c(85,90,78,88,92,84,79,81,91,87),
+ Gender=c("Male","Male","Female","Female","Male","Female","Male","Femal
e","Male","Female")
+ )
> [Link](Score1~Gender,data=data)

Welch Two Sample t-test


data: Score1 by Gender
t = -1.2447, df = 7.5025, p-value = 0.2507
alternative hypothesis: true difference in means between group Female and
group Male is not equal to 0
95 percent confidence interval:
-10.922017 3.322017
sample estimates:
mean in group Female mean in group Male
83.6 87.4

> df<-[Link](
+ score=c(88,75,90,95,82,70,65,78),
+ group=c("A","A","A","A","B","B","B","B")
+ )
> [Link](score~group,data=df)

Welch Two Sample t-test


data: score by group
t = 2.3102, df = 5.9352, p-value = 0.06072
alternative hypothesis: true difference in means between group A and group
B is not equal to 0
95 percent confidence interval:
-0.8214672 27.3214672
sample estimates:
mean in group A mean in group B
87.00 73.75

> [Link](score~group,data=df,alternative="greater")
Welch Two Sample t-test

data: score by group


t = 2.3102, df = 5.9352, p-value = 0.03036
alternative hypothesis: true difference in means between group A and group
B is greater than 0
95 percent confidence interval:
2.083078 Inf
sample estimates:
mean in group A mean in group B
87.00 73.75
> [Link](score~group,data=df,alternative="less")

Welch Two Sample t-test

data: score by group


t = 2.3102, df = 5.9352, p-value = 0.9696
alternative hypothesis: true difference in means between group A and group
B is less than 0
95 percent confidence interval:
-Inf 24.41692
sample estimates:
mean in group A mean in group B
87.00 73.75
[Link].6:-

Aim:-Performing Paired Sample T-Test using [Link]() in R.

data<-[Link](

Name=c("John","Jane","Alice","Bob"),

Score1=c(85,92,78,88),

Score2=c(80,89,90,75)

[Link](data$Score1,data$Score2,paired=TRUE)

Output:-

> data<-[Link](
+ Name=c("John","Jane","Alice","Bob"),
+ Score1=c(85,92,78,88),
+ Score2=c(80,89,90,75)
+ )
> [Link](data$Score1,data$Score2,paired=TRUE)

Paired t-test
data: data$Score1 and data$Score2
t = 0.43119, df = 3, p-value = 0.6954
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
-14.35651 18.85651
sample estimates:
mean difference
2.25
[Link].7:-

Aim:-Performing One-Way ANOVA using aov() in R.

data<-[Link](

Score1 =c(85,90,78,88,92,84,79,81,91,87),

Gender=c("Male","Male","Female","Female","Male","Female","Male","Female","Male","Female")

anoval<-aov(Score1~Gender,data=data)

print(anoval)

Output:-

> data<-[Link](
+ Score1 =c(85,90,78,88,92,84,79,81,91,87),
+ Gender=c("Male","Male","Female","Female","Male","Female","Male","Female","Male","Fe
+ )
> anoval<-aov(Score1~Gender,data=data)
> print(anoval)
Call:
aov(formula = Score1 ~ Gender, data = data)

Terms:
Gender Residuals
Sum of Squares 36.1 186.4
Deg. of Freedom 1 8

Residual standard error: 4.827007


Estimated effects may be unbalanced

>
[Link].8:-

Aim:- Performing Two-Way ANOVA using aov() in R.

data<-[Link](

Score1 =c(85,90,78,88,92,84,79,81,91,87),

Gender=c("Male","Male","Female","Female","Male","Female","Male","Female","Male","Female"),

AgeGroup=c(30,35,46,50,18,20,25,30,28,40)

)
anova2<-aov(Score1~Gender*AgeGroup,data=data)

summary(anova2)

Output:-

> data<-[Link](
+ Score1 =c(85,90,78,88,92,84,79,81,91,87),
+ Gender=c("Male","Male","Female","Female","Male","Female","Male","Female","Male","Fe
+ AgeGroup=c(30,35,46,50,18,20,25,30,28,40)
+ )
> anova2<-aov(Score1~Gender*AgeGroup,data=data)
> summary(anova2)
Df Sum Sq Mean Sq F value Pr(>F)
Gender 1 36.10 36.10 1.173 0.320
AgeGroup 1 0.58 0.58 0.019 0.895
Gender:AgeGroup 1 1.11 1.11 0.036 0.855
Residuals 6 184.70 30.78

[Link].9:-

Aim:-Conducting Chi-Square Test using [Link]() in R.

data<-[Link](

AgeGroup=c("25-30","35-40","30-35","40-45"),

Gender=c("M","F","M","F")

[Link](table(data$Gender,data$AgeGroup))

Output:-

> data<-[Link](
+ AgeGroup=c("25-30","35-40","30-35","40-45"),
+ Gender=c("M","F","M","F")
+ )
> [Link](table(data$Gender,data$AgeGroup))

Pearson's Chi-squared test

data: table(data$Gender, data$AgeGroup)


X-squared = 4, df = 3, p-value = 0.2615

[Link].10:-

Aim:-Creating graphical reports() in R.

ScatterPlots

input<-mtcars[,c('wt','mpg')]

print(head(input))

png(file ="[Link]")
plot(x =input$wt,y=input$mpg,

xlab="Weight",

ylab="Milage",

xlim=c(2.5,5),

ylim=c(15,30),

main="Weight vs Milage"

[Link]()

Output:-

> input<-mtcars[,c('wt','mpg')]
> print(head(input))
wt mpg
Mazda RX4 2.620 21.0
Mazda RX4 Wag 2.875 21.0
Datsun 710 2.320 22.8
Hornet 4 Drive 3.215 21.4
Hornet Sportabout 3.440 18.7
Valiant 3.460 18.1
> png(file ="[Link]")
> plot(x =input$wt,y=input$mpg,
+ xlab="Weight",
+ ylab="Milage",
+ xlim=c(2.5,5),
+ ylim=c(15,30),
+ main="Weight vs Milage"
+ )
> [Link]()
null device
1

>
[Link].11:-

Aim:-Generating Histogram and Boxplot in R.

input<-mtcars[,c('mpg','cyl')]

print(head(input))

png(file ="[Link]")

boxplot(mpg~cyl,data=mtcars,xlab="Numbers of Cylinders",

ylab="Miles Per Gallon",main="Milage Data")

[Link]()

Output:-

> input<-mtcars[,c('mpg','cyl')]
> print(head(input))
mpg cyl
Mazda RX4 21.0 6
Mazda RX4 Wag 21.0 6
Datsun 710 22.8 4
Hornet 4 Drive 21.4 6
Hornet Sportabout 18.7 8
Valiant 18.1 6
>
> png(file ="[Link]")
>
> boxplot(mpg~cyl,data=mtcars,xlab="Numbers of Cylinders",
+ ylab="Miles Per Gallon",main="Milage Data")
> [Link]()
null device
1
> input<-mtcars[,c('mpg','cyl')]
> print(head(input))
mpg cyl
Mazda RX4 21.0 6
Mazda RX4 Wag 21.0 6
Datsun 710 22.8 4
Hornet 4 Drive 21.4 6
Hornet Sportabout 18.7 8
Valiant 18.1 6
> png(file ="[Link]")
> boxplot(mpg~cyl,data=mtcars,xlab="Numbers of Cylinders",
+ ylab="Miles Per Gallon",main="Milage Data")
> [Link]()
null device
1

>

v<-c(9,13,21,8,36,22,12,14,41,31,33,19)

png(file ="histogram_lim_breaks.png")

hist(v,xlab ="Weight",col="green",border="red",xlim=c(0,40),ylim=c(0,5),breaks=5)

[Link]()

Output:-

> v<-c(9,13,21,8,36,22,12,14,41,31,33,19)
>
> png(file ="histogram_lim_breaks.png")
>
> hist(v,xlab ="Weight",col="green",border="red",xlim=c(0,40),ylim=c(0,5),breaks=5)
>
> [Link]()
png
2

>

[Link].12:-

Aim:-Generating Correlation Matrix using cor() in R.

df<-mtcars

numeric_data<-df%>%select(where([Link]))

cor_matrix<-cor(numeric_data)

print(cor_matrix)

Output:-

> df<-mtcars
> numeric_data<-df%>%select(where([Link]))
> cor_matrix<-cor(numeric_data)
> print(cor_matrix)
mpg cyl disp hp drat wt qsec
mpg 1.0000000 -0.8521620 -0.8475514 -0.7761684 0.68117191 -0.8676594 0.41868403 0.
cyl -0.8521620 1.0000000 0.9020329 0.8324475 -0.69993811 0.7824958 -0.59124207 -0.
disp -0.8475514 0.9020329 1.0000000 0.7909486 -0.71021393 0.8879799 -0.43369788 -0.
hp -0.7761684 0.8324475 0.7909486 1.0000000 -0.44875912 0.6587479 -0.70822339 -0.
drat 0.6811719 -0.6999381 -0.7102139 -0.4487591 1.00000000 -0.7124406 0.09120476 0.
wt -0.8676594 0.7824958 0.8879799 0.6587479 -0.71244065 1.0000000 -0.17471588 -0.
qsec 0.4186840 -0.5912421 -0.4336979 -0.7082234 0.09120476 -0.1747159 1.00000000 0.
vs 0.6640389 -0.8108118 -0.7104159 -0.7230967 0.44027846 -0.5549157 0.74453544 1.
am 0.5998324 -0.5226070 -0.5912270 -0.2432043 0.71271113 -0.6924953 -0.22986086 0.
gear 0.4802848 -0.4926866 -0.5555692 -0.1257043 0.69961013 -0.5832870 -0.21268223 0.
carb -0.5509251 0.5269883 0.3949769 0.7498125 -0.09078980 0.4276059 -0.65624923 -0.
gear carb
mpg 0.4802848 -0.55092507
cyl -0.4926866 0.52698829
disp -0.5555692 0.39497686
hp -0.1257043 0.74981247
drat 0.6996101 -0.09078980
wt -0.5832870 0.42760594
qsec -0.2126822 -0.65624923
vs 0.2060233 -0.56960714
am 0.7940588 0.05753435
gear 1.0000000 0.27407284
carb 0.2740728 1.00000000

>
[Link].13:-

Aim:-Performing Linear Regression analysis lm() in R.

lm_model<-lm(mpg~wt+hp,data=df)

summary(lm_model)

Output:-

> lm_model<-lm(mpg~wt+hp,data=df)
> summary(lm_model)

Call:
lm(formula = mpg ~ wt + hp, data = df)

Residuals:
Min 1Q Median 3Q Max
-3.941 -1.600 -0.182 1.050 5.854

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.22727 1.59879 23.285 < 2e-16 ***
wt -3.87783 0.63273 -6.129 1.12e-06 ***
hp -0.03177 0.00903 -3.519 0.00145 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 2.593 on 29 degrees of freedom


Multiple R-squared: 0.8268, Adjusted R-squared: 0.8148
F-statistic: 69.21 on 2 and 29 DF, p-value: 9.109e-12

[Link].14:-

Aim:-Performing Logistic Regression using glm() in R.

df$high_mpg<-ifelse(df$mpg>20,1,0)

log_model<-glm(high_mpg~wt+hp,

data=df,

family=binomial)

summary(log_model)
Output:-

> df$high_mpg<-ifelse(df$mpg>20,1,0)
> log_model<-glm(high_mpg~wt+hp,
+ data=df,
+ family=binomial)
Warning messages:
1: [Link]: algorithm did not converge
2: [Link]: fitted probabilities numerically 0 or 1 occurred
> summary(log_model)

Call:
glm(formula = high_mpg ~ wt + hp, family = binomial, data = df)

Deviance Residuals:
Min 1Q Median 3Q Max
-6.743e-05 -2.100e-08 -2.100e-08 2.100e-08 7.480e-05
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 894.228 365884.162 0.002 0.998
wt -202.865 84688.218 -0.002 0.998
hp -2.021 858.062 -0.002 0.998
(Dispersion parameter for binomial family taken to be 1)

Null deviance: 4.3860e+01 on 31 degrees of freedom


Residual deviance: 1.1156e-08 on 29 degrees of freedom
AIC: 6
Number of Fisher Scoring iterations: 25

You might also like