R tuan 8

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.7 kB
2
Indexable
Never
n = 35;
means = 10;
dlc = 5;
alpha = .05
z = qnorm(1-alpha/2)
z
epsilon = z*(dlc/sqrt(n))
epsilon
left = means - epsilon
left
right = means + epsilon
right

# bai 2
data <- read.csv("data31.csv")
data
attach(data)
profit
ci.mean <- function(x, alpha){
  x_mean = mean(x);
  z = qnorm(1-alpha/2)
  epsil = z*sd(x)/sqrt(length(x))
  print(x_mean - epsil)
  print(x_mean + epsil)
}
alpha = 1 - .95
ci.mean(profit,alpha)
alpha = 1 - .99
ci.mean(profit,alpha)
detach(data)
#Cau 3
#a
data <- read.csv("data32.csv")
data
attach(data)
KHTN
alpha = 1 - .95
ci.mean(KHTN, alpha)
# b
listSv5 = c();
for(i in 1:length(KHTN)){
    if(KHTN[i] > 5) listSv5 = append(listSv5,KHTN[i])
   
} 
listSv5
ci.prop <- function(f, n, alpha){
  p.hat = f/n;
  epsilon = qnorm(1-alpha/2)*sqrt(p.hat*(1-p.hat)/n)
  left <- p.hat - epsilon
  right <- p.hat + epsilon
  print(left)
  print(right)
}
alpha = 1 - .9
ci.prop(length(listSv5), length(KHTN),alpha )
alpha = 1 - .95
ci.prop(length(listSv5), length(KHTN),alpha )
alpha = 1 - .99
ci.prop(length(listSv5), length(KHTN),alpha )

# Cau 4
list.height = c(1.3,1.5,1.7,1.9,2.1)
list.num = c(6,34,31,42,12)
alpha  = 1 - .95
vectors = rep(list.height , list.num)
vectors
ci.mean(vectors, alpha)
list17 = c();
for(i in 1:length(vectors)){
  if(vectors[i] > 1.7) list17 = append(list17,vectors[i]) 
} 
list17
ci.prop(length(list17),length(vectors),alpha)

# Cau 5
ktc.tb <- function(x.mean, n ,sigma , s , alpha){
  z = qnorm(1-alpha/2)
  if(is.null(sigma) && n>= 30){
    epsilon = z*s/sqrt(n)
    print(x.mean - epsilon)
    print(x.mean + epsilon)
  } else if ( is.null(sigma) && n < 30){
    epsilon = qt(1-alpha/2, df = n-1)*s/sqrt(n);
    print(x.mean - epsilon)
    print(x.mean + epsilon)
  } else{
    epsilon = z*sigma/sqrt(n)
    print(x.mean - epsilon)
    print(x.mean + epsilon)
  }
}
ktc.tb(17, 34, NULL, 3.4 , .005)

# Cau 6
ktc.tb.maus <- function(x_vector,sigma, alpha){
  x.mean = mean(x_vector)
  s = sd(x_vector)
  z = qnorm(1-alpha/2)
  n = length(x_vector)
  if(is.null(sigma) && n>= 30){
    epsilon = z*s/sqrt(n)
    print(x.mean - epsilon)
    print(x.mean + epsilon)
  } else if ( is.null(sigma) && n < 30){
    epsilon = qt(1-alpha/2, df = n-1)*s/sqrt(n);
    print(x.mean - epsilon)
    print(x.mean + epsilon)
  } else{
    epsilon = z*sigma/sqrt(n)
    print(x.mean - epsilon)
    print(x.mean + epsilon)
  }
}
listX = c(12.00,12.05,12.10,12.15,12.20,12.25,12.30,12.35,12.40)
listN = c(2, 3 ,7 ,9 ,10, 8 ,6 ,5 ,3)
vect = rep(listX, listN)
vect
alpha = 1 - .95
ktc.tb.maus(vect ,NULL, alpha)