You are on page 1of 8

#time series moving avg

#3 yeraly total

data=c(64,55,53,65,80,75,70,80,66,44,45)
moving_total<-function(x,n=3){
filter(x,rep(1,n),sides = 2)
}
moving_avg<-function(x,n=3){
filter(x,rep(1/n,n),sides = 2)
}
moving_total(data)

## Time Series:
## Start = 1
## End = 11
## Frequency = 1
## [1] NA 172 173 198 220 225 225 216 190 155 NA

moving_avg(data)

## Time Series:
## Start = 1
## End = 11
## Frequency = 1
## [1] NA 57.33333 57.66667 66.00000 73.33333 75.00000 75.00000
72.00000
## [9] 63.33333 51.66667 NA

#5 yeraly total

data=c(64,55,53,65,80,75,70,80,66,44,45)
moving_total<-function(x,n=5){
filter(x,rep(1,n),sides = 2)
}
moving_avg<-function(x,n=5){
filter(x,rep(1/n,n),sides = 2)
}
moving_total(data)

## Time Series:
## Start = 1
## End = 11
## Frequency = 1
## [1] NA NA 317 328 343 370 371 335 305 NA NA

moving_avg(data)

## Time Series:
## Start = 1
## End = 11
## Frequency = 1
## [1] NA NA 63.4 65.6 68.6 74.0 74.2 67.0 61.0 NA NA

#4 yearly
#3 yeraly total

data=c(3.6,4.3,4.3,3.4,4.4,5.2,3.8,4.9,5.4)
moving_total<-function(x, n=4){
filter(x,rep(1,n),sides = 2)
}
moving_avg<-function(x,n=4){
filter(x,rep(1/n,n),sides = 2)
}
moving_total(data)

## Time Series:
## Start = 1
## End = 9
## Frequency = 1
## [1] NA 15.6 16.4 17.3 16.8 18.3 19.3 NA NA

data1=moving_avg(data)
centered_4_yearly<-function(x,n=2){
filter(x,rep(1/n,n),sides = 2)
}
centered_4_yearly(data1)

## Time Series:
## Start = 1
## End = 9
## Frequency = 1
## [1] NA 4.0000 4.2125 4.2625 4.3875 4.7000 NA NA NA

#regression
x=c(30,40,50,60,70,80)
y=c(43,45,54,53,56,63)
bxy=lm(x~y)
byx=lm(y~x)

cat("regression eq of y:", round(coef(byx)[1],2),"+",round(coef(byx)


[2],2),"x\n")

## regression eq of y: 31.59 + 0.38 x

cat("regression eq of x:", round(coef(bxy)[1],2),"+",round(coef(bxy)


[2],2),"y\n")

## regression eq of x: -72.3 + 2.43 y


plot(x,y)
abline(byx)

plot(y,x)
abline(bxy)
#rank
x=c(15,12,20,16,18,20,26)
y=c(10,15,11,11,25,18,30)
rank=cor(x,y,method = "spearman")
rank

## [1] 0.5727273

#standar error
a=c(14,15,16,12)
sd=sd(a)
n=4
se=sd/sqrt(n)
se #standard error

## [1] 0.8539126

var(a) #mean square

## [1] 2.916667

#SRSWR
library(gtools)
N=7 #population size
n=2 #sample size
yi=c(11:17) #data

pop_mean=mean(yi)
pop_mean_square=sum((yi-pop_mean)^2)/(N-1)
p=permutations(N,n,yi,repeats.allowed = TRUE)
c=combinations(N,n,yi)
rp=rowMeans(p)
rc=rowMeans(c)
sample_mean_square=sum((p-rp)^2)/(n-1) #sample mean square for with
replacement
sample_mean_square

## [1] 196

sample_mean_square=sum((c-rc)^2)/(n-1) #sample mean square for without


replcemnet
sample_mean_square

## [1] 98

