BEWARE FORMULAS IN BLACK SO DIFFICULT TO READ IN DARK MODE
I would like to create a matrix of point estimates of
where
and
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