Use regression residuals to calculate another regression (within the function) in R [closed]

4

I'm using a function to calculate regressions. I need the residuals of a specific relation to relate to another variable. But I need the residuals to be calculated according to facet grid.

So, for each of the divisions the waste will be specific. This is my code, but it returns the null values.

reg = function(data) {
    model1 = lm(r ~ a,data= df)
    model1_sum = summary(model1)
    residuals = as.data.frame(model1$residuals)
    df2 = cbind(df,residuals)
    names(df2)[names(df2)=="model1$residuals"] <- "residuals"
    model2 = lm(residuals ~ a,data= df2)
    model2_sum = summary(model2)
    formula = sprintf("y= %.3f %+.3f*x",coef(model)[1], coef(model)[2])
    r = model2_sum$r.squared
    r2 = sprintf("r2= %.3f", r)
    x  = cor.test(~residuals + lat,data = df2)
    r0 = sprintf("r= %.3f", sqrt(model2_sum$r.squared))
    p1 =  pf(model2_sum$fstatistic[1],model2_sum$fstatistic[2],model2_sum$fstatistic[3],lower.tail=F)
    p =sprintf("p = %.3f", p1)
    n0 = length(model2_sum$residual)
    n1 = sprintf("N = %.f", n0)
    data.frame(formula=formula, r=r0,r2=r2, p=p,n=n1, stringsAsFactors=FALSE)
}

df2_math = ddply(data, c("continente","banco"), df2)
df2_math
    
asked by anonymous 11.07.2015 / 16:37

1 answer

0

The code has several errors. The function produces results, but I do not know if what it is trying to do.

# simulando dados
set.seed(123)
e=rnorm(50)
a=runif(50,2,6)+2*e;lat=rnorm(50,3,.5)
df=data.frame(r=2+5*a^2+e,a,lat)

reg = function(data) { #data=df
  model1 = lm(r ~ a,data= data)
  model1_sum = summary(model1)
  residuals = as.data.frame(model1$residuals)
  df2 = cbind(data,residuals)
  names(df2)[names(df2)=="model1$residuals"] <- "residuals"
  model2 = lm(residuals ~ a,data= df2)
  model2_sum = summary(model2)
  formula = sprintf("y= %.6g %+.6g*x",coef(model2)[1], coef(model2)[2])
  r = model2_sum$r.squared
  r2 = sprintf("r2= %.4f", r)
  x  = cor.test(~residuals + lat,data = df2)
  r0 = sprintf("r= %.4f", sqrt(model2_sum$r.squared))
  p1 =  pf(model2_sum$fstatistic[1],model2_sum$fstatistic[2],model2_sum$fstatistic[3],lower.tail=F)
  p =sprintf("p = %.4f", p1)
  n0 = length(model2_sum$residual)
  n1 = sprintf("N = %.f", n0)
  data.frame(formula=formula, r=r0,r2=r2, p=p,n=n1, stringsAsFactors=FALSE)
}
reg(df)

              formula         r         r2          p      n
1 y= -5.0243e-16 +0*x r= 0.0000 r2= 0.0000 p = 1.0000 N = 50
    
14.07.2015 / 02:01