R language Coding

 R language Coding


# starting...
x=1
y=4
plot(x,y,col="red",pch=19,main="gaurav point",xlab="hello")
plot(dbinom,0,3,col="red",pch=19,main="gaurav point",xlab="hello")
mean(x,y)
mean(x)
mean(y)
clear
rm()
ss=sample(c(1,2,3,4,5,6),size=100,replace=TRUE)
factorial(5)
choose(4,6)
table(ss/length(ss))
table()

#to see the current directory
getwd()

#to set the directory as current working directory
setwd("C:/Users/gaura/OneDrive/Desktop/r language/data")

#read data form csv file
mydata = read.csv("C:/Users/gaura/OneDrive/Desktop/r language/data/marks.csv")

#print data 
print(mydata)

somedata=mydata[1:3]
print(somedata)

#creating and writing data in a file
write.csv(somedata,"somedata.csv")

read.csv("C:/Users/gaura/OneDrive/Desktop/r language/data/somedata.csv")

x=rdunif(100,1,20)

# Defining Function
rdu<-function(n,k){sample(1:k,n,replace=T)}

pdu<-function(x,k){ifelse(x<1,0,ifelse(x<=k,floor(x)/k,1))}

#calling fucntion
pdu(5,6)

rdu(10,5)

#doubt
x1= rdunif(100,1,20)

hist(x1)


x=sample((80:100),100,replace = TRUE)
length(x)
x
hist(x)
table(x)

rand.unif<-runif(1000,min=-2,max=0.8)
hist(rand.unif,freq=FALSE,density=90)
plot(table(x),type="s")

mean(x)
var(x)

barplot(table(x)/length(x),ylim=c(0,0.3))

require(graphics)
library(graphics)
probabilities<-100*dbinom(x=c(0:10,probabilities,type="h"))
dbinom(x=c(0:4), size = 10, prob = 1/3)



r code



Mam code 



#import export all types of files
install.packages("purrr")
library(purrr)
#E:\1 Teaching\GGSIPU\Probability and statistics May 2022
getwd()
setwd("E:/1 Teaching/GGSIPU/Probability and statistics May 2022")
mydata=read.csv("AIML B2 attendence sheet.csv")
setwd("E:/1 Teaching/GGSIPU/Probability and statistics May 2022")
setwd("C:/Users/DELL/OneDrive/Documents")
print(mydata)# print mydata
mydata = read.csv("E:/1 Teaching/GGSIPU/Probability and statistics May 2022/AIML B2 attendence sheet.csv")
read.csv("E:/1 Teaching/GGSIPU/Probability and statistics May 2022/example 2.csv")
outputdata=mydata[2:3]
write.csv(outputdata,"outputdata_example.csv")
write.csv(outputdata,"E:/1 Teaching/GGSIPU/Probability and statistics May 2022/outputdata_example.csv")
#fitting of binomial, poisson
#mean, variance, histogram, pdf, cdf, etc for distribution
#show graphs of all distributions with different sample
#size and actual graph also(take image from net and make
#word ppt for them)
#Discrete Uniform in (1,20)
rdu<-function(n,k) {sample(1:k,n,replace=T)}
pdu<-function(x,k) {ifelse(x<1,0,ifelse(x<=k,floor(x)/k,1))}
rdu(10)

x=rdunif(1000,1,20)
#1 to k, mean=(k+1)/2
hist(x)
length(x[(x==1)])
length(x[(x==4)])
table(x)
rand.unif <- runif(10000, min = -2, max = 0.8)
hist(rand.unif, freq = FALSE, xlab = 'x', density = 20)
plot(table(x)/length(x))
plot(table(x),type="l")
plot(table(x),type="s")
mean(x)#actual mean is k+1/2
var(x)#actual variance is k^2-1/12
barplot(table(x)/length(x),ylim=c(0,.3))
rdunif(10000,1,100)
x=rdunif(10000,1,100)
k=100
(k+1)/2#theoritical_mean
(k^2-1)/12#theoritical_var
mean(x)
var(x)
#Binomial
#P(Y=6)P(Y=6) if Y∼b(n=10,p=0.75)
probabilities <- 100*dbinom(x = c(0:10), size = 10, prob = 1 / 6)
data.frame(x, probabilities)#distribution tableplot(0:10, probabilities, type = "h")
plot(x, probabilities, type = "h")#pmf function


dbinom(x = 0:4, size = 4, prob = 0.75)#The probability of flipping an unfair coin 10 times and seeing 6 heads, if the probability of heads is 0.75
#
probabilities=dbinom(x = 0:10, size = 10, prob = 0.05)

