Professional Documents
Culture Documents
Raghav Khemka 3B RMC
Raghav Khemka 3B RMC
INDEX
P a g e 1 | 72
RMC FILE
ANN2
Twowayann
Wtloss
P a g e 2 | 72
RMC FILE
ASSIGNMENT 1
BASIC COMMANDS FOR R STUDIO
Variables: A variable is a name for a value, such as x, current temperature, or subject.id. We can
create a new variable by assigning a value to it using assignment operator.
Assignment Operator: The use of these operators is to assign values to the variables. These are the
two kinds of assignments, leftwards and rightwards assignment.
Operators <- and = are used to assign variables.
ASSIGN VALUES TO VARIABLES, PASTE AND READLINE FUNCTIONS
> x <- 20
> x
[1] 20
>
> y
[1] 30
> z <- x + y
> z
[1] 50
> a <- "Hello"
> a
[1] "Hello"
> b <- "Kunal"
> b
[1] "Kunal"
> paste (a, b)
[1] "Hello Kunal"
> readline("what is your name?")
what is your name? Lakshay
[1] "Lakshay"
> urname <- readline("what is your name?")
what is your name? Kunal
P a g e 3 | 72
RMC FILE
> urname
[1] "Kunal"
> paste(a, urname)
[1] "Hello Kunal"
> m <- x+y+z
> m
[1] 100
> m <- x+y+z #Adding 3 numeric variables
> m <- x+y+z # Adding 3 numeric variables
> m # display value of m
[1] 100
> # "Good Night"
> "Good Morning!" # "Good night"
[1] "Good Morning!"
> c
P a g e 4 | 72
RMC FILE
ASSIGNMENT 2
VARIABLES AND DATA TYPES
age <- 5
age
# [1] 5
Age <- 10
Age
# [1] 10
x <-10
y <- 12.5
z <- 13L
class(z)
# [1] "integer"
class(x)
# [1] "numeric"
class(y)
# [1] "numeric"
# [1] "character"
# [1] "character"
# [1] 30
class(name3)
# [1] "numeric"
class(name2)
# [1] "character"
P a g e 5 | 72
RMC FILE
l <- "true"
l
# [1] "true"
class(l)
# [1] "character"
v <- TRUE
v
# [1] TRUE
class(v)
# [1] "logical"
str <- "R is lower case case sensative language. It is very good language.
It include"
str
# [1] "R is lower case case sensative language. It is very good language.
It include"
cat(str)
cat(a)
nchar(a)
# [1] 37
grepl("Hello",a)
# [1] TRUE
q <- "Hello"
w <-"How are you!!!!"
paste(q,w)
P a g e 6 | 72
RMC FILE
# [1] 15
min(5,10,15)
# [1] 5
sqrt(16)
# [1] 4
abs(-4.7)
# [1] 4.7
ceiling(1.4)
# [1] 2
floor(1.4)
# [1] 1
P a g e 7 | 72
RMC FILE
ASSIGNMENT 3
DIFFERENT TYPES OF OPERATORS
# Arithmatic Operators
> x <- 5
> y <- 2
> x
[1] 5
> y
[1] 2
> x <- 5
> y <- 2
> x
[1] 5
> y
[1] 2
> x+y
[1] 7
> x-y
[1] 3
> x*y
[1] 10
> x/y
[1] 2.5
> y^x
[1] 32
> x%%y
[1] 1
# Comparison operators
x
## [1] 5
P a g e 8 | 72
RMC FILE
## [1] 2
x == y
## [1] FALSE
x != y
## [1] TRUE
x > y
## [1] TRUE
x < y
## [1] FALSE
x >= y
## [1] TRUE
# Logical Operators
a <- 200
b <- 33
c <- 500
a
## [1] 200
## [1] 33
## [1] 500
## [1] TRUE
## [1] FALSE
# OR operator (|)
z <- a>b | a > c
z
## [1] TRUE
P a g e 9 | 72
RMC FILE
## [1] FALSE
## [1] TRUE
x <- ! a < c
x
## [1] FALSE
P a g e 10 | 72
RMC FILE
ASSIGNMENT 4
IF CONSTRUCT AND NESTED IF
IF CONSTRUCT:
a <- 33
b <- 200
if (b>a){ print("b is greater than a")}
a <- 33
b <- 33
if (b>a)
{print("b is greater than a")} else if (a==b)
{print("a and b are equal")}
a <- 245
b <- 33
if (b > a)
{print("b is greater than a")} else if (a==b)
{print("a and b are equal")} else
{print("a is greater than b")}
a <- 200
b <- 33
if (b > a)
{print("b is greater than a")} else
{print("b is not greater than a")}
NESTED IF:
myage <- readline("Enter your age")
if(myage <18)
{print("You are not a major, u are not eligible to work")} else
{
if(myage >=18 & myage <=60) {
print("u are eligible to work, please fill application form and email
us")} else
P a g e 11 | 72
RMC FILE
{
print("As per govt rules, you are too old to work, please collect
your pension")
}}
P a g e 12 | 72
RMC FILE
ASSIGNMENT 5
LOOP
While Loop:
i <- 1
while (i < 6) {
print(i)
i <- i + 1
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
i <- 1
while (i < 6) {
print(i)
i <- i + 1
if (i == 4) {
break
}
}
## [1] 1
## [1] 2
## [1] 3
i <- 0
while (i < 6) {
i <- i + 1
if (i == 3) {
next
}
print(i)
}
## [1] 1
## [1] 2
## [1] 4
## [1] 5
## [1] 6
dice <- 1
while (dice <= 6) {
if (dice < 6) {
print("No Yahtzee")
} else {
print("Yahtzee!")
}
P a g e 13 | 72
RMC FILE
For Loop:
for (x in 1:10) {
print(x)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
for(x in dice) {
if (x == 6) {
print(paste("The dice number is", x, "Yahtzee!"))
} else {
print(paste("The dice number is", x, "Not Yahtzee"))
}
}
P a g e 14 | 72
RMC FILE
ASSIGNMENT 6
USER DEFINED AND BUILT IN
BUILT IN FUNCTION:
> print(seq(32,44))
#[1] 32 33 34 35 36 37 38 39 40 41 42 43 44
> print(mean(25:82))
#[1] 53.5
> print(sum(41:68))
#[1] 1526
CALLING A FUNCTION:
new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
new.function(6)
##new.function(6)
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
P a g e 15 | 72
RMC FILE
}
new.function()
## new.function()
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
# [1] 26
# [1] 58
# [1] 18
# [1] 45
print(a^2)
print(a)
print(b)
P a g e 16 | 72
RMC FILE
new.function(6)
# [1] 36
# [1] 6
# Error in print(b) : argument "b" is missing, with no default
P a g e 17 | 72
RMC FILE
ASSIGNMENT 7
LISTS
LISTS OF STRING:
thislist <- list("apple", "banana", "cherry")
thislist
## [[1]]
## [1] "apple"
##
## [[2]]
## [1] "banana"
##
## [[3]]
## [1] "cherry"
thislist[1]
## [[1]]
## [1] "apple"
## [[1]]
## [1] "blackcurrant"
##
## [[2]]
## [1] "banana"
##
## [[3]]
## [1] "cherry"
LENGHTH OF LISTS:
thislist <- list("apple", "banana", "cherry")
length(thislist)
## [1] 3
## [[1]]
## [1] "apple"
P a g e 18 | 72
RMC FILE
##
## [[2]]
## [1] "banana"
##
## [[3]]
## [1] "cherry"
##
## [[4]]
## [1] "orange"
## [[1]]
## [1] "apple"
##
## [[2]]
## [1] "banana"
##
## [[3]]
## [1] "orange"
##
## [[4]]
## [1] "cherry"
newlist
## [[1]]
## [1] "banana"
##
## [[2]]
## [1] "cherry"
## [[1]]
## [1] "banana"
##
## [[2]]
## [1] "cherry"
##
## [[3]]
## [1] "orange"
P a g e 19 | 72
RMC FILE
##
## [[4]]
## [1] "kiwi"
## [1] "apple"
## [1] "banana"
## [1] "cherry"
COMBINING LISTS:
list1 <- list("a", "b", "c")
list2 <- list(1,2,3)
list3 <- c(list1,list2)
list3
## [[1]]
## [1] "a"
##
## [[2]]
## [1] "b"
##
## [[3]]
## [1] "c"
##
## [[4]]
## [1] 1
##
## [[5]]
## [1] 2
##
## [[6]]
## [1] 3
P a g e 20 | 72
RMC FILE
ASSIGNMENT 8
VECTORS
VECTOR OF STRINGS:
fruits <- c("banana", "apple", "orange")
fruits
## [1] "banana" "apple” “orange"
numbers
## [1] 1 2 3
## [1] 1 2 3 4 5 6 7 8 9 10
TO FIND NO OF VECTORS:
fruits <- c("banana", "apple", "orange")
length(fruits)
## [1] 3
SORT OF VECTORS:
fruits <- c("banana", "apple", "orange", "mango", "lemon")
numbers <- c(13, 3, 5, 7, 20, 2)
sort(fruits)
P a g e 21 | 72
RMC FILE
sort(numbers)
## [1] 2 3 5 7 13 20
ACESSING VECTORS:
fruits[1]
## [1] "banana"
fruits[c(1, 3)]
fruits
P a g e 22 | 72
RMC FILE
ASSIGNMENT 9
MATRICES
CREATING A MATRIX:
thismatrix <- matrix(c(1,2,3,4,5,6), nrow = 3, ncol = 2)
thismatrix
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
## [,1] [,2]
## [1,] "apple" "cherry"
## [2,] "banana" "orange"
## [1] "cherry"
P a g e 23 | 72
RMC FILE
## [,1] [,2]
## [1,] "apple" "orange"
## [2,] "banana" "grape"
## [3,] "cherry" "pineapple"
R-BIND:
## [1] 2 2
P a g e 24 | 72
RMC FILE
## [1] 4
COMBINING MATRICES:
Matrix1 <- matrix(c("apple", "banana", "cherry", "grape"), nrow = 2, ncol
= 2)
Matrix2 <- matrix(c("orange", "mango", "pineapple", "watermelon"), nrow =
2, ncol = 2)
Matrix_Combined <- rbind(Matrix1, Matrix2)
Matrix_Combined
## [,1] [,2]
## [1,] "apple" "cherry"
## [2,] "banana" "grape"
## [3,] "orange" "pineapple"
## [4,] "mango" "watermelon"
P a g e 25 | 72
RMC FILE
P a g e 26 | 72
RMC FILE
ASSIGNMENT 10
ARRAYS
ARRAY WITH ONE DIMENSION:
thisarray <- c(1:24)
thisarray
## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
23 24
## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
## [1] 22
## [1] 1 5 9
## [1] 1 2 3 4
P a g e 27 | 72
RMC FILE
## [1] TRUE
## [1] 4 3 2
DIMENSION OF ARRAY:
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))
length(multiarray)
## [1] 24
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
## [1] 11
## [1] 12
## [1] 13
## [1] 14
## [1] 15
## [1] 16
## [1] 17
## [1] 18
## [1] 19
## [1] 20
## [1] 21
## [1] 22
P a g e 28 | 72
RMC FILE
## [1] 23
## [1] 24
P a g e 29 | 72
RMC FILE
ASSIGNMENT 11
DATA FRAMES
CREATING DATA FRAME:
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Data_Frame
Data_Frame
summary(Data_Frame)
Data_Frame[["Training"]]
Data_Frame$Training
## Training
## 1 Strength
P a g e 30 | 72
RMC FILE
## 2 Stamina
## 3 Other
ADDING ROWS :
P a g e 31 | 72
RMC FILE
## [1] 3 3
## [1] 3
nrow(Data_Frame)
## [1] 3
## [1] 3
R-BIND:
P a g e 32 | 72
RMC FILE
## 5 Stamina 150 30
## 6 Strength 160 20
C-BIND:
P a g e 33 | 72
RMC FILE
ASSIGNMENT 12
IMPORTING EXCEL FILE
STEP 1: First go to packages on the lower right column and install xlsx
package.
STEP 2: Create a excel file and import it via import data set.
P a g e 34 | 72
RMC FILE
ASSIGNMENT 13
DATA EXPLORATION
install.packages("xlsx")
any(grepl("xlsx",installed.packages()))
library(readxl)
INPUT <- read_excel("INPUT.XLSX")
print(INPUT)
class(INPUT)
head(INPUT)
tail(INPUT)
head(INPUT,3)
tail(INPUT,3)
names(INPUT)
dim(INPUT)
ncol(INPUT)
nrow(INPUT)
#class(INPUT)
[1] "tbl_df" "tbl" "data.frame"
# head(INPUT)
# A tibble: 6 × 4
ID NAME SALARY DEPT
<dbl> <chr> <dbl> <chr>
1 1 Rick 45000 IT
2 2 Dan 78000 Finance
3 3 Michel 45000 HR
4 4 Ryan 89000 Operations
5 5 Gary 48000 Finance
6 6 Nina 92000 IT
# tail(INPUT)
# A tibble: 6 × 4
ID NAME SALARY DEPT
<dbl> <chr> <dbl> <chr>
1 3 Michel 45000 HR
2 4 Ryan 89000 Operations
3 5 Gary 48000 Finance
4 6 Nina 92000 IT
5 7 Simon 65000 Operations
6 8 Guru 65000 HR
# head(INPUT,3)
# A tibble: 3 × 4
ID NAME SALARY DEPT
<dbl> <chr> <dbl> <chr>
1 1 Rick 45000 IT
2 2 Dan 78000 Finance
P a g e 35 | 72
RMC FILE
3 3 Michel 45000 HR
# tail(INPUT,3)
# A tibble: 3 × 4
ID NAME SALARY DEPT
<dbl> <chr> <dbl> <chr>
1 6 Nina 92000 IT
2 7 Simon 65000 Operations
3 8 Guru 65000 HR
# names(INPUT)
[1] "ID" "NAME" "SALARY" "DEPT"
# dim(INPUT)
[1] 8 4
# ncol(INPUT)
[1] 4
# nrow(INPUT)
[1] 8
P a g e 36 | 72
RMC FILE
ASSIGNMENT 14
SUMMARY STATISTICS
str(INPUT)
summary(INPUT)
# str(INPUT)
tibble [8 × 4] (S3: tbl_df/tbl/data.frame)
$ ID : num [1:8] 1 2 3 4 5 6 7 8
$ NAME : chr [1:8] "Rick" "Dan" "Michel" "Ryan" ...
$ SALARY: num [1:8] 45000 78000 45000 89000 48000 92000 65000 65000
$ DEPT : chr [1:8] "IT" "Finance" "HR" "Operations" ...
# summary(INPUT)
ID NAME SALARY DEPT
Min. :1.00 Length:8 Min. :45000 Length:8
1st Qu.:2.75 Class :character 1st Qu.:47250 Class :character
Median :4.50 Mode :character Median :65000 Mode :character
Mean :4.50 Mean :65875
3rd Qu.:6.25 3rd Qu.:80750
Max. :8.00 Max. :92000
P a g e 37 | 72
RMC FILE
ASSIGNMENT 15
REFERRING SPECIFIC ROWS AND COLUMNS
summary(INPUT[,3])
summary(INPUT[,2:3])
summary(INPUT[,c(2,4)])
min(INPUT$SALARY)
max(INPUT$SALARY)
mean(INPUT$SALARY)
median(INPUT$SALARY)
var(INPUT$SALARY)
sd(INPUT$SALARY)
quantile(INPUT$SALARY,0.25)
quantile(INPUT$SALARY,0.50)
quantile(INPUT$SALARY,0.75)
# summary(INPUT[,3])
SALARY
Min. :45000
1st Qu.:47250
Median :65000
Mean :65875
3rd Qu.:80750
Max. :92000
# summary(INPUT[,2:3])
NAME SALARY
Length:8 Min. :45000
Class :character 1st Qu.:47250
Mode :character Median :65000
Mean :65875
3rd Qu.:80750
Max. :92000
# summary(INPUT[,c(2,4)])
NAME DEPT
Length:8 Length:8
Class :character Class :character
Mode :character Mode :character
# min(INPUT$SALARY)
[1] 45000
# max(INPUT$SALARY)
[1] 92000
P a g e 38 | 72
RMC FILE
# mean(INPUT$SALARY)
[1] 65875
# median(INPUT$SALARY)
[1] 65000
# var(INPUT$SALARY)
[1] 365267857
# sd(INPUT$SALARY)
[1] 19111.98
# quantile(INPUT$SALARY,0.25)
25%
47250
# quantile(INPUT$SALARY,0.50)
50%
65000
# quantile(INPUT$SALARY,0.75)
75%
80750
P a g e 39 | 72
RMC FILE
ASSIGNMENT 16
QUICK PLOTS
STEP 1: First go on the lower right-side window and click on install, and install
GGally and ggplot2. When installed it will be this.
STEP 2: Now according to the file, create a qplot or quick plot. After creating a
new r script, qplot would look like this.
P a g e 40 | 72
RMC FILE
P a g e 41 | 72
RMC FILE
ASSIGNMENT 18
HISTOGRAM, DENSITY PLOT, WHISKER PLOT
BOX PLOT:
HISTOGRAM:
P a g e 42 | 72
RMC FILE
SCATTER PLOT:
P a g e 43 | 72
RMC FILE
DENSITY PLOT:
P a g e 44 | 72
RMC FILE
ASSIGNMENT 19
PIE CHARTS, CLEVELAND DOT CHARTS, PAIR PLOTS
PIE CHARTS:
PAIR PLOTS:
P a g e 45 | 72
RMC FILE
P a g e 46 | 72
RMC FILE
ASSIGNMENT 20
FACTOR DATA TYPE
CREATING A VECTOR:
apple_colors <- c('green','green','yellow','red','red','red','green')
class(apple_colors)
# [1] "character"
# [1] 3
EXAMPLE:
gender <- factor(c("female", "female", "male", "female", "male"))
gender
levels(gender)
# [1] "character"
# [1] "factor"
P a g e 47 | 72
RMC FILE
P a g e 48 | 72
RMC FILE
ASSIGNMENT 21
DATE DATA TYPE
P a g e 49 | 72
RMC FILE
ASSIGNMENT 22
CORRELATION
PEARSON CORRELATION:
res <- cor.test(cardata$wt, cardata$mpg, method = "pearson")
res
KENDALL CORRELATION:
SPEARMAN CORRELATION:
P a g e 50 | 72
RMC FILE
ASSIGNMENT 23
T-TEST, TWO SAMPLE INDEPENDENT T-TEST, PAIRED TEST
data: sample_data$HEIGHT
t = -3.8005, df = 19, p-value =
0.001208
alternative hypothesis: true mean is not equal to 65
95 percent confidence interval:
57.16883 62.73117
sample estimates:
mean of x
59.95
PAIRED T TEST:
#Paired t-test
t.test(sample_data$HEIGHT~sample_data$GENDER)
P a g e 51 | 72
RMC FILE
P a g e 52 | 72
RMC FILE
ASSIGNMENT 24
CHI SQUARE T TEST
table(cardata$am,cardata$cyl)
chisq.test(table(cardata$am,cardata$cyl))
chisq.test(cardata$am,cardata$cyl)
## table(cardata$am,cardata$cyl)
4 6 8
0 3 4 12
1 8 3 2
## chisq.test(table(cardata$am,cardata$cyl))
## chisq.test(cardata$am,cardata$cyl)
P a g e 53 | 72
RMC FILE
ASSIGNMENT 25
ANOVA
ONE WAY ANOVA TEST:
summary(aov(wtloss$grps~wtloss$df_wt))
## library(readxl)
> wtloss <- read_excel("D:/Lakshay/Bcom/RMC/wtloss.xlsx")
> View(wtloss)
> summary(aov(wtloss$grps~wtloss$df_wt))
Df Sum Sq Mean Sq F value
wtloss$df_wt 1 12.27 12.273 17.36
Residuals 18 12.73 0.707
Pr(>F)
wtloss$df_wt 0.00058 ***
Residuals
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’
0.1 ‘ ’ 1
0.5 1 2
OJ 3 1 3
VC 1 1 1
> res.aov2 <- aov(len ~ supp + dose, data = ANN2)
> summary(res.aov2)
Df Sum Sq Mean Sq F value
supp 1 38.7 38.7 1.506
dose 1 690.5 690.5 26.910
Residuals 7 179.6 25.7
Pr(>F)
supp 0.25935
dose 0.00127 **
Residuals
---
P a g e 54 | 72
RMC FILE
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’
0.1 ‘ ’ 1
EXAMPLE 2:
res.aov2 <- aov(plant_height ~ Watering_freq + sunlight_exp, data =
twowayann)
summary(res.aov2)
## library(readxl)
> twowayann <- read_excel("D:/Lakshay/Bcom/RMC/twowayann.xlsx")
> View(twowayann)
> res.aov2 <- aov(plant_height ~ Watering_freq + sunlight_exp, data =
twowayann)
> summary(res.aov2)
Df Sum Sq Mean Sq F value
Watering_freq 2 1.281 0.6405 1.694
sunlight_exp 3 7.028 2.3427 6.198
Residuals 4 1.512 0.3780
Pr(>F)
Watering_freq 0.2931
sunlight_exp 0.0552 .
Residuals
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’
0.1 ‘ ’ 1
P a g e 55 | 72
RMC FILE
ASSIGNMENT 26
REGRESSION-LINEAR
## print(model)
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
-39.7137 0.6847
EXAMPLE 2:
PLOT:
P a g e 56 | 72
RMC FILE
ASSIGNMENT 27
MULTIPLE LINEAR AGGRESSION
## input
Ozone Wind Temp
1 41 7.4 67
2 36 8.0 72
3 12 12.6 74
4 18 11.5 62
5 NA 14.3 56
6 28 14.9 66
7 23 8.6 65
8 19 13.8 59
9 8 20.1 61
10 NA 8.6 69
11 7 6.9 74
12 16 9.7 69
13 11 9.2 66
14 14 10.9 68
15 18 13.2 58
16 14 11.5 64
17 34 12.0 66
18 6 18.4 57
19 30 11.5 68
20 11 9.7 62
21 1 9.7 59
22 11 16.6 73
23 4 9.7 61
24 32 12.0 61
25 NA 16.6 57
26 NA 14.9 58
27 NA 8.0 57
28 23 12.0 67
29 45 14.9 81
30 115 5.7 79
31 37 7.4 76
32 NA 8.6 78
33 NA 9.7 74
34 NA 16.1 67
35 NA 9.2 84
36 NA 8.6 85
37 NA 14.3 79
38 29 9.7 82
39 NA 6.9 87
40 71 13.8 90
P a g e 57 | 72
RMC FILE
41 39 11.5 87
42 NA 10.9 93
43 NA 9.2 92
44 23 8.0 82
45 NA 13.8 80
46 NA 11.5 79
47 21 14.9 77
48 37 20.7 72
49 20 9.2 65
50 12 11.5 73
## print(model)
Call:
lm(formula = Ozone ~ Wind + Temp, data = input)
Coefficients:
(Intercept) Wind Temp
-58.239 -0.739 1.329
P a g e 58 | 72
RMC FILE
ASSIGNMENT 28
LOGISTICS REGRESSION
## print(summary(data1))
Call:
glm(formula = am ~ cyl + hp + wt, family = binomial, data = input)
Deviance Residuals:
Min 1Q Median 3Q
-2.17272 -0.14907 -0.01464 0.14116
Max
1.27641
Coefficients:
Estimate Std. Error z value
(Intercept) 19.70288 8.11637 2.428
cyl 0.48760 1.07162 0.455
hp 0.03259 0.01886 1.728
wt -9.14947 4.15332 -2.203
Pr(>|z|)
(Intercept) 0.0152 *
cyl 0.6491
hp 0.0840 .
wt 0.0276 *
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’
0.1 ‘ ’ 1
P a g e 59 | 72
RMC FILE
ASSIGNMENT 29
STEPWISE REGRESSION
## summary(x2)
Call:
lm(formula = mpg ~ cyl + disp + hp + drat + wt + qsec + vs +
am + gear + carb, data = cardata)
Residuals:
Min 1Q Median 3Q Max
-3.4506 -1.6044 -0.1196 1.2193 4.6271
Coefficients:
Estimate Std. Error t value
(Intercept) 12.30337 18.71788 0.657
cyl -0.11144 1.04502 -0.107
disp 0.01334 0.01786 0.747
hp -0.02148 0.02177 -0.987
drat 0.78711 1.63537 0.481
wt -3.71530 1.89441 -1.961
qsec 0.82104 0.73084 1.123
vs 0.31776 2.10451 0.151
am 2.52023 2.05665 1.225
gear 0.65541 1.49326 0.439
carb -0.19942 0.82875 -0.241
Pr(>|t|)
(Intercept) 0.5181
cyl 0.9161
disp 0.4635
hp 0.3350
drat 0.6353
wt 0.0633 .
qsec 0.2739
vs 0.8814
am 0.2340
gear 0.6652
carb 0.8122
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’
0.1 ‘ ’ 1
P a g e 60 | 72
RMC FILE
## summary(forwardnew)
Call:
lm(formula = mpg ~ wt + cyl + hp, data = cardata)
Residuals:
Min 1Q Median 3Q Max
-3.9290 -1.5598 -0.5311 1.1850 5.8986
Coefficients:
Estimate Std. Error t value
(Intercept) 38.75179 1.78686 21.687
wt -3.16697 0.74058 -4.276
cyl -0.94162 0.55092 -1.709
hp -0.01804 0.01188 -1.519
Pr(>|t|)
(Intercept) < 2e-16 ***
wt 0.000199 ***
cyl 0.098480 .
hp 0.140015
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’
0.1 ‘ ’ 1
## forwardnew$coefficients
(Intercept) wt cyl
38.7517874 -3.1669731 -0.9416168
hp
-0.0180381
P a g e 61 | 72
RMC FILE
## summary(backwardnew)
Call:
lm(formula = mpg ~ wt + qsec + am, data = cardata)
Residuals:
Min 1Q Median 3Q Max
-3.4811 -1.5555 -0.7257 1.4110 4.6610
Coefficients:
Estimate Std. Error t value
(Intercept) 9.6178 6.9596 1.382
wt -3.9165 0.7112 -5.507
qsec 1.2259 0.2887 4.247
am 2.9358 1.4109 2.081
Pr(>|t|)
(Intercept) 0.177915
wt 6.95e-06 ***
qsec 0.000216 ***
am 0.046716 *
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’
0.1 ‘ ’ 1
## backwardnew$coefficients
(Intercept) wt qsec
9.617781 -3.916504 1.225886
am
2.935837
P a g e 62 | 72
RMC FILE
Step: AIC=68.92
mpg ~ disp + hp + drat + wt + qsec + vs + am + gear + carb
Step: AIC=66.97
mpg ~ disp + hp + drat + wt + qsec + am + gear + carb
Step: AIC=65.12
mpg ~ disp + hp + drat + wt + qsec + am + gear
P a g e 63 | 72
RMC FILE
Step: AIC=63.46
mpg ~ disp + hp + drat + wt + qsec + am
Step: AIC=62.16
mpg ~ disp + hp + wt + qsec + am
Step: AIC=61.52
mpg ~ hp + wt + qsec + am
P a g e 64 | 72
RMC FILE
Step: AIC=61.31
mpg ~ wt + qsec + am
## bothnew$anova
Step Df Deviance Resid. Df Resid. Dev
1 NA NA 21 147.4944
2 - cyl 1 0.07987121 22 147.5743
3 - vs 1 0.26852280 23 147.8428
4 - carb 1 0.68546077 24 148.5283
5 - gear 1 1.56497053 25 150.0933
6 - drat 1 3.34455117 26 153.4378
7 - disp 1 6.62865369 27 160.0665
8 - hp 1 9.21946935 28 169.2859
AIC
1 70.89774
2 68.91507
3 66.97324
4 65.12126
5 63.45667
6 62.16190
7 61.51530
8 61.30730
## bothnew$coefficients
(Intercept) wt qsec
9.617781 -3.916504 1.225886
am
2.935837
## summary(bothnew)
Call:
lm(formula = mpg ~ wt + qsec + am, data = cardata)
Residuals:
Min 1Q Median 3Q Max
-3.4811 -1.5555 -0.7257 1.4110 4.6610
P a g e 65 | 72
RMC FILE
Coefficients:
Estimate Std. Error t value
(Intercept) 9.6178 6.9596 1.382
wt -3.9165 0.7112 -5.507
qsec 1.2259 0.2887 4.247
am 2.9358 1.4109 2.081
Pr(>|t|)
(Intercept) 0.177915
wt 6.95e-06 ***
qsec 0.000216 ***
am 0.046716 *
---
Signif. codes:
0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’
0.1 ‘ ’ 1
P a g e 66 | 72
RMC FILE
ASSIGNMENT 30
TABLES USED
INPUT TABLE:
CARDATA TABLE:
P a g e 67 | 72
RMC FILE
PAIRED T TEST:
ANN2 TABLE:
P a g e 68 | 72
RMC FILE
TWOWAYANN:
WTLOSS:
P a g e 69 | 72
RMC FILE
P a g e 70 | 72
RMC FILE
ASSIGNMENT 17
R Notebook
summary(INPUT[,3])
summary(INPUT[,2:3])
NAME SALARY
Length:8 Min. :45000
Class :character 1st Qu.:47250
Mode :character Median :65000
Mean :65875
3rd Qu.:80750
Max. :92000
min(INPUT$SALARY)
max(INPUT$SALARY)
mean(INPUT$SALARY)
Try executing this chunk by clicking the *Run* button within the chunk or by
placing your cursor inside it and pressing *Ctrl+Shift+Enter*.
```{r}
plot(cars)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing
Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved
alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
P a g e 71 | 72
RMC FILE
The preview shows you a rendered HTML copy of the contents of the editor. Consequently,
unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when
it was last run in the editor is displayed.
P a g e 72 | 72