estimator=sample_mean_square/21
# 21 is the smaple size of without replacement and
#pop_mean square is equal to sample mean sqaure
va1=((N-n)/(N*n))*pop_mean_square #with replacemt
va1=((N-1)/(N*n))*pop_mean_square #without replacemt

#straified4
N=20
n=6
yi=c(20,80,50 ,100, 150,70 ,20, 250 ,220,10,50,
140 ,80 ,20 ,50 ,30 ,70 ,90 ,100 ,220)
pop_mean1=mean(yi)
pop_mean_square1=sum((yi-pop_mean1)^2)/(N-1)
var(yi)

## [1] 5062.105

variance=((N-n)/(N*n))*pop_mean_square1
variance

## [1] 590.5789

s1=subset(yi,yi<=50)
s2=subset(yi,yi>50 & yi<=100)
s3=subset(yi,yi>100)
s1_mean=mean(s1)
s1_var=var(s1)
s2_mean=mean(s2)
s2_var=var(s2)
s3_mean=mean(s3)
s3_var=var(s3)
#index no
p0=c(12,14,8,16)

q0=c(20,12,10,15)

p1=c(18,21,12,20)
q1=c(24,16,18,25)

p0q0=p0*q0
p1q1=p1*q1
p0q1=p0*q1
p1q0=p1*q0
IP=(sum(p1q1)/sum(p0q1))*100
IL=(sum(p1q0)/sum(p0q0))*100
ID=((IL+IP)/2)
IF=(sqrt(IL*IP))
IME=(sum(p1q1)+sum(p1q0))/(sum(p0q1)+sum(p0q0))*100
IP

## [1] 140.5303

IL

## [1] 141.7582

ID

## [1] 141.1443

IF

## [1] 141.1429

IME

## [1] 141.0314

#cost of living index


p0=c(40,60,80,90,84)
p1=c(55,75,85,93,90)
w=c(15,20,25,10,30)
I=(p1/p0)*100
Iw=I*w

#estimation of future yr
#trend values
#y=a+bx
# if even then x=2*(yr-md) use this formula for x
#if odd then x=yr=middle yr
yr=(2011:2018) #even
y=c(263,364,563,262,361,265,268,200)
md=mean(yr)
x=2*(yr-md)
x

## [1] -7 -5 -3 -1 1 3 5 7

sy=sum(y)
sx=sum(x)
xy=x*y
x2=x^2
sxy=sum(xy)
sx2=sum(x2)

n=8
#sum(y)=n*a+b*sum(x)
#so sum of x is 0

a=sy/n
a

## [1] 318.25

#sum(x)=a*sum(x)+b*sum(x2)
#b shift hojayega and a haath jayega bec x 0 hai
b=sum(xy)/sum(x2)
b

## [1] -10.21429

tr=a+b*x #trend value


tr

## [1] 389.7500 369.3214 348.8929 328.4643 308.0357 287.6071 267.1786


246.7500

tr2020=a+b*11 #2020
tr2020

## [1] 205.8929

#systematic
library(TeachingSampling)

## Loading required package: dplyr

##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag

## The following objects are masked from 'package:base':


##
## intersect, setdiff, setequal, union

## Loading required package: magrittr

library(dplyr)
P <- c("Mon-8", "Tues-4", "Wed-4", "Thurs-6", "Fri-7","Sat-45","Sun-34","Mon-
21", "Tues-11","Wed-34","Thurs-16","Fri-10","Sat-17","Sun-19")
#systematic sample from a population of 14 with every 2nd included from the
populaion P
systematic_sample <- S.SY(14,2)
systematic_sample

## [,1]
## [1,] 1
## [2,] 3
## [3,] 5
## [4,] 7
## [5,] 9
## [6,] 11
## [7,] 13

P[systematic_sample]

## [1] "Mon-8" "Wed-4" "Fri-7" "Sun-34" "Tues-11" "Thurs-16"


"Sat-17"

You might also like