hist(probabilities)
#P(X<=x)>=.1 Quantile function
#dbinom(k, n, p) P(X=k)
dbinom(6,10,0.75)
k=0:10
plot(k,dbinom(k,10,0.75),type="l")
k=8
pbinom(k,10,0.75)#P(X<=k) CDF function
k=c(0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1)
#probability mass function of binomial plot
n=10
x <- 0:n
p=0.6
plot(x, dbinom(x, size = n, prob = p), main = "Probability mass function for Bin(13,0.7)")
#P(X<=x)>=0.5, smallest x
qbinom(0.75, 10, 0.75)#median(50) 25th quantile P(X<=x)=>25/100 minimum x, 25th, 50th, 75th
x=rbinom(1000, 10, 0.75)
#QP(X<=x)>=0.66
qbinom(0.66, 10, 0.75)
cdfprobabilities = dbinom(x = c(0:10), size = 10, prob = 1/6)
data.frame(probabilities)#give all the probabilities
#pbinom(k, n, p) : P(X<=k)
pbinom(3, size = 13, prob = 1/6)
pbinom(13, size = 13, prob = 1/6)
pbinom(3, size = 13, prob = 1 / 6)

plot(0:10, pbinom(0:10, size = 10, prob = 0.8), type = "l")#CDF
#qbinom():This function is used to find the nth quantile, that is if P(x <= k) is given, it finds k.
#qbinom(P, n, p) P(X<=k)=P, K=?
qbinom(0.8419226, size = 13, prob = 1 / 6)
x <- seq(0, 1, by = 0.1)
y <- qbinom(x, size = 13, prob = 1 / 6)
plot(x, y, type = 'l')
#rbinom(n, N, p): generates n random binomial variables
x=rbinom(10000, size = 13, prob = 1 / 6)
hist(rbinom(10000, size = 13, prob = 1 / 6))
print("graph of binomial")
x=rbinom(10000,15,0.5)
 15*0.5#theoritical_mean
 15*0.5*0.5#theoritical_var
mean(x)
var(x)

######################
#Poisson
x=5
lambda=0.5
dpois(x,lambda)#gives the density
ppois(x,lambda)#gives the cdf
qpois(p,lambda)#gives quantile function, which is the smallest
#integer x such that P(X<=x)>=p
rpois(n,lambda)#generates a random poisoon number
k=0:200
prob=ppois(k,lambda=100)#P(X<=k)
plot(x=k,y=prob, main="Poisson Probability Mass Function",type="h")
dpois(x=2,lambda=4)#P(X=2)
dpois(2,4)
#P(2<X<=4)=F(4)-F(2)
ppois(4,4)-ppois(2,4)
n=100
lambda=5
k=0:n
pmfpois=dpois(k,lambda)
plot(k,pmfpois,type="h")
x=rpois(100000,4)
mean(x)
var(x)
###############3
print("Math")
x <- "Probability and statistics"
print(x)
for(i in 1:20) print(1:i)
##################
##geometric: definition in R is slightly different,
#k is the number of failures before a success
#p(x)=p(1-p)^x,x=0,1,2,..
k=0:10
prob=dgeom(x=k,prob=0.6)
#P(X=4) for our defined distribution will be
dgeom(x=4-1,prob=0.6)#p(X=4)
plot(x=k,y=prob, main="Geometric distribution PMF",type="h")

pgeom(x=4-1,prob=0.6)#P(X<=4)
qgeom(p,0.6)#gives x such that P(X<=x+1)>=p
x=rgeom(n=10,p=0.6)
mean(x)
var(x)

Comments

  1. #Binomial Distribution
    n=10
    p=1/6
    #rbinom(kitne chaiye, kaha se kaha tk chaiye, probability)
    x=rbinom(1000, n , p)
    #print(x)
    # table
    y=table(x)
    print(y)


    #prob=table(x)/length(x)
    prob=y/length(x)
    #print(prob)
    plot(prob,type="l")
    # theoritical probabilty
    range=min(x):max(x)
    z=dbinom(range,n,p)
    #print(z)

    #cmf\
    y1=pbinom(range,n,p)

    plot(range,prob,type='l',ylim=0:1)
    lines(range,z,col="red")
    lines(range,y1,col="green")


    p1=0.5
    qbinom(p1,n,p)



    mean(x)

    var(x)


    # to delete graph
    dev.off()






    ####################################################################

    # Poison distribution
    #all the things are same only replace n,p to lam and binom to pois

    lam=5


    xp=rpois(1000,lam)
    print(x)

    yp=table(x)
    print(yp)

    prob=yp/length(x)
    plot(prob,type="l")

    range=min(x):max(x)

    zp=dpois(range,lam)

    plot(range,zp,type="l",col="blue",ylim=0:1)
    lines(range,prob,col="pink")
    z1p=ppois(range,lam)
    lines(range,z1p,col="green")

    print(prob)
    print(z1p)

    mean(x)
    var(x)

    py1=0.5
    qpois(py1,lam)



    ###############################

    # to check that the poison is extension of binomial


    n=100
    p=0.01
    l=1

    u=dbinom(0:10,n,p)
    v=dpois(0:10,l)

    plot(0:10,u,type="l",col="yellow")
    lines(0:10,v,col="red")

    ####################################################################

    # Geometric distribution

    ReplyDelete
  2. https://pastebin.com/PbuiLiCt

    ReplyDelete

Post a Comment

Popular posts from this blog

INTRODUCTION TO BLOGGING

Given and integer N. Calculate the sum of series 13 + 23 + 33 + 43 + … till N-th term using recursion.