Use regression residues to calculate another regression (within the function) in R

Asked

Viewed 113 times

4

I’m using a function to calculate regressions. I need the residues of a specific relation to relate to another variable. However I need the waste to be calculated according to Facet grid.

Thus, for each of the divisions the waste will be specific.
This is my code, but it returns 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
  • 1

    Joyce, could you post a reproducible example of your data? Also, your code seems to be incomplete: no time do you use the function reg, the objects df and df2 have not been defined and you do not use the function argument within it.

1 answer

0

The code has several errors. The function produces results, but I don’t know if what you’re 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.