I am trying to check if the difference between two coefficients extracted from two equation is greater than zero using the deltaMethod
in R
set.seed(111)
y1.predict <- rnorm(12,3,1)
y1.n1 <- c(1000, 2000, 3000, 4000,0,0,0,0,1000,4000,3000,3000)
y1.n2 <- c(0,0,0,0,1000, 2000, 3000, 4000,4000,1000,2000,2000)
y2.predict <- rnorm(16,5,1)
y2.n1 <- c(1000, 2000, 3000, 4000,0,0,0,0,1000,4000,3000,3000,1000,0,2500,4000)
y2.n2 <- c(0,0,0,0,1000, 2000, 3000, 4000,4000,1000,2000,2000,0,4000,2500,0)
df1 <- data.frame(y1.predict, y1.n1,y1.n2)
df2 <- data.frame(y2.predict, y2.n1,y2.n2)
y1 <- lm(y1.predict ~ n1 + n2)
y2 <- lm(y2.predict ~ n1 + n2)
#Typical use of delta method when comparing coefficients from same regression. Here I am trying to see if n1 - n2 > 0
g1.delta <- deltaMethod(y1,"(-n1) - (-n2)")
What I need is to compare the coefficient of n1
of y1
and n2
of y2
using the delta method. How can I do this?
deltaMethod(...)
extracts the variance covariance matrix from the fitted model object (the lm
object in your example). If you are comparing two regressions, based on different data, you don't know the vcov.
If you assume n1
(from fit.1
) and n2
(from fit.2
) are independent, then you could use deltaMethod(...)
this way:
df1 <- data.frame(y1.predict, n1=y1.n1, n2=y1.n2)
df2 <- data.frame(y2.predict, n1=y2.n1, n2=y2.n2)
fit.1 <- lm(y1.predict ~ n1 + n2, df1)
fit.2 <- lm(y2.predict ~ n1 + n2, df2)
#
means <- c(coef(fit.1)['n1'], coef(fit.2)['n2'])
##
# assumes independence (covariance = 0)
#
vcov <- diag(c(summary(fit.1)$coefficients['n1', 2],
summary(fit.2)$coefficients['n2', 2])^2)
deltaMethod(means, 'n1-n2', vcov)
## Estimate SE 2.5 % 97.5 %
## n1 - n2 6.3295e-05 2.7686e-04 -4.7934e-04 6e-04
Since the 95%CL includes 0 you cannot conclude the n`s are different.
BTW your code does not run as presented. This will likely discourage ppl from responding to your questions.