Use residuals of regression to calculate another regression (inside function) in R -


i'm using function calculate regressions. need residuals relationship relate variable. because change facet grid several times.

this code:

modelregression = function(file) { mod2 = lm(y ~ x,data=file) mod = lm(mod2$residuals ~ anotherx,data=file) mod_sum = summary(mod) formula = sprintf("y= %.3f %+.3f*x",                 coef(mod)[1], coef(mod)[2]) r = mod_sum$r.squared r2 = sprintf("r2= %.3f", r) x  = cor.test(~mod2$residuals + anotherx,data=file) r0 = sprintf("r= %.3f", x[4]) p1 = pf(mod_sum$fstatistic[1],mod_sum$fstatistic[2],mod_sum$fstatistic[3],lower.tail=f) p =sprintf("p = %.3f", p1) n0 = length(mod_sum$residual) n1 = sprintf("n = %.f", n0) data.frame(formula=formula, r=r0,r2=r2, p=p,n=n1, stringsasfactors=false) }  modelregression_math = ddply(file, c("database","level"), modelregression) 

the runs without problem, coefficients zero. how can fix this?

you need residuals reside "inside" specified data=. thus, insert line follwing before running second regression:

file <- cbind(mod2$residuals, file) 

however, work if lines in file match order , amount of lines in used 1st regression. if have missing values, gets more complicated: use mod2$model data used in regression (also in right order) , combine residuals:

data_with_residuals <- cbind(mod2$model, mod2$residuals) mod = lm(residuals ~ anotherx, data=data_with_residuals) 

(or approach merge() trick.)


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -