compute a grid of (normal) point estimates over a set of Y values

Viewed 24

BEWARE FORMULAS IN BLACK SO DIFFICULT TO READ IN DARK MODE

I would like to create a matrix of point estimates of \hat{R}_i(Y=y, X)

where Y_i|X_i\sim N(\beta_0+X_i\beta_1,\sigma^2)

and \hat{R}_i=\frac{1}{\sqrt{2\pi\hat{\sigma}^2}}exp\Big(-\frac{1}{2\hat{\sigma}^2}(Y_i-\hat{\beta}_0-X_i\hat{\beta}_1)^2\Big)

for certain set values of Y (e.g. the percentiles of Y).

I have been trying to adjust to my needs code found here and there, starting from a function which is supposed to do something similar, but I haven't been successful.

Ideally I would also like to name the new variables (the point estimates of R for each level of Y) with a name that make sense.

# creating the dataset
set.seed(2020)
x.Gender <- rep(0:1, c(4000,6000)) 
x.Age <- round(abs(rnorm(10000, mean=45, sd=10))) 
Y <- x.Gender * 5 + x.Age / 5 + round(rnorm(10000, mean=0, sd=2)) 
data <- data.frame(x.Age, x.Gender, Y)
head(data)

#estimating the parameters of the normal function
lm.Y <- lm(formula = Y~x.Age+x.Gender, data=data) 

#I can compute point estimate of the values of the normal function at Y for each observation
data$GPS = dnorm(Y, mean = lm.Y$fitted, sd=summary(lm.Y)$sigma)

# I now would like to compute point estimates for a set of values of Y for all observations and store them in a matrix

# I store the quantiles of Y (the values at which I would like to compute the point estimates)
grid_val <- quantile(data$Y, probs = seq(0, .99, by = 0.01))


#this is where I fail:

GPS_grid <- for (i in 1:length(grid_val)) 
{cbind(mutate(sweep(data, 1, STATS=grid_val[i], FUN=dnorm(grid_val[i], mean = lm.Y$fitted, 
                                        sd=summary(lm.Y)$sigma))))}
#here the error message says that dnorm is not a function, character or symbol

Thanks in advance

Change variable name in for loop using R

0 Answers
Related