R/myy.R

Defines functions myy

Documented in myy

#' Creates plots of features and response along with confidence interval for mean of response
#'
#' @param data The data to be read in
#' @param Y The response vector
#' @param X The feature matrix
#' @param alpha The level of significance of the CI
#' @param ncategories The number of categores for plotting
#'
#' @return several plots
#' @export
#'
#' @examples
myy = function(data,Y,X,alpha,ncategories){
  library(ggplot2)
  n = dim(X)[1]
  kplus1 = dim(X)[2]
  df = n-(kplus1)
  
  XXinv = solve(t(X)%*%X)
  betahat = (XXinv%*%t(X))%*%Y
  yhat = X%*%betahat
  sse  = t(Y)%*%Y -t(betahat)%*%t(X)%*%Y
  
  s2 = sse/df
  s=sqrt(s2)
  
  rownames(a) = c()
  colnames(a) = c()
  
  yhat = X%*%betahat
  
  for(j in 1:ncategories)
    {
      p = ggplot(data,aes(x=X[,j+1], y=Y[,1])) + labs(y=colnames(Y), x =colnames(X)[j+1])+   geom_point()
    print(p)
    
    }
  
  pairs(X[,2:kplus1], pch = 19)

  mat = matrix(NA, nr = n, nc = 3, byrow = TRUE, dimnames = list(1:n,c("L", "U", "yhat") ))
  for(i in 1:n)
    {
    
    a = as.matrix(X[i,])
    mat[i, 1] = yhat[i] - qt(1-alpha/2, df)*s*sqrt(1 +t(a)%*%XXinv%*%a)
    mat[i, 2] = yhat[i] + qt(1-alpha/2, df)*s*sqrt(1 +t(a)%*%XXinv%*%a)
    mat[i, 3] = yhat[i]
  }
  
  matplot(mat, type = "p", ylab = "Confidence Intervals", xlab = "1:n")
  segments(x0 = 1:n, y0 = mat[,1], x1=1:n,y1 = mat[,2])
  
  for(i in 1:n)
  {
    val = c("Point Estimate",mat[i,3],"Lower Bound",mat[i,1],"Upper Bound",mat[i,2])
    print(val)
  }
}
reza-niazi/MLRpack documentation built on Oct. 16, 2020, 7:30 a.m.