2014-06-11 29 views
9

Come si stampa l'equazione di una linea su un grafico?R equazione di stampa della regressione lineare sulla trama stessa

Ho 2 variabili indipendenti e vorrei un'equazione come questa:

y=mx1+bx2+c 

where x1=cost, x2 =targeting 

posso tracciare la retta di regressione, ma come faccio a stampare l'equazione sulla trama?

Forse non posso stampare le 2 variabili indipendenti in un'equazione, ma come faccio a dire y=mx1+c almeno?

Ecco il mio codice:

fit=lm(Signups ~ cost + targeting) 
plot(cost, Signups, xlab="cost", ylab="Signups", main="Signups") 
abline(lm(Signups ~ cost)) 
+1

1) Volevi i valori * dei coefficienti * nell'equazione o semplicemente 'y = m x1 + b x2 + c'? 2) La linea tracciata (1 predittore) non corrisponde al modello lineare che hai montato. Infatti, il coefficiente per la variabile di costo nella retta si adatta [potrebbe essere diverso nel segno] (http://en.wikipedia.org/wiki/Simpson%27s_paradox) a quello della regressione multipla. Se stampi qualcosa che potrebbe essere drasticamente diverso, non ti confonderebbe? –

+3

duplicati: http://stackoverflow.com/questions/14913109/how-to-specify-equation-for-regression-line-in-ggplot2 (ggplot), http://stackoverflow.com/questions/22970708/plot- quadratica-regressione-con-equazione-visualizzata (quadratica), http://stackoverflow.com/questions/9681765/display-regression-equation-and-r2-for-each-scatter-plot-when-using-facet-wrap (gpl faceted), http://stackoverflow.com/questions/12248116/add-text-to-lattice-plot (reticolo) –

risposta

11

ho cercato di automatizzare l'uscita un po ':

fit <- lm(mpg ~ cyl + hp, data = mtcars) 
summary(fit) 
##Coefficients: 
##    Estimate Std. Error t value Pr(>|t|)  
## (Intercept) 36.90833 2.19080 16.847 < 2e-16 *** 
## cyl   -2.26469 0.57589 -3.933 0.00048 *** 
## hp   -0.01912 0.01500 -1.275 0.21253 


plot(mpg ~ cyl, data = mtcars, xlab = "Cylinders", ylab = "Miles per gallon") 
abline(coef(fit)[1:2]) 

## rounded coefficients for better output 
cf <- round(coef(fit), 2) 

## sign check to avoid having plus followed by minus for negative coefficients 
eq <- paste0("mpg = ", cf[1], 
      ifelse(sign(cf[2])==1, " + ", " - "), abs(cf[2]), " cyl ", 
      ifelse(sign(cf[3])==1, " + ", " - "), abs(cf[3]), " hp") 

## printing of the equation 
mtext(eq, 3, line=-2) 

enter image description here

Speranza che aiuta,

alex

3

si utilizza ?text. Inoltre, non utilizzare abline(lm(Signups ~ cost)), poiché si tratta di un modello diverso (vedere la risposta al CV qui: Is there a difference between 'controling for' and 'ignoring' other variables in multiple regression). In ogni caso, prendere in considerazione:

set.seed(1) 
Signups <- rnorm(20) 
cost  <- rnorm(20) 
targeting <- rnorm(20) 
fit  <- lm(Signups ~ cost + targeting) 

summary(fit) 
# ... 
# Coefficients: 
#    Estimate Std. Error t value Pr(>|t|) 
# (Intercept) 0.1494  0.2072 0.721 0.481 
# cost   -0.1516  0.2504 -0.605 0.553 
# targeting  0.2894  0.2695 1.074 0.298 
# ... 

windows();{ 
    plot(cost, Signups, xlab="cost", ylab="Signups", main="Signups") 
    abline(coef(fit)[1:2]) 
    text(-2, -2, adj=c(0,0), labels="Signups = .15 -.15cost + .29targeting") 
} 

enter image description here

Problemi correlati