I want to to fit y~intercept+x.1*x+x.2*x^2.
Here is the indata. The items "ab" and "ad" have the same underlying formula as "aa" and "ac".
library(data.table)
library(ggplot2)
set.seed(123)
DT.1 <- data.table(item= "aa", x=1:100)
DT.1[, y:=0+x*40-x^2*0.4+rnorm(100, mean=0, sd=100)]
DT.2 <- data.table(item= "ab", x=1:10)
DT.2[, y:=0+x*40-x^2*0.4+rnorm(10, mean=0, sd=100)]
DT.3 <- data.table(item= "ac", x=1:100)
DT.3[, y:=0+x*50-x^2*0.4+rnorm(100, mean=0, sd=100)]
DT.4 <- data.table(item= "ad", x=61:70)
DT.4[, y:=0+x*40-x^2*0.4+rnorm(10, mean=0, sd=100)]
DT <- rbind(DT.1, DT.2, DT.3, DT.4)
p <- ggplot(DT, aes(x=x, y=y, group=item))
p <- p + geom_point()
p <- p + facet_wrap(~item)
p
But when doing a normal lm, the fitting of "ab" and "ad" is not what I need.
DT[, c("intercept", "x.1", "x.2"):=as.list(coef(lm("y~x+I(x^2)", .SD))), by = list(item)]
DT[, x.index:=1:.N, by=item]
DT.coef <- DT[x.index==1, list(item, intercept, x.1, x.2)]
DT.1 <- data.table(item="aa", x=1:100)
DT.1[, y.lm:=DT.coef[item=="aa", intercept]+x*DT.coef[item=="aa", x.1]+x^2*DT.coef[item=="aa", x.2]]
DT.2 <- data.table(item="ab", x=1:100)
DT.2[, y.lm:=DT.coef[item=="ab", intercept]+x*DT.coef[item=="ab", x.1]+x^2*DT.coef[item=="ab", x.2]]
DT.3 <- data.table(item="ac", x=1:100)
DT.3[, y.lm:=DT.coef[item=="ac", intercept]+x*DT.coef[item=="ac", x.1]+x^2*DT.coef[item=="ac", x.2]]
DT.4 <- data.table(item="ad", x=1:100)
DT.4[, y.lm:=DT.coef[item=="ad", intercept]+x*DT.coef[item=="ad", x.1]+x^2*DT.coef[item=="ad", x.2]]
DT.lm <- rbind(DT.1, DT.2, DT.3, DT.4)
p <- ggplot(DT.lm, aes(x=x, y=y.lm, group=item))
p <- p + geom_point()
p <- p + facet_wrap(~item)
p
So I would like to set a prior that makes the "ab" and "ad" be much more similar to the "aa" and "ac".
Trying to replicate examples from rstanarm documentation and vignettes, I get stuck in how to define the prior. This is the closest I get, and it produces the error "Error in prior$location : $ operator is invalid for atomic vectors".
library(rstanarm)
y.posterior <- stan_lm(y~x+I(x^2), data=DT[item=="aa", ], prior=mean(0.5), chains=1, cores=1, iter=1000, seed=12345)
What (conceptually) is the prior of y~intercept+x.1*x+x.2*x^2? Should I define mean and sd for intercept, x.1, and x.2? I can "draw" how the prior curve should look (peak around x=50 with y around 1100), which translates to intercept=18, x.1=40, and x.2=-0.4.
How do I translate that to the prior rstanarm wants?
I hope my trying to use advanced statistical methods with so little knowledge of statistical science will not insult anyone here. Please do not comment on the input data, it is only there to make the code run.