0

I've been trying to look at the explanatory power of individual variables in a model by holding other variables constant at their sample mean.

However, I am unable to do something like:

Temperature = alpha + Beta1*RFGG + Beta2*RFSOx + Beta3*RFSolar 

where Beta1=Beta2=Beta3 -- something like

Temperature = alpha + Beta1*(RFGG + RFSolar + RFSOx)

I want to do this so I can compare the difference in explanatory power (R^2/size of residuals) when one independent variable is not held at the sample mean while the rest are.

Temperature = alpha + Beta1*(RFGG + meanRFSolar + meanRFSOx)

or

Temperature = alpha + Beta1*RFGG + Beta1*meanRFSolar + Beta1*meanRFSOx

However, the lm function seems to estimate its own coefficients so I don't know how I can hold anything constant. Here's some ugly code I tried throwing together that I know reeks of wrongness:

    # fixing a new clean matrix for my data
dat = cbind(dat[,1:2],dat[,4:6]) # contains 162 rows of: Date, Temp, RFGG, RFSolar, RFSOx

# make a bunch of sample mean independent variables to use
meandat = dat[,3:5]
meandat$RFGG = mean(dat$RFGG)
meandat$RFSolar = mean(dat$RFSolar)
meandat$RFSOx = mean(dat$RFSOx)

RFTotal = dat$RFGG + dat$RFSOx + dat$RFSolar

B = coef(lm(dat$Temp ~ 1 + RFTot)) # trying to save the coefficients to use them...
B1 = c(rep(B[1],length = length(dat[,1])))
B2 = c(rep(B[2],length = length(dat[,1])))

summary(lm(dat$Temp ~ B1 + B2*dat$RFGG:meandat$RFSOx:meandat$RFSolar)) # failure
summary(lm(dat$Temp ~ B1 + B2*RFTot))

Thanks for taking a look to whoever sees this and please ask me any questions.

tumultous_rooster
  • 12,150
  • 32
  • 92
  • 149
Evan Friedland
  • 3,062
  • 1
  • 11
  • 25
  • You would never use the `Beta`'s in the formula, right? Are you looking for the I() function? Or perhaps you want an offset term? `Temperature = RFGG + offset( mean(RFSolar) + mean(RFSOx) )`. No data presented ... therefore likely to be closed if not edited to make a complete question in the next few hours. – IRTFM Feb 09 '16 at 03:06
  • What's the best way for me to present data for the community to use? I'm new here and thought this was more of a conceptual problem and formula use issue than actually needing my data. – Evan Friedland Feb 09 '16 at 03:42
  • See [how to make a reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for tips on sharing data to make your question easier to answer. YOu Probably just want `summary(lm(Temp ~ I(RFGG + meandat$RFSOx + meandat$RFSolar), dat))` but mixing data from two data.frames isn't really a good idea in general. – MrFlick Feb 09 '16 at 04:10

1 Answers1

0

Thank you both of you, it was a combination of eliminating the intercept with (-1) and the offset function.

a = lm(Temp ~ I(RFGG + RFSOx + RFSolar),data = dat)
beta1hat = rep(coef(a)[1],length=length(dat[,1]))
beta2hat = rep(coef(a)[2],length=length(dat[,1]))

b = lm(Temp ~ -1 + offset(beta1hat) + offset(beta2hat*(RFGG + RFSOx_bar + RFSolar_bar)),data = dat)
c = lm(Temp ~ -1 + offset(beta1hat) + offset(beta2hat*(RFGG_bar + RFSOx + RFSolar_bar)),data = dat)
d = lm(Temp ~ -1 + offset(beta1hat) + offset(beta2hat*(RFGG_bar + RFSOx_bar + RFSolar)),data = dat)
Evan Friedland
  • 3,062
  • 1
  • 11
  • 25