RemoveWatermark pdf24 Merged+
RemoveWatermark pdf24 Merged+
OUTPUT:
The addition of a and b is: 8
The multiplication of a and b is: 15
The division of a and b is: 1.666667
The subtraction of a and b is: 2
The exponent of a and b is: 125
EXPERIMENT – 1(B)
PROBLEM STATEMENT : WRITE A PROGRAM TO ILLUSTRATE
VARIABLE ASSIGNMENT IN R
CODE :
#program illustrating Variable assignment in R
#assignment using equal operator
a=2
#assignment using leftward operator
b <- 5
#assignment using rightward operator
7 -> c
cat(a,b,c)
OUTPUT:
2 5 7
EXPERIMENT – 1(C)
PROBLEM STATEMENT : WRITE A PROGRAM TO ILLUSTRATE DATA
TYPES IN R.
CODE:
#program to illustrate data types in R
#NUMERIC DATA TYPE
a <- 10
cat(“The data type of a is”,class(a),”\n\n”)
#LOGICAL DATA TYPE
TRUE -> b
cat(“The data type of b is”,class(b),”\n\n”)
#INTEGER DATA TYPE
c = 2L
cat(“The data type of c is”,class(c),”\n\n”)
#COMPLEX DATA TYPE
d = 2+3i
cat(“The data type of d is”,class(d),”\n\n”)
#CHAR DATA TYPE
e = "Samatha"
cat(“The data type of e is”,class(e),”\n\n”)
OUTPUT:
The data type of a is numeric
The data type of b is logical
The data type of c is integer
The data type of d is complex
The data type of e is character
EXPERIMENT-2(A)
PROBLEM STATEMENT : WRITE A PROGRAM TO ILLUSTRATE IF-ELSE-
ELSE-IF IN R
CODE :
#IF-ELSE
age <- 20L
if(age >= 18)
{
cat("Eligible to vote")
}else{
cat("Not Eligible to vote")
}
OUTPUT :
Eligible to vote
ELSE-IF :
CODE :
#ELSE-IF
y <- -3L
if(y < 0){
cat(y," is less than 0")
}else if(y > 0){
cat(y,"is greater than 0")
}else{
cat(y,"is Zero")
}
OUTPUT :
-3 is less than 0
EXPERIMENT – 2(B)
PROBLEM STATEMENT : WRITE A PROGRAM TO ILLUSTRATE
WHILE AND FOR LOOPS IN R.
CODE :
WHILE LOOP:
#WHILE-LOOP
c("It is an Example of while loop.")
x=1
while(x < 6){
cat(x," ")
x=x+1
}
OUTPUT :
It is an Example of while loop.
1 2 3 4 5
CODE:
FOR LOOP:
#For loop
#WHILE-LOOP
c("It is an Example of while loop")
x=1
while(x < 6){
cat(x," ")
x=x+1
}
OUTPUT :
1 2 4 5 7 8 10 17
EXPERIMENT-2(C)
PROBLEM STATEMENT : WRITE A PROGRAM TO DEMONSTRATE
WORKING WITH FUNCTIONS IN R.
CODE :
#Demonstration of functions in R.
#Functions With Parameters.
add_numbers <- function(a,b){
result <- a + b
return(result)
}
sum_result <- add_numbers(13,24)
print(sum_result)
OUTPUT :
37
CODE:
#Functions Without Parameters.
fun_without_parameters <- function(){
for(i in 1:5){
a <- i^3
print(a)
}
}
fun_without_parameters()
OUTPUT :
[1] 1
[1] 8
[1] 27
[1] 64
[1] 125
EXPERIMENT – 3(A)
PROBLEM STATEMENT : WRITE A PROGRAM TO DEMONSTRATE WORKING
WITH VECTORS,LISTS IN R.
CODE:
#VECTOR OPERATIONS.
#CREATION OF VECTOR.
vec <- c(2,4,6,8)
vec
#vector Exponent with scalar.
vec <- c(10,21,50,90)
vec^5
#vector Exponent with vector.
vec1 <- c(5,10,15,20)
vec2 <- c(30,40)
vec1^vec2
#vector Exponent with vector.
vec1 <- c(11,21,31,41)
vec2 <- c(4,5,6)
vec1^vec2
#Addition of vector with scalar.
c(6,7,8)+6
#Addition of vector with vector.
c(6.5,4.7,3.1)+c(2.3,5.1,8.4)
#Addition of vector with vector.
c(1,2,3,4)+c(7.1,6.1,5.2)
#Multiplication of vector with scalar.
c(9,3,4)*7
#Multiplication of vector with vector.
c(4,6,9)*c(2,5,10)
#Multiplication of vector with vector.
c(3,5,8)*c(7,10,9,2)
#Subtraction of scalar from vector.
c(2,5,7)-4
#Subtraction of vector from vector.
c(5,9,3)-c(4,5,8)
#Subtraction of vector with vector.
c(4,8,2)-c(3,6,9,7)
#Length of vector
s = length(vec)
print(s)
#accessing the elements in a vector using indexes.
data <- c(7,14,21,28)
print(data[3])
out_list = list(vec,char_vec,long_vec)
#Printing the new list that created.
out_list
OUTPUT :
[1] 2 4 6 8
[1] 100000 4084101 312500000 5904900000
[1] 9.313226e+20 1.000000e+40 1.917511e+35 1.099512e+52
[1] 14641 4084101 887503681 2825761
Warning message:
In vec1^vec2 :
longer object length is not a multiple of shorter object length
[1] 12 13 14
[1] 8.8 9.8 11.5
[1] 8.1 8.1 8.2 11.1
Warning message:
In c(1, 2, 3, 4) + c(7.1, 6.1, 5.2) :
longer object length is not a multiple of shorter object length
[1] 63 21 28
[1] 8 30 90
[1] 21 50 72 6
Warning message:
In c(3, 5, 8) * c(7, 10, 9, 2) :
longer object length is not a multiple of shorter object length
[1] -2 1 3
[1] 1 4 -5
[1] 1 2 -7 -3
Warning message:
In c(4, 8, 2) - c(3, 6, 9, 7) :
longer object length is not a multiple of shorter object length
[1] 4
[1] 21
[1] 1 2 4
[[1]]
[1] 5 4 3 2
[[2]]
[1] "A" "B" "C" "D"
[[3]]
[1] TRUE FALSE TRUE
[[1]]
[1] "A" "B" "C" "D"
[[1]]
[1] 5 4 3 2
[[1]]
NULL
[[1]]
NULL
EXPERIMENT – 3(B)
PROBLEM STATEMENT : WRITE A PROGRAM TO DEMONSTRATE
WORKING WITH ARRAYS AND MATRIX IN R.
CODE :
#Working with Arrays
#Creation of Vectors
V1 = c(1,3,5,7,9,11,13)
V2 = c(1:9)
#printing the vector
cat(“The data of v1 is”,v1)
cat(“The data of v2 is”,v2)
#Array Creation
A1 = array(c(v1,v1))
#printing the array that is created
Cat(“The array is:,A1)
#Dimension of array
Cat(“The dimensions of the array A1 is”,dim(A1))
#Working of Matrix
#OPERATIONS ON MATRIX.
#CREATION OF MATRIX.(COLUMN-WISE).
A = matrix(nrow = 2,ncol = 2,data=c(4,7,3,9))
A
#CREATION OF MATRIX.(COLUMN-WISE).
#CREATION OF MATRIX.(ROW-WISE).
C = matrix(nrow = 3,ncol = 2,data = c(25,50,12,24,4,8),byrow=T)
C
#ACCESS TO ROWS,COLUMNS AND SUBMATRICES.
C[3,]
C[,2]
C[2:3,1:2]
X = matrix(nrow = 2,ncol=2,data=c(2,5,7,3))
rnames = c("A","B")
cnames = c("C","D")
BT+3
#SUBTRACTION OF MATRICES FROM MATRICES.
BT-C
#SUBTRACTION OF SCALAR FROM MATRIX.
BT-4
#MULTIPLICATION OF MATRIX WITH SCALAR.
C*4
#MULTIPLICATION OF MATRIX WITH MATRIX.
B %*% C
#DIMENSION OF MATRIX.
dim(C)
#APPENDING NEW ROWS AND COLUMNS TO A MATRIX.
A1 = rbind(A,c(10,24))
A1
B1 = cbind(B,c(11,7))
B1
OUTPUT :
The data of v1 is 1 3 5 7 9 11 13
The data of v2 is 1 3 5 7 9
The array is 1 3 5 7 9 11 13 1 3 5 7 9 11 13
The dimensions of the array A1 is 14
[,1] [,2]
[1,] 4 3
[2,] 7 9
[,1] [,2] [,3]
[1,] 5 12 18
[2,] 10 15 20
[,1] [,2]
[1,] 25 50
[2,] 12 24
[3,] 4 8
[1] 4 8
[1] 50 24 8
[,1] [,2]
[1,] 12 24
[2,] 4 8
C D
A 2 5
B 7 3
[,1] [,2]
[1,] 5 10
[2,] 12 15
[3,] 18 20
[,1] [,2]
[1,] 30 60
[2,] 24 39
[3,] 22 28
[,1] [,2]
[1,] -20 -40
[2,] 0 -9
[3,] 14 12
[,1] [,2]
[1,] 1 6
[2,] 8 11
[3,] 14 16
[,1] [,2]
[1,] 100 200
[2,] 48 96
[3,] 16 32
[,1] [,2]
[1,] 341 682
[2,] 510 1020
[1] 3 2
[,1] [,2]
[1,] 4 3
[2,] 7 9
[3,] 10 24
EXPERIMENT – 3(C)
PROBLEM STATEMENT : WRITE A PROGRAM TO DEMONSTRATE WORKING
WITH DATA FRAMES IN R.
CODE :
EmployeeDetails = data.frame(empName =
c("Sam","Vasavi","Sai","Usha","Durga","Manasa"),empId = c(101,102,103,104,105,106),Role
= c("Full Stack Developer","Frontend Developer","Backend Developer","ML Engineer","Data
Scientist","Data Analyst"),Salary = c(50000,60000,70000,80000,90000,95000))
EmployeeDetails
1) Slice Data Frame
EmployeeDetails[[4]][5]
EmployeeDetails[,3]
EmployeeDetails[3,]
EmployeeDetails[c("empName","Role")]
5) Write a program to illustrate Data Frame Selection of Elements in a Data Frame
NewEmployeeDetails[c("Company")]
6) Write a program to illustrate Sorting a Data Frame
OUTPUT :
empName empId Role Salary
1 Sam 101 Full Stack Developer 50000
2 Vasavi 102 Frontend Developer 60000
3 Sai 103 Backend Developer 70000
4 Usha 104 ML Engineer 80000
5 Durga 105 Data Scientist 90000
6 Manasa 106 Data Analyst 95000
[1] 90000
empId
1 101
2 102
3 103
4 104
5 105
6 106
empName Role
1 Sam Full Stack Developer
2 Vasavi Frontend Developer
3 Sai Backend Developer
4 Usha ML Engineer
5 Durga Data Scientist
6 Manasa Data Analyst
Company
1 Amazon
2 Flipkart
3 Google
4 Microsoft
5 Tcs
6 Infosys
EXPERIMENT – 3(D)
PROBLEM STATEMENT:Write a program to demonstrate working
with Factors in R
CODE:
cat("WORKING WITH FRACTORS IN R")
cat("It helps to rduce data redundability and to save a lot of space in the
memory")
creating a list
film=c('comdey','historical','classical','animated','crime','historical','classic
al','animated')
using factor
gfactor=factor(film)
printing the factor of given list
gfactor
printing summarys
cat("Summary of original list is :")
summary(film)
cat("Summary of factor list is :")
summary(gfactor)
printing list as string
str(film)
accessing two elements of a factored list at a time
cat("the 3rd and 4th elements of gfactor are : ")
film[c(3,4)]
accessing elements of a factored list using colon(":")
cat("the elements of gfactor from 2 to 5 are : ")
film[c(2:5)]
checking factor or not
is.factor(film)
accessing elements of a factored
list cat("the 3rd element of factor list
is: ") film[3]
cat("the 7th element of factor list is: ")
film[7]
OUTPUT:
>cat("WORKING WITH FRACTORS
IN R") WORKING WITH FRACTORS
IN R
>cat("It helps to rduce data redundability and to save a lot of space in
the memory")
It helps to rduce data redundability and to save a lot of space in the
memory
> creating a list
>
film=c('comdey','historical','classical','animated','crime','historical','classi
cal','animated')
> using factor
>gfactor=factor(film)
> printing the factor of given list
>gfactor
[1] comdey historical classical animated crime historical classical
[8] animated
Levels: animated classical comdey crime historical
> printing summarys
>cat("Summary of original list is :")
Summary of original list is :
>summary(film) Length
Class Mode
8 character character
>cat("Summary of factor list is :")
Summary of factor list is :
>summary(gfactor)
animated classical comdey crime historical
2 2 1 1 2
> printing list as string
>str(film)
chr [1:8] "comdey" "historical" "classical" "animated" "crime"
"historical" ...
> accessing two elements of a factored list at a time
>cat("the 3rd and 4th elements of gfactor are :
") the 3rd and 4th elements of gfactor are :
>film[c(3,4)]
[1] "classical" "animated"
> accessing elements of a factored list using colon(":")
>cat("the elements of gfactor from 2 to 5 are :
") the elements of gfactor from 2 to 5 are :
>film[c(2:5)]
[1] "historical" "classical" "animated" "crime"
> checking factor or not
>is.factor(film)
[1] FALSE
> accessing elements of a factored list
>cat("the 3rd element of factor list is: ")
the 3rd element of factor list is:
> film[3]
[1] "classical"
> cat("the 7th element of factor list is:
") the 7th element of factor list is:
> film[7]
[1] "classical"
>
EXPERIMENT-4(A)
PROBLEM STATEMENT:WRITE A PROGRAM TO DEMONSTRATE
WORKING WITH INSTALLING AND LOADING PACKAGES IN R.
CODE:
#install.packages("package-name")
.libPaths()
#see all packages installed
search()
install.packages("readxl")
library('readxl')
install.packages("XML")
install.packages("moments")
library(moments)
install.packages("ggplot2")
library(ggplot2)
install.packages("scatterplot3d")
library(scatterplot3d)
install.packages("ggmap")
library(ggmap)
install.packages("plotrix")
library(plotrix)
OUTPUT:
> #installing packages("package_name")
> .libPaths()
[1] "C:/Users/vhyma/AppData/Local/R/win-library/4.3"
> library()
> search()
> install.packages("readxl")
C:\Users\vhyma\AppData\Local\Temp\Rtmp6nWJUQ\
downloaded_packages
> library("readxl")
> install.packages("XML")
C:\Users\vhyma\AppData\Local\Temp\Rtmp6nWJUQ\
downloaded_packages
> install.packages("moments")
> library(moments)
> install.packages("ggplot2")
C:\Users\vhyma\AppData\Local\Temp\Rtmp6nWJUQ\
downloaded_packages
EXPERIMENT – 4(B)
PROBLEM STATEMENT:WRITE A PROGRAMS TO
DEMONSTRATE WORKING WITH DATA RESHAPING IN R.
CODE:
#create vector objects
state<-c("AP","TELANGANA","TAMIL NADU")
city<-c("VIZAG","HYDERABAD","CHENNAI")
employ_comp.A<-c(43602,58104,90294)
details<-cbind(state,city,employ_comp.A)
#print header
new.details<-data.frame(city = c("BANGLORE","JAIPUR"),state =
c("KARNATAKA","RAJASTHAN"),employ_comp.A =
c("90230","53949"),stringsAsFactors = FALSE)
#print header
all.details<-rbind(details,new.details)
print(all.details)
library(MASS)
merged.Pima<-merge(x = Pima.te,y = Pima.tr,
by.x=c("bp","bmi"),
by.y=c("bp","bmi")
)
print(merged.Pima)
nrow(merged.Pima)
OUTPUT:
### The first data frame
state city employ_comp.A
[1,] "AP" "VIZAG""43602"
[2,] "TELANGANA" "HYDERABAD" "58104"
[3,] "TAMIL NADU" "CHENNAI" "90294"
# # # The second data frame
city state employ_comp.A
1 BANGLORE KARNATAKA 90230
2 JAIPUR RAJASTHAN 53949
# # # The combined data frame
state city employ_comp.A
1 AP VIZAG 43602
2 TELANGANA HYDERABAD 58104
3 TAMIL NADU CHENNAI 90294
4 KARNATAKA BANGLORE 90230
5 RAJASTHAN JAIPUR 53949
[1] 17
EXPERIMENT-5
PROBLEM STATEMENT : Write a program to demonstrate the following
operations on the given data set.
CODE :
CODE :
s1 <- read.csv("R_data_set(sam).csv")
head(s1)
summary(s1)
OUTPUT :
Pregnancies Glucose BloodPressure SkinThickness Insulin BMI
DiabetesPedigreeFunction Age Outcome
1 6 148 72 35 0 33.6
0.627 50 1
2 1 85 66 29 0 NA
0.351 31 0
3 8 183 64 0 0 23.3
0.672 32 1
4 1 89 66 23 94 NA
0.167 21 0
5 0 137 40 35 168 43.1
2.288 33 1
6 5 116 74 0 0 25.6
0.201 30 0
> summary(s1)
Pregnancies Glucose BloodPressure
Min. : 0.000 Min. : 0.0 Min. : 0.00
1st Qu.: 1.000 1st Qu.: 99.0 1st Qu.: 64.00
Median : 4.000 Median :113.0 Median : 72.00
Mean : 4.118 Mean :118.7 Mean : 69.26
3rd Qu.: 7.000 3rd Qu.:141.0 3rd Qu.: 80.00
Max. :17.000 Max. :197.0 Max. :122.00
SkinThickness Insulin BMI
Min. : 0.00 Min. : 0.00 Min. : 0.00
1st Qu.: 0.00 1st Qu.: 0.00 1st Qu.:27.20
Median :23.00 Median : 0.00 Median :32.40
Mean :19.96 Mean : 75.08 Mean :31.66
3rd Qu.:32.00 3rd Qu.:115.00 3rd Qu.:37.00
Max. :60.00 Max. :846.00 Max. :55.00
NA's 2
DiabetesPedigreeFunction Age
Min. :0.0850 Min. :21.00
1st Qu.:0.2480 1st Qu.:24.00
Median :0.3560 Median :30.00
Mean :0.4632 Mean :33.33
3rd Qu.:0.5870 3rd Qu.:41.00
Max. :2.2880 Max. :69.00
Outcome
Min. :0.0000
1st Qu.:0.0000
Median :0.0000
Mean :0.3491
3rd Qu.:1.0000
Max. :1.0000
2) DATA DESCRIPTION
#MEAN
CODE :
mean = mean(s1$Pregnancies)
mean
OUTPUT :
[1] 4.118343
#MEDIAN
CODE :
median = median(s1$SkinThickness)
median
OUTPUT :
[1] 23
#MODE
CODE :
install.packages("modeest")
library(modeest)
mode = mfv(s1$BMI)
mode
OUTPUT :
[1] NA
#RANGE
CODE:
max = max(s1$Pregnancies)
max
min = min(s1$Pregnancies)
min
range(s1$Pregnancies)
ranged = max - min
cat("The difference between max and min,Range is:\n")
ranged
OUTPUT:
[1] 17
[1] 0
[1] 0 17
The difference between max and min,Range is:
[1] 17
#VARIANCE
CODE:
variance = var(s1$Pregnancies)
cat("Variance of the Pregnancies is:",variance)
OUTPUT:
Variance of the Pregnancies is: 11.9621
#STANDARD DEVIATION
CODE:
standard_deviation = sd(s1$Pregnancies)
cat("The Standard deviation of Pregnancies is:",standard_deviation)
mean = mean(s1$Pregnancies)
mean
cv1 = (standard_deviation/mean)*100
cv1
stdev2 = sd(s1$Insulin)
cat("The Standard deviation of Insulin is:",stdev2)
mean2 = mean(s1$Insulin)
mean2
cv2 = (stdev2/mean2)*100
cv2
if(cv1 > cv2)
{
cat("Pregnancies have more variability than Insulin.")
}else
{
cat("Insulin has more Variability than Pregnancies.")
}
OUTPUT :
The Standard deviation of Pregnancies is: 3.458627
[1] 4.118343
[1] 83.98103
The Standard deviation of Insulin is: 119.0564
[1] 75.07692
[1] 158.5792
Insulin has more Variability than Pregnancies.
#QUANTILES
CODE:
quartiles = quantile(s1$Pregnancies)
quartiles
OUTPUT:
0% 25% 50% 75% 100%
0 1 4 7 17
#DECILES
CODE:
probs = seq(0,1,0.1)
quantile(s1$Pregnancies,probs)
OUTPUT:
0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%
0.0 0.0 1.0 2.0 2.0 4.0 4.0 5.6 7.0 9.0 17.0
#PERCENTILES
CODE:
k = (1:300)
probs1 = seq(0,1,0.01)
quantile(k,probs)
OUTPUT:
0% 10% 20% 30% 40% 50% 60% 70% 80%
1.0 30.9 60.8 90.7 120.6 150.5 180.4 210.3 240.2
90% 100%
270.1 300.0
#INTERQUARTILE RANGE
CODE:
iqr
OUTPUT:
[1] 9.8
#SUMMARY OF DATAFRAME
Summary = summary(s1$BMI)
OUTPUT:
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
0.00 27.20 32.40 31.66 37.00 55.00 2
3) DATA CLEANING
#Installing dplyr Package
install.packages("dplyr")
library(dplyr)
install.packages("readxl")
library("readxl")
getwd()
setwd("C:/Users/samat/Downloads")
Emp_Details = read_excel('MyDataSet_Employee_details.xlsx')
Emp_Details
gimpse(Emp_Details)
OUTPUT :
Emp_Id Emp_name Salary Job_Role
<dbl> <chr> <dbl> <chr>
1 101 Samatha 50000 FrontendDeveloper
2 102 Vasavika 60000 NA
3 103 Jahnavi 70000 BackendDeveloper
4 104 Saranya 80000 DataScientist
5 105 Kavya NA JavaDeveloper
6 106 Prajwala 30000 SoftwareDeveloper
7 107 Satya NA PythonDeveloper
Rows: 7
Columns: 4
$ Emp_Id <dbl> 101, 102, 103, 104, 105, 106, 107
$ Emp_name <chr> "Samatha", "Vasavika", "Jahnavi", "Sar…
$ Salary <dbl> 50000, 60000, 70000, 80000, NA, 30000,…
$ Job_Role <chr> "FrontendDeveloper", NA, "BackendDevel…
#MISSING VALUES
install.packages("tidyr")
library(tidyr)
CODE:
Emp_Details_no_missing
OUTPUT:
Emp_Id Emp_name Salary Job_Role
<dbl> <chr> <dbl> <chr>
1 101 Samatha 50000 FrontendDeveloper
2 103 Jahnavi 70000 BackendDeveloper
3 104 Saranya 80000 DataScientist
4 106 Prajwala 30000 SoftwareDeveloper
CODE:
# When you apply replace_na(., na.rm = TRUE)) , it produces a double type, which cannot be directly assigned
to an integer column.
OUTPUT:
Emp_Id Emp_name Salary Job_Role
<int> <chr> <int> <chr>
1 104 Samatha 60000 FrontendDeveloper
2 104 Vasavika 60000 NA
3 104 Jahnavi 60000 BackendDeveloper
4 104 Saranya 60000 DataScientist
5 104 Kavya 60000 JavaDeveloper
6 104 Prajwala 60000 SoftwareDeveloper
7 104 Satya 60000 PythonDeveloper
#UNIQUE COLUMNS
CODE:
df = data.frame(teams =
c(1,2,4,4),points=c(NA,8,7,10),rebounds=c(1,2,4,4))
df
unique_cols <- df %>% distinct(teams,rebounds,keep_all = TRUE)
unique_cols
OUTPUT:
> df
teams points rebounds
1 1 NA 1
2 2 8 2
3 4 7 4
4 4 10 4
> unique_cols
teams rebounds keep_all
1 1 1 TRUE
2 2 2 TRUE
3 4 4 TRUE
#REMOVE EMPTY VALUES
CODE:
vec1 = c(2,4,6,8,10,NA,12,NA)
vec1
length(vec1)
mean(vec1)
n = mean(vec1, na.rm = TRUE)
n
OUTPUT:
> vec1
[1] 2 4 6 8 10 NA 12 NA
> length(vec1)
[1] 8
> mean(vec1)
[1] NA
> n = mean(vec1, na.rm = TRUE)
> n
[1] 7
EXPERIMENT – 6
A) WRITE PROGRAMS TO DEMONSTRATE THE DIFFERENT PLOTS
LIKE LINECHART, BARCHART,HISTOGRAM,PIECHART,STACKED
BAR CHART,SCATTER PLOT,BOX PLOT,HEAT MAP BY LOADING
THE REALTIME DATA
i) Histogram
CODE:
library(RColorBrewer)
data("VADeaths")
par(mfrow=c(2,3))
library(RColorBrewer)
data("VADeaths")
par(mfrow=c(2,3))
hist(VADeaths,breaks=10,col=brewer.pal(3,"Set3"),main="Set3 3
colors")
hist(VADeaths,breaks=3,col=brewer.pal(3,"Set2"),main="Set2 3
colors")
hist(VADeaths,breaks=7,col=brewer.pal(3,"Set1"),main="Set1 3
colors")
hist(VADeaths,breaks=2,col=brewer.pal(8,"Set3"),main="Set3 8
colors")
hist(VADeaths,col=brewer.pal(8,"Greys"),main="Greys 8 colors")
hist(VADeaths,col=brewer.pal(8,"Greens"),main="Greens 8
colors")
OUTPUT:
#Bar/Line chart
plot(AirPassengers,type='l')
OUTPUT:
ii)Barplot
barplot(iris$Petal.Length)
OUTPUT:
barplot(iris$Petal.Length,col = brewer.pal(3,"Set1"))
OUTPUT:
barplot(table(iris$Species,iris$Petal.Length),col=brewer.pal(3,"Set1"))
OUTPUT:
plot(iris,col=brewer.pal(3,"Set1"))
OUTPUT:
v) Heat Map
heatmap(as.matrix(mtcars))
OUTPUT:
vi)Mosaic Map
mosaicplot(as.matrix(A),col=heat.colors(9))
mosaicplot(as.matrix(A),col=topo.colors(9))
mosaicplot(as.matrix(mtcars),col=topo.colors(150))
OUTPUT:
EXPERIMENT-7
PROBLEM STATEMENT : DATA VISUALISATIONS-II IN R.
(A) WRITE PROGRAMS TO ILLUSTRATE THE DIFFERENT PLOTTING DATA
DISTRIBUTIONS LIKE,UNIVARIATE DISTRIBUTIONS,BIVARIATE DISTRIBUTIONS.
CODE:
#DATA VISUALISATIONS IN R
install.packages("ggplot2")
library(ggplot2)
install.packages("mosaicData")
library(mosaicData)
data(Marriage,package="mosaicData")
ggplot(Marriage,aes(x=race))+geom_bar(fill="violet", color="black")
+labs(x="Race",y="Frequency",title="Participants")
OUTPUT:
> x = c(3,6,9,12,15,18)
> length(x)
[1] 6
> summary(x)
Min. 1st Qu. Median Mean 3rd Qu. Max.
3.00 6.75 10.50 10.50 14.25 18.00
> table(x)
x
3 6 9 12 15 18
1 1 1 1 1 1
library(ggplot2)
library(mosaicData)
CODE:
#PIE CHART
library(ggplot2)
df<-data.frame(value = c(2,5,3,9) , group=paste0("G",1:4))
ggplot(df,aes(x=" ",y=value,fill=group))+geom_col(color="black")+coord_polar(theta="y")
ggplot(df,aes(x=" ",y=value,fill=group))+geom_col(color="black")
+geom_label(aes(label=value),position=position_stack(vjust=0.5),show.legend = FALSE)
+coord_polar(theta="y")
OUTPUT:
library(ggplot2)
CODE:
#HISTOGRAM
set.seed(1234)
OUTPUT:
> set.seed(1234)
> head(df1)
sex weight
1 F 49
2 F 56
3 F 60
4 F 43
5 F 57
6 F 58
CODE:
#BASIC HISTOGRAM
ggplot(df1,aes(x=weight))+geom_histogram()
#change the width of the bins
ggplot(df1,aes(x=weight))+geom_histogram(binwidth=2)
#change clors
p <- ggplot(df1,aes(x=weight))
+geom_histogram(color="black",fill="lightgreen")
p
###Add mean line and density plot on the histogram
##Add mean line
p+geom_vline(aes(xintercept =
mean(weight)),color="blue",linetype="dashed",size=1)
#Histogram with density plot
ggplot(df1,aes(x=weight))+geom_histogram(aes(y
= ..density..),color="black",fill="white")
+geom_density(alpha=.2,fill="#FF6666")
OUTPUT:
> ggplot(df1,aes(x=weight))+geom_histogram()
`stat_bin()` using `bins = 30`. Pick better value with
`binwidth`.
> ggplot(df1,aes(x=weight))+geom_histogram(binwidth=2)
CODE:
###BIVARIATE PLOT
OUTPUT:
CODE:
###BIVARIATE PLOT
OUTPUT:
(B)WRITE PROGRAMS TO DEMONSTRATE PLOTTING CATEGORICAL AND TIME-SERIES DATA.
CODE:
###Time Series
#R Time series Analysis
#getting the data points in form of a R vector.
dev.off()
OUTPUT:
> snowfall
[1] 790.0 1170.8 860.1 1330.6 630.4 911.5 683.5 996.6
783.2 982.0 881.8 1021.0
> print(snowfall_timeseries)
Jan Feb Mar Apr May Jun Jul Aug
Sep Oct Nov Dec
2013 790.0 1170.8 860.1 1330.6 630.4 911.5 683.5 996.6
783.2 982.0 881.8 1021.0
> dev.off()
RStudioGD
2
EXPERIMENT-8
#BINOMIAL DISTRIBUTION
#dbinom
k=0:100
prob=dbinom(x=k,size=100,prob=0.5)
#dbinom
dbinom(2,size=8,prob=0.7)
#pbinom
pbinom(2,size=8,prob=0.7)
#or equivalently
pbinom(2,size=8,0.7,lower.tail = TRUE)
#qbinom
qbinom(0.6,size=8,prob=0.7)
#or equivalently
qbinom(0.6,8,0.7,lower.tail = TRUE)
#pbinom
1-pbinom(1,size=8,prob=0.7)
#or equivalently
pbinom(1,size=8,prob=0.7,lower.tail = FALSE)
#rbinom
rbinom(n=5,size=8,prob=0.7)
#pmfbin
n=100
p=0.7
k=0:n
pmfbin=dbinom(k,n,p)
plot(k,pmfbin)
OUTPUT
> #BINOMIAL
> prob=dbinom(x=k,size=100,prob=0.5)
> #dbinom
> dbinom(2,size=8,prob=0.7)
[1] 0.01000188
> #pbinom
> pbinom(2,size=8,prob=0.7)
[1] 0.01129221
[1] 0.01129221
> #qbinom
> qbinom(0.6,size=8,prob=0.7)
[1] 6
[1] 6
> #pbinom
> 1-pbinom(1,size=8,prob=0.7)
[1] 0.9987097
[1] 0.9987097
> #rbinom
> rbinom(n=5,size=8,prob=0.7)
[1] 4 7 4 8 6
> #pmfbin
> n=100
> p=0.7
> k=0:n
> pmfbin=dbinom(k,n,p)
> plot(k,pmfbin)
> x=rbinom(5,8,0.7)
> mean(x)
[1] 5.2
> var(x)
[1] 1.7
#POISSON DISTRIBUTION
#dpois
k=0:100
prob=dpois(x=k,lambda=4)
dpois(2,lambda=8)
#ppois
ppois(2,lambda=8)
#or equivalently
#qpois
qpois(0.6,lambda=8)
#or equivalently
qpois(0.6,8,lower.tail = TRUE)
#ppois
1-ppois(1,lambda=8)
#or equivalently
ppois(1,lambda=8,lower.tail = FALSE)
#rpois
rpois(5,lambda=3)
#pmfpois
n=100
lambda=4
k=0:n
pmfpois=dpois(k,lambda)plot(k,pmfpois,type="h")
x=rpois(n=100,lambda=4)
mean(x)
var(x)
OUTPUT:
> #BINOMIAL
> k=0:100
> prob=dpois(x=k,lambda=4)
> dpois(2,lambda=8)
[1] 0.0107348
> #ppois
> ppois(2,lambda=8)
[1] 0.01375397
[1] 0.01375397
> #qpois
> qpois(0.6,lambda=8)
[1] 9
[1] 9
> #ppois
> 1-ppois(1,lambda=8)
[1] 0.9969808
[1] 0.9969808
> #rpois
> rpois(5,lambda=3)
[1] 3 4 3 4 4
> #pmfpois
> n=100
> lambda=4
> k=0:n
> pmfpois=dpois(k,lambda)
> plot(k,pmfpois,type="h")
> x=rpois(n=100,lambda=4)
> mean(x)
[1] 4.06
> var(x)
[1] 4.824646
#NORMALDISTRIBUTION
#dnorm
dnorm(2,mean=8,sd=5)
#pnorm
pnorm(18,mean=20,sd=sqrt(4))
#or equivalently
pnorm(18,mean=20,sd=sqrt(4),lower.tail = TRUE)
#qnorm
qnorm(0.9,mean=20,sd=sqrt(4))
#or equivalently
qnorm(0.9,mean=20,sd=sqrt(4),lower.tail = TRUE)
#ppois
1-pnorm(q=22,mean=20,sd=sqrt(4))
#or equivalently
pnorm(22,mean=20,sd=sqrt(4),lower.tail = FALSE)
#rpois
rnorm(10,mean=20,sd=sqrt(4))
mean(x)
var(x)
OUTPUT:
#NORMAL DISTRIBUTION
> #dnorm
> dnorm(2,mean=8,sd=5)
[1] 0.03883721
> #pnorm
> pnorm(18,mean=20,sd=sqrt(4))
[1] 0.1586553
[1] 0.1586553
> #qnorm
> qnorm(0.9,mean=20,sd=sqrt(4))
[1] 22.5631
[1] 22.5631
> #pnorm
> 1-pnorm(q=22,mean=20,sd=sqrt(4))
[1] 0.1586553
[1] 0.1586553
> #rnorm
> rnorm(10,mean=20,sd=sqrt(4))
[1] 21.87323 22.34675 18.93756 18.81159 21.95431 21.97595 22.29326 22.23453 15.83450
21.09639
> mean(x)
[1] 4.06
> var(x)
[1] 4.824646
EXPERIMENT-9
PROBLEM STATEMENT: BUILDING CONFIDENCE IN CONFIDENCE INTERVALS.
a)Population versus Samples
b)Large Sample Confidence Intervals
c)Simulating Data Sets
d)Evaluating the Coverage of Confidence Intervals
CODE:
#Set the seed of R's random number generator ,which is useful for creating a number
#set.seed(5)
#create a matrix object
ncols=4
nrows=3
population<-matrix(runif(ncols*nrows),ncol = ncols)
population
#print the population
print.listof(list(population))
#print the first row population
first_row<-population[1,]
print(first_row)
p=0.56
q=0.44
s=(p-q)
s
se=sqrt(s)
se
ser=se/10
ser
rand_poissons<-rpois(n=10,lambda=1.5)
print(rand_poissons)
#binorm
coin_flips<-rbinom(n=1,size = 1000,prob = 0.5)
print(coin_flips)
coin_flips<-rbinom(n=4,size = 1000,prob = 0.5)
print(coin_flips)
#95%C.I
sample<-rnorm(32,mean = 2.5,sd=9)
m<-mean(sample)
m
summary(sample)
s<-sd(sample)
ci.l=m-1.96*(s/sqrt(32))
ci.u=m+1.96*(s/sqrt(32))
ci.l
ci.u
#practical
#simulate linear model
x<-1:5
mod1<-lm(c(1:3,8,12)~x)
s1<-simulate(mod1,nsim=4)
s1
confint(mod1,level = 0.95)
confint(mod1,level = 0.99)
#ex2
data(mtcars)
mod2<-(lm(mpg~1,mtcars))
set.seed(30)
x<-rnorm(100)
e<-rnorm(100,2,5)
y<-0.5+(5*x)+e
plot(x,y)
summary(y)
#repeat the simulation
Random.seed<-attr(s1,"seed")
identical(s1,simulate(mod1,nsim = 4))
#coverage probability
set.seed(50)
dab=rbinom(n=50,size=1,prob=0.2)
dabb=rbinom(n=50,size=2,prob=0.2)
dab
sum(dab)
length(dab)
##calculate the estimation of mean
x_hat=mean(dab)
##cal S.E of the estimate
se=sqrt((x_hat*(1-x_hat)/n))
##obtain critical values of CI
zz=qnorm(p=0.05,lower.tail=F)
zz
l=x_hat-zz*se
l
u=x_hat+zz*se
u
cl=c(lower=1,upper=u)
cl
##90% CI COVERAGE,SMALL n formula
##x_hat~approxN(x,x(1-x)/n)
set.seed(50)
cover=rep(0,1000)
for(i in seq_along(1:1000))
{
#generate data,n=8,p=0.58
c(coverage=mean(cover))
###
pchisq(25,15,lower.tail=T)
}
OUTPUT:
> population
[,1] [,2] [,3] [,4]
[1,] 0.4225944 0.9010428 0.2877793 0.7520915
[2,] 0.5642985 0.3186195 0.2586554 0.1496048
[3,] 0.4762914 0.2215889 0.6680279 0.8455009
> print.listof(list(population))
Component 1 :
[,1] [,2] [,3] [,4]
[1,] 0.4225944 0.9010428 0.2877793 0.7520915
[2,] 0.5642985 0.3186195 0.2586554 0.1496048
[3,] 0.4762914 0.2215889 0.6680279 0.8455009
> print(first_row)
[1] 0.4225944 0.9010428 0.2877793 0.7520915
> print(first_col)
[1] 0.4225944 0.5642985 0.4762914
> random_sam
[1] 0.2877793 0.3186195 0.6680279 0.4225944
> s
[1] 0.12
> se
[1] 0.3464102
> ser=se/10
> ser
[1] 0.03464102
> low
[1] 0.4921036
> high
[1] 0.6278964
> low
[1] 0.02364509
> high
[1] 0.2963549
> print(rand_poissons)
[1] 2 3 0 1 2 1 2 2 5 2
> print(coin_flips)
[1] 488
> print(coin_flips)
[1] 501 515 513 460
> m
[1] 0.5037408
> summary(sample)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-19.1934 -6.0440 -0.4835 0.5037 8.2114 20.2858
> ci.l
[1] -2.893567
> ci.u
[1] 3.901049
> s1
sim_1 sim_2 sim_3 sim_4
1 -1.224885 -1.226925 3.535729 0.07006479
2 1.861009 1.036812 1.851196 -0.58824658
3 4.165784 6.448161 4.767999 3.00401090
4 5.341031 9.217115 10.930663 7.38188023
5 11.652820 7.671960 11.694232 11.53120722
> confint(mod1,level = 0.95)
2.5 % 97.5 %
(Intercept) -8.785171 2.385171
x 1.116008 4.483992
> confint(mod1,level = 0.99)
0.5 % 99.5 %
(Intercept) -13.4507542 7.050754
x -0.2907187 5.890719
> summary(y)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-16.1556 -4.5298 0.5078 0.5889 6.1553 20.1545
> identical(s1,simulate(mod1,nsim = 4))
[1] FALSE
> dab
[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
[46] 0 1 0 0 1
> sum(dab)
[1] 5
> length(dab)
[1] 50
> zz
[1] 1.644854
> l
[1] 0.05065439
> u
[1] 0.1493456
> cl
lower upper
1.0000000 0.1493456
EXPERIMENT-10
PROBLEM STATEMENT:
How to perform tests of hypotheses about the mean when the variance is known.
How to compute the p-value . Explore the connection between the critical region,
the test statistic, and the p-value
CODE:
#LOWER TAIL TEST OF POPULATION MEAN WITH KNOWN VARIANCE
#Suppose the manufacturer claims that the mean lifetime of a lightbulb is more than 15,000
hours. In a sample of 35 light bulbs,it was found that they only last 9500 hours on
average.Assume the population standard deviation is 125 hours.At .05 significance level,can
we reject the claim by the manufacturer?
mean = 9500 #sample mean
population_mean = 15000 #hypothesised value
standard_deviation = 125 #population standard deviation
n = 35 #sample size
z = (mean-population_mean)/(standard_deviation/sqrt(n)) #test statistic
z
#compute critical value
alpha = 0.05
z.alpha = qnorm(1-alpha)
z.alpha
#the test statistic |z| is greater than |z.alpha|.Hence at 0.05 significance level,we reject the
claim that mean lifetime of a lightbulb is above 15,000 hours.
#ALTERNATIVE SOLUTION USING P-Value.
pval = pnorm(z)
pval
#pval is less than 0.05.Hence at 0.05 significance level,we reject the claim that mean lifetime
of a lightbulb is above 15,000 hours.
#UPPER TAIL TEST OF POPULATION MEAN WITH KNOWN VARIANCE.
#Suppose the food label on a cookie bag states that there is at most 5 grams of saturated fat
in a single cookie.In a sample of 40 cookies,it is found that the mean amount of saturated fat
per cookie is 3.2 grams.Assume that the population standard deviation is 0.25 grams.At 0.05
significance level,can we reject the claim on food label?
sample_mean = 3.2 #sample mean
mu0 = 3 #hypothesized value
standard_deviation = 0.25 #population standard deviation
n = 40
z = (sample_mean-mu0)/(standard_deviation/sqrt(n))
z #test statistic
#Compute critical value
alpha = .05
z.alpha = qnorm(1-alpha)
#Suppose the mean weight of king penguins found in an antarctic colony last year was 13
kg.In a sample of 35 penguins same time this year in the same colony,the mean penguin
weight is 15 kg.Assume the population standard deviation is 3 kg.At .05 significance
level,can we reject the null hypothesis that the mean penguin weight does not differ from
last year?
sample_mean = 14.3 #sample mean
n = 35 #sample size
z = (sample_mean-mu0)/(standard_deviation/sqrt(n))
z
#compute critical value.
alpha = 0.05
z.half.alpha = qnorm(1-alpha/2)
c(-z.half.alpha,z.half.alpha)
#the test statistic -1.380419 lies between the critical values -1.959964 and 1.9599654.Hence
at 0.05 significance level,we do not reject the null hypothesis that the mean penguin weight
does not differ from last year.
#USING P-VALUE
pval = 2*pnorm(z)
pval
#pval is greater than 0.05 ,hence H0 is accepted.
OUTPUT:
> z
[1] -260.3075
> z.alpha
[1] 1.644854
> pval
[1] 0
> z #test statistic
[1] 5.059644
> z.alpha #critical value
[1] 1.644854
> pval
[1] 0.9999998
> z
[1] -1.380419
> z.half.alpha = qnorm(1-alpha/2)
> c(-z.half.alpha,z.half.alpha)
[1] -1.959964 1.959964
> pval = 2*pnorm(z)
> pval
[1] 0.1674578
EXPERIMENT-11
REGRESSION
Wrie a program to demonstrate line regression in R for the given dataset by
following the below steps
>Reading and understanding the data
>Hypothesis testing in linear Regression
>Building a linear mode
>Residual analysis and predictions
CODE:
x = c(150,130,138,120,110,117,113,116,126,134,167,198,125,114,115,196)
y = c(180,190,116,153,123,110,113,145,163,142,143,110,178,128,146,197)
plot(x,y)
o/p:-
lm(y~x)
o/p:-
Call:
lm(formula = y ~ x)
Coefficients:
(Intercept) x
115.3781 0.2263
abline(lm(y~x),col ="red")
o/p:-
coefficients(lm(y~x))
o/p:-
coefficients(lm(y~x))
(Intercept) x
115.378062 0.226349
>
fitted.values(lm(y~x))
o/p:-
> fitted.values(lm(y~x))
1 2 3 4 5 6 7 8 9 10 11 12
149.3304 144.8034 146.6142 142.5399 140.2765 141.8609 140.9555 141.6345
143.8980 145.7088 153.1783 160.1952
13 14 15 16
143.6717 141.1818 141.4082 159.7425
residuals(lm(y~x))
o/p:-
residuals(lm(y~x))
1 2 3 4 5 6 7 8 9 10
30.669586 45.196566 -30.614226 10.460057 -17.276453 -31.860896 -27.955500
3.365453 19.101962 -3.708830
11 12 13 14 15 16
-10.178347 -50.195167 34.328311 -13.181849 4.591802 37.257532
summary(lm(y~x))
summary(lm(y~x))
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-50.195 -19.946 -0.172 21.994 45.197
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 115.3781 36.7930 3.136 0.00729 **
x 0.2263 0.2660 0.851 0.40918
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 29.15 on 14 degrees of freedom
Multiple R-squared: 0.04917, Adjusted R-squared: -0.01875
F-statistic: 0.7239 on 1 and 14 DF, p-value: 0.4092
>
z<-data.frame(x=170)
p<-predict(lm(y~x),z)
print(p)
o/p:-
print(p)
1
153.8574
getwd()
o/p:-
[1] "C:/Users/karis/Documents">
setwd("C:\Users\karis\Downloads\diabetes.csv")f1<-read.csv("diabetes.csv")
plot(f1$Age,f1$Glucose)
o/p:-
lm(f1$Age~f1$Glucose)
o/p:- Call:
lm(formula = f1$Age ~ f1$Glucose)
Coefficients:
(Intercept) f1$Glucose
21.52302 0.09693
abline(lm(f1$Age~f1$Glucose),col ="red")
o/p:-
coefficients(lm(f1$Age~f1$Glucose))
o/p:-
(Intercept) f1$Glucose
21.52301888 0.09692636
>
fitted.values(lm(f1$Age~f1$Glucose))
o/p:-
> fitted.values(lm(f1$Age~f1$Glucose))
1 2 3 4 5 6 7 8 9 10
35.86812 29.76176 39.26054 30.14946 34.80193 32.76648 29.08327 32.66955 40.61751 33.63881
13 14 15 16 17 18 19 20 21 22
34.99578 39.84210 37.61279 31.21565 32.96033 31.89414 31.50643 32.66955 33.73574 31.11873
25 26 27 28 29 30 31 32 33 34
35.38349 33.63881 35.77119 30.92488 35.57734 32.86340 32.08799 36.83738 30.05254 30.44024
37 38 39 40 41 42 43 44 45 46
34.89886 31.40951 30.24639 32.28184 38.96976 34.41422 31.79721 38.09743 36.93431 38.96976
49 50 51 52 53 54 55 56 57 58
31.50643 31.70029 31.50643 31.31258 30.05254 38.58206 36.06197 28.59864 39.64825 31.21565
61 62 63 64 65 66 67 68 69 70
29.66483 34.41422 25.78778 35.18964 32.57262 31.11873 32.08799 32.08799 30.73102 35.67427
73 74 75 76 77 78 79 80 81 82
33.73574 34.02652 29.18020 21.52302 27.53245 30.73102 34.22037 32.37877 32.47570 28.69557
85 86 87 88 89 90 91 92 93 94
34.80193 32.18492 31.79721 31.21565 34.70500 31.89414 29.27713 33.44496 29.37405 34.51115
97 98 99 100 101 102 103 104 105 106
30.44024 28.40479 30.53717 33.34803 37.32202 36.15890 33.63881 29.37405 29.76176 33.73574
109 110 111 112 113 114 115 116 117 118
29.56791 30.73102 38.09743 36.54660 30.14946 28.88942 37.03124 35.67427 33.54189 29.08327
121 122 123 124 125 126 127 128 129 130
37.22509 32.28184 31.89414 34.31730 32.47570 30.05254 33.15418 32.96033 32.86340 31.70029
133 134 135 136 137 138 139 140 141 142
38.00050 29.66483 30.82795 33.63881 31.21565 30.53717 34.02652 31.70029 33.92959 31.79721
145 146 147 148 149 150 151 152 153 154
36.44968 31.40951 27.04782 31.79721 35.77119 30.24639 34.70500 32.57262 36.64353 36.35275
157 158 159 160 161 162 163 164 165 166
31.11873 32.08799 30.05254 37.32202 36.15890 31.40951 32.57262 31.21565 34.22037 31.60336
169 170 171 172 173 174 175 176 177 178
32.18492 32.28184 31.40951 34.51115 29.95561 29.18020 28.79250 38.87284 29.76176 34.02652
181 182 183 184 185 186 187 188 189 190
29.95561 33.05726 21.52302 28.59864 35.18964 40.32673 39.06669 33.92959 32.08799 34.99578
193 194 195 196 197 198 199 200 201 202
36.93431 34.60808 29.76176 36.83738 31.70029 31.89414 32.08799 35.86812 32.47570 34.89886
205 206 207 208 209 210 211 212 213 214
31.50643 32.28184 40.52058 37.22509 30.82795 39.35747 29.37405 35.77119 38.87284 35.09271
217 218 219 220 221 222 223 224 225 226
32.08799 33.63881 29.76176 32.37877 38.67898 36.83738 33.05726 35.28656 31.21565 29.95561
229 230 231 232 233 234 235 236 237 238
40.61751 32.86340 35.28656 34.51115 29.18020 33.34803 28.69557 38.09743 39.06669 38.87284
241 242 243 244 245 246 247 248 249 250
30.34332 30.34332 34.99578 33.05726 35.67427 39.35747 33.34803 37.51587 33.54189 32.28184
253 254 255 256 257 258 259 260 261 262
30.24639 29.85869 30.44024 32.47570 32.28184 32.57262 40.22981 36.54660 40.03595 35.18964
265 266 267 268 269 270 271 272 273 274
33.44496 30.82795 34.89886 33.92959 31.40951 35.67427 31.31258 31.99107 33.34803 28.40479
277 278 279 280 281 282 283 284 285 286
31.79721 31.60336 32.57262 31.99107 35.67427 34.02652 34.41422 37.12816 31.99107 34.70500
289 290 291 292 293 294 295 296 297 298
30.82795 31.99107 29.08327 31.89414 33.92959 33.92959 37.12816 36.15890 35.67427 33.73574
301 302 303 304 305 306 307 308 309 310
37.70972 35.48041 28.98635 32.66955 36.06197 33.15418 37.12816 34.80193 33.92959 33.54189
313 314 315 316 317 318 319 320 321 322
36.54660 32.47570 32.08799 32.37877 31.11873 39.16362 32.66955 40.32673 34.02652 32.37877
325 326 327 328 329 330 331 332 333 334
32.37877 36.74046 33.34803 38.87284 31.40951 31.70029 32.96033 29.95561 38.96976 31.79721
337 338 339 340 341 342 343 344 345 346
32.86340 32.66955 36.25583 38.77591 34.12345 30.73102 21.52302 33.34803 30.73102 33.73574
349 350 351 352 353 354 355 356 357 358
31.11873 21.52302 30.44024 34.80193 27.43553 30.24639 30.24639 37.51587 33.63881 34.02652
361 362 363 364 365 366 367 368 369 370
39.84210 36.83738 31.50643 35.67427 35.77119 31.11873 33.54189 31.31258 29.37405 34.41422
373 374 375 376 377 378 379 380 381 382
29.66483 31.70029 33.34803 35.09271 31.02180 29.95561 36.64353 30.53717 31.89414 31.70029
385 386 387 388 389 390 391 392 393 394
33.63881 33.05726 32.76648 31.70029 35.48041 31.21565 31.21565 37.61279 34.22037 32.76648
397 398 399 400 401 402 403 404 405 406
30.82795 34.22037 29.47098 40.22981 30.73102 34.80193 34.70500 28.50172 37.80665 33.44496
409 410 411 412 413 414 415 416 417 418
40.61751 38.19435 31.40951 32.37877 35.38349 35.38349 34.89886 38.29128 30.92488 35.48041
421 422 423 424 425 426 427 428 429 430
33.05726 30.63410 31.40951 32.66955 36.15890 39.35747 30.63410 39.06669 34.60808 30.73102
433 434 435 436 437 438 439 440 441 442
29.27713 34.99578 30.24639 35.18964 35.09271 35.77119 30.92488 31.89414 39.84210 29.56791
445 446 447 448 449 450 451 452 453 454
32.86340 38.96976 31.21565 30.73102 31.60336 33.15418 29.47098 34.51115 30.34332 33.05726
457 458 459 460 461 462 463 464 465 466
34.60808 29.85869 35.86812 34.51115 33.15418 28.40479 28.69557 30.05254 32.66955 33.54189
469 470 471 472 473 474 475 476 477 478
33.15418 36.44968 35.48041 34.80193 33.05726 34.70500 32.57262 34.80193 31.70029 32.57262
481 482 483 484 485 486 487 488 489 490
36.83738 33.44496 29.76176 29.66483 35.57734 34.60808 34.99578 38.29128 31.11873 40.32673
493 494 495 496 497 498 499 500 501 502
31.11873 33.63881 29.27713 37.61279 32.18492 29.37405 40.42366 36.44968 32.86340 29.66483
505 506 507 508 509 510 511 512 513 514
30.82795 28.79250 38.96976 34.12345 29.66483 33.15418 29.66483 34.99578 30.34332 30.34332
517 518 519 520 521 522 523 524 525 526
35.57734 33.63881 28.88942 34.02652 28.11401 33.54189 32.57262 34.12345 33.63881 29.95561
529 530 531 532 533 534 535 536 537 538
32.86340 32.28184 33.34803 31.89414 29.85869 30.34332 28.98635 34.31730 31.70029 27.04782
541 542 543 544 545 546 547 548 549 550
31.21565 33.92959 30.24639 29.66483 30.05254 39.55132 39.64825 34.22037 37.41894 39.84210
553 554 555 556 557 558 559 560 561 562
32.57262 30.05254 29.66483 33.54189 30.92488 32.18492 31.50643 29.76176 33.63881 40.71444
565 566 567 568 569 570 571 572 573 574
30.34332 30.73102 31.11873 30.44024 36.44968 33.25111 29.08327 34.12345 32.28184 31.02180
577 578 579 580 581 582 583 584 585 586
31.99107 32.96033 34.41422 40.61751 36.15890 32.08799 33.25111 31.21565 33.54189 30.53717
589 590 591 592 593 594 595 596 597 598
38.58206 28.59864 32.28184 32.37877 34.31730 29.47098 33.44496 39.74517 28.01708 30.14946
601 602 603 604 605 606 607 608 609 610
31.99107 30.82795 33.54189 36.06197 39.26054 33.54189 39.06669 30.44024 36.25583 32.28184
613 614 615 616 617 618 619 620 621 622
37.80665 31.70029 34.89886 31.79721 32.86340 28.11401 32.37877 33.05726 32.37877 30.44024
625 626 627 628 629 630 631 632 633 634
31.99107 30.24639 33.63881 34.31730 33.92959 30.63410 32.57262 31.40951 32.28184 33.92959
637 638 639 640 641 642 643 644 645 646
31.60336 30.63410 30.92488 31.21565 31.40951 33.92959 35.77119 30.24639 31.50643 36.74046
649 650 651 652 653 654 655 656 657 658
34.70500 31.89414 30.34332 32.86340 33.44496 33.15418 31.79721 36.54660 31.31258 33.15418
661 662 663 664 665 666 667 668 669 670
37.22509 40.81136 37.70972 35.57734 32.66955 32.37877 35.57734 32.28184 31.02180 36.44968
673 674 675 676 677 678 679 680 681 682
28.11401 33.44496 30.34332 40.42366 36.64353 30.53717 33.25111 31.31258 26.95089 37.22509
685 686 687 688 689 690 691 692 693 694
34.70500 34.02652 34.12345 31.89414 35.09271 35.48041 31.89414 36.83738 33.25111 34.02652
697 698 699 700 701 702 703 704 705 706
37.90357 31.11873 33.83267 32.96033 33.34803 33.63881 37.80665 34.02652 32.18492 29.27713
709 710 711 712 713 714 715 716 717 718
37.41894 30.53717 36.83738 33.73574 34.02652 34.51115 31.40951 39.64825 38.29128 30.63410
721 722 723 724 725 726 727 728 729 730
29.56791 32.57262 35.96505 32.86340 32.28184 32.37877 32.76648 35.18964 38.48513 30.44024
733 734 735 736 737 738 739 740 741 742
38.38821 31.79721 31.70029 30.73102 33.73574 27.82323 31.11873 31.40951 33.15418 31.40951
745 746 747 748 749 750 751 752 753 754
36.35275 31.21565 35.77119 29.37405 39.64825 37.22509 34.70500 33.25111 31.99107 39.06669
757 758 759 760 761 762 763 764 765 766
34.80193 33.44496 31.79721 39.93903 30.05254 38.00050 30.14946 31.31258 33.34803 33.25111
residuals(lm(f1$Age~f1$Glucose))
o/p:-
> fitted.values(lm(f1$Age~f1$Glucose))
1 2 3 4 5 6 7 8 9 10
35.86812 29.76176 39.26054 30.14946 34.80193 32.76648 29.08327 32.66955 40.61751 33.63881
13 14 15 16 17 18 19 20 21 22
34.99578 39.84210 37.61279 31.21565 32.96033 31.89414 31.50643 32.66955 33.73574 31.11873
25 26 27 28 29 30 31 32 33 34
35.38349 33.63881 35.77119 30.92488 35.57734 32.86340 32.08799 36.83738 30.05254 30.44024
37 38 39 40 41 42 43 44 45 46
34.89886 31.40951 30.24639 32.28184 38.96976 34.41422 31.79721 38.09743 36.93431 38.96976
49 50 51 52 53 54 55 56 57 58
31.50643 31.70029 31.50643 31.31258 30.05254 38.58206 36.06197 28.59864 39.64825 31.21565
61 62 63 64 65 66 67 68 69 70
29.66483 34.41422 25.78778 35.18964 32.57262 31.11873 32.08799 32.08799 30.73102 35.67427
73 74 75 76 77 78 79 80 81 82
33.73574 34.02652 29.18020 21.52302 27.53245 30.73102 34.22037 32.37877 32.47570 28.69557
85 86 87 88 89 90 91 92 93 94
34.80193 32.18492 31.79721 31.21565 34.70500 31.89414 29.27713 33.44496 29.37405 34.51115
97 98 99 100 101 102 103 104 105 106
30.44024 28.40479 30.53717 33.34803 37.32202 36.15890 33.63881 29.37405 29.76176 33.73574
109 110 111 112 113 114 115 116 117 118
29.56791 30.73102 38.09743 36.54660 30.14946 28.88942 37.03124 35.67427 33.54189 29.08327
121 122 123 124 125 126 127 128 129 130
37.22509 32.28184 31.89414 34.31730 32.47570 30.05254 33.15418 32.96033 32.86340 31.70029
133 134 135 136 137 138 139 140 141 142
38.00050 29.66483 30.82795 33.63881 31.21565 30.53717 34.02652 31.70029 33.92959 31.79721
145 146 147 148 149 150 151 152 153 154
36.44968 31.40951 27.04782 31.79721 35.77119 30.24639 34.70500 32.57262 36.64353 36.35275
157 158 159 160 161 162 163 164 165 166
31.11873 32.08799 30.05254 37.32202 36.15890 31.40951 32.57262 31.21565 34.22037 31.60336
169 170 171 172 173 174 175 176 177 178
32.18492 32.28184 31.40951 34.51115 29.95561 29.18020 28.79250 38.87284 29.76176 34.02652
181 182 183 184 185 186 187 188 189 190
29.95561 33.05726 21.52302 28.59864 35.18964 40.32673 39.06669 33.92959 32.08799 34.99578
193 194 195 196 197 198 199 200 201 202
36.93431 34.60808 29.76176 36.83738 31.70029 31.89414 32.08799 35.86812 32.47570 34.89886
205 206 207 208 209 210 211 212 213 214
31.50643 32.28184 40.52058 37.22509 30.82795 39.35747 29.37405 35.77119 38.87284 35.09271
217 218 219 220 221 222 223 224 225 226
32.08799 33.63881 29.76176 32.37877 38.67898 36.83738 33.05726 35.28656 31.21565 29.95561
229 230 231 232 233 234 235 236 237 238
40.61751 32.86340 35.28656 34.51115 29.18020 33.34803 28.69557 38.09743 39.06669 38.87284
241 242 243 244 245 246 247 248 249 250
30.34332 30.34332 34.99578 33.05726 35.67427 39.35747 33.34803 37.51587 33.54189 32.28184
253 254 255 256 257 258 259 260 261 262
30.24639 29.85869 30.44024 32.47570 32.28184 32.57262 40.22981 36.54660 40.03595 35.18964
265 266 267 268 269 270 271 272 273 274
33.44496 30.82795 34.89886 33.92959 31.40951 35.67427 31.31258 31.99107 33.34803 28.40479
277 278 279 280 281 282 283 284 285 286
31.79721 31.60336 32.57262 31.99107 35.67427 34.02652 34.41422 37.12816 31.99107 34.70500
289 290 291 292 293 294 295 296 297 298
30.82795 31.99107 29.08327 31.89414 33.92959 33.92959 37.12816 36.15890 35.67427 33.73574
301 302 303 304 305 306 307 308 309 310
37.70972 35.48041 28.98635 32.66955 36.06197 33.15418 37.12816 34.80193 33.92959 33.54189
313 314 315 316 317 318 319 320 321 322
36.54660 32.47570 32.08799 32.37877 31.11873 39.16362 32.66955 40.32673 34.02652 32.37877
325 326 327 328 329 330 331 332 333 334
32.37877 36.74046 33.34803 38.87284 31.40951 31.70029 32.96033 29.95561 38.96976 31.79721
337 338 339 340 341 342 343 344 345 346
32.86340 32.66955 36.25583 38.77591 34.12345 30.73102 21.52302 33.34803 30.73102 33.73574
349 350 351 352 353 354 355 356 357 358
31.11873 21.52302 30.44024 34.80193 27.43553 30.24639 30.24639 37.51587 33.63881 34.02652
361 362 363 364 365 366 367 368 369 370
39.84210 36.83738 31.50643 35.67427 35.77119 31.11873 33.54189 31.31258 29.37405 34.41422
373 374 375 376 377 378 379 380 381 382
29.66483 31.70029 33.34803 35.09271 31.02180 29.95561 36.64353 30.53717 31.89414 31.70029
385 386 387 388 389 390 391 392 393 394
33.63881 33.05726 32.76648 31.70029 35.48041 31.21565 31.21565 37.61279 34.22037 32.76648
397 398 399 400 401 402 403 404 405 406
30.82795 34.22037 29.47098 40.22981 30.73102 34.80193 34.70500 28.50172 37.80665 33.44496
409 410 411 412 413 414 415 416 417 418
40.61751 38.19435 31.40951 32.37877 35.38349 35.38349 34.89886 38.29128 30.92488 35.48041
421 422 423 424 425 426 427 428 429 430
33.05726 30.63410 31.40951 32.66955 36.15890 39.35747 30.63410 39.06669 34.60808 30.73102
433 434 435 436 437 438 439 440 441 442
29.27713 34.99578 30.24639 35.18964 35.09271 35.77119 30.92488 31.89414 39.84210 29.56791
445 446 447 448 449 450 451 452 453 454
32.86340 38.96976 31.21565 30.73102 31.60336 33.15418 29.47098 34.51115 30.34332 33.05726
457 458 459 460 461 462 463 464 465 466
34.60808 29.85869 35.86812 34.51115 33.15418 28.40479 28.69557 30.05254 32.66955 33.54189
469 470 471 472 473 474 475 476 477 478
33.15418 36.44968 35.48041 34.80193 33.05726 34.70500 32.57262 34.80193 31.70029 32.57262
481 482 483 484 485 486 487 488 489 490
36.83738 33.44496 29.76176 29.66483 35.57734 34.60808 34.99578 38.29128 31.11873 40.32673
493 494 495 496 497 498 499 500 501 502
31.11873 33.63881 29.27713 37.61279 32.18492 29.37405 40.42366 36.44968 32.86340 29.66483
505 506 507 508 509 510 511 512 513 514
30.82795 28.79250 38.96976 34.12345 29.66483 33.15418 29.66483 34.99578 30.34332 30.34332
517 518 519 520 521 522 523 524 525 526
35.57734 33.63881 28.88942 34.02652 28.11401 33.54189 32.57262 34.12345 33.63881 29.95561
529 530 531 532 533 534 535 536 537 538
32.86340 32.28184 33.34803 31.89414 29.85869 30.34332 28.98635 34.31730 31.70029 27.04782
541 542 543 544 545 546 547 548 549 550
31.21565 33.92959 30.24639 29.66483 30.05254 39.55132 39.64825 34.22037 37.41894 39.84210
553 554 555 556 557 558 559 560 561 562
32.57262 30.05254 29.66483 33.54189 30.92488 32.18492 31.50643 29.76176 33.63881 40.71444
565 566 567 568 569 570 571 572 573 574
30.34332 30.73102 31.11873 30.44024 36.44968 33.25111 29.08327 34.12345 32.28184 31.02180
577 578 579 580 581 582 583 584 585 586
31.99107 32.96033 34.41422 40.61751 36.15890 32.08799 33.25111 31.21565 33.54189 30.53717
589 590 591 592 593 594 595 596 597 598
38.58206 28.59864 32.28184 32.37877 34.31730 29.47098 33.44496 39.74517 28.01708 30.14946
601 602 603 604 605 606 607 608 609 610
31.99107 30.82795 33.54189 36.06197 39.26054 33.54189 39.06669 30.44024 36.25583 32.28184
613 614 615 616 617 618 619 620 621 622
37.80665 31.70029 34.89886 31.79721 32.86340 28.11401 32.37877 33.05726 32.37877 30.44024
625 626 627 628 629 630 631 632 633 634
31.99107 30.24639 33.63881 34.31730 33.92959 30.63410 32.57262 31.40951 32.28184 33.92959
637 638 639 640 641 642 643 644 645 646
31.60336 30.63410 30.92488 31.21565 31.40951 33.92959 35.77119 30.24639 31.50643 36.74046
649 650 651 652 653 654 655 656 657 658
34.70500 31.89414 30.34332 32.86340 33.44496 33.15418 31.79721 36.54660 31.31258 33.15418
661 662 663 664 665 666 667 668 669 670
37.22509 40.81136 37.70972 35.57734 32.66955 32.37877 35.57734 32.28184 31.02180 36.44968
673 674 675 676 677 678 679 680 681 682
28.11401 33.44496 30.34332 40.42366 36.64353 30.53717 33.25111 31.31258 26.95089 37.22509
685 686 687 688 689 690 691 692 693 694
34.70500 34.02652 34.12345 31.89414 35.09271 35.48041 31.89414 36.83738 33.25111 34.02652
697 698 699 700 701 702 703 704 705 706
37.90357 31.11873 33.83267 32.96033 33.34803 33.63881 37.80665 34.02652 32.18492 29.27713
709 710 711 712 713 714 715 716 717 718
37.41894 30.53717 36.83738 33.73574 34.02652 34.51115 31.40951 39.64825 38.29128 30.63410
721 722 723 724 725 726 727 728 729 730
29.56791 32.57262 35.96505 32.86340 32.28184 32.37877 32.76648 35.18964 38.48513 30.44024
733 734 735 736 737 738 739 740 741 742
38.38821 31.79721 31.70029 30.73102 33.73574 27.82323 31.11873 31.40951 33.15418 31.40951
745 746 747 748 749 750 751 752 753 754
36.35275 31.21565 35.77119 29.37405 39.64825 37.22509 34.70500 33.25111 31.99107 39.06669
757 758 759 760 761 762 763 764 765 766
34.80193 33.44496 31.79721 39.93903 30.05254 38.00050 30.14946 31.31258 33.34803 33.25111
summary(lm(f1$Age~f1$Glucose))
o/p:-
Call:
lm(formula = f1$Age ~ f1$Glucose)
Residuals:
Min 1Q Median 3Q Max
-18.811 -8.379 -3.542 6.661 46.489
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 21.52302 1.60311 13.43 < 2e-16 ***
f1$Glucose 0.09693 0.01282 7.56 1.15e-13 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 11.35 on 766 degrees of freedom
Multiple R-squared: 0.06944, Adjusted R-squared: 0.06822
F-statistic: 57.16 on 1 and 766 DF, p-value: 1.15e-13