Нам нужно заполнить все точки NA формулами без каких-либо функций lm или функций предсказания в R - PullRequest
0 голосов
/ 30 сентября 2019

Нам нужно заполнить все точки NA формулами без каких-либо функций lm или функций прогнозирования в R. Помогите! Я не могу понять это без lm или предсказания, я сделал x_bar, y_bar и n, однако другие гораздо сложнее. Это используется в R studio, но может быть и в R.

statsslr = function(x, y){
  
  x_bar = mean(x)
  y_bar = mean(y)
  n = length(x)

  #fit the model
  beta1_hat = NA
  beta0_hat = NA

  #find fitted values and residuals for the n inputs in vector x
  yhat = NA         #this is a vector of length n
  residuals = NA    #this is a vector of length n
  
  SSE = NA
  SST = NA
  SSxx = NA
  
  #calculate measures of fit
  s = NA
  Rsq = NA
  
  #calculate standard errors
  beta0_SE = NA
  beta1_SE = NA
  
  #calculate t-scores
  beta0_tscore = NA
  beta1_tscore = NA
  
  #calculate p-values
  beta0_pvalue = NA
  beta1_pvalue = NA
  
  #DON'T CHANGE
  #Plotting lines with software can be done by plotting points.  Think about it... the screen only has so many pixels, so isn't it the same thing?
  #We'll plot the confidence bands by finding their values at 1000 particular x-values over the range of the x data
  xp = seq(min(x), max(x), length=1000)
  
  #Calculate 95% confidence and prediction intervals for the 1000 particular x-values in xp
  #Everything calculated in this block is for each value in xp, so the results are vectors of length 1000
  yp = NA
  SE_yp_at_xp = NA
  SE_Ey_at_xp = NA
  CI_yp_upper = yp + qt(NA, NA)*SE_yp_at_xp   #little help :)
  CI_yp_lower = NA
  CI_Ey_upper = NA
  CI_Ey_lower = NA

  #DON'T CHANGE (unless you want to customize)
  options(digits=4)
  x_name = deparse(substitute(x))
  y_name = deparse(substitute(y))
  plot(x, y, xlab=paste("x = ", x_name), ylab=paste("y = ", y_name))
  abline(beta0_hat, beta1_hat, lwd=2)
  lines(xp, CI_yp_upper, col="red", lty=2, lwd=1.5)
  lines(xp, CI_yp_lower, col="red", lty=2, lwd=1.5)
  lines(xp, CI_Ey_upper, col="blue", lty=2, lwd=1.5)
  lines(xp, CI_Ey_lower, col="blue", lty=2, lwd=1.5)

  #FIX ALL THE MISTAKES
  cat("\nSimple Least Squares with x = ", y_name, "and y =", x_name,"\n\n")
  cat("The sample regression equation is \n\n\t y = ", beta0_hat, "+", beta1_SE, "* x\n\n")
  cat("Intercept:  SE      =", beta0_SE, "\n\t    t-score =", beta0_hat, "\n\t    P(>|t|) =", 1-beta0_pvalue, "\n\n")
  cat("Slope:\t    SE      =", beta0_SE, "\n\t    t-score =", beta1_tscore, "\n\t    P(>|t|) =", 1-beta1_pvalue, "\n\n")
  cat("Residual Standard Error is s = ", Rsq, "on", n, "degrees of freedom")
  cat("\nCoefficient of Determination is R^2 =", sqrt(Rsq), "\n\n")

  options(digits=7)

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...