Quadratic Curve Estimating Minimum Square using R

Asked

Viewed 271 times

4

I have a quadratic model that I want to run a simple multivariate regression using Minimal Ordinary Squares.

This is my code:

df<-data.frame(rnorm(50),rnorm(50))
x_Square<-df[,2]^2
data<-cbind(df,x_Square)
names(data)<-c("y","x","x_Square")
regression<-lm(data$y ~ data$x + data$x_Square)

Now I want to plot the graph of the squared curve adjusted in the scatter graph (y - x).

How do I do that?

When I do: plot(regression) I don’t have that option.

Some help?

Thank you!

1 answer

2


It seems to me that your data has no quadratic regression structure. I would create another data set as follows:

set.seed(321)

n     <- 20 # numero de observacoes
x1    <- rep(1:n/2, each=2) # variavel deterministica linear
x2    <- x1^2 # variavel deterministica quadratica
y     <- -10*x1 + x2 + rnorm(n) # criacao do y, juntando x1, x2 e um erro
dados <- data.frame(y, x1, x2) # banco de dados final

With the creation of the database, we can proceed with the adjustment of a model as well as the plotting of the results:

library(ggplot2)

ggplot(dados, aes(x=x1+x2, y=y)) + geom_point()

ajuste <- lm(y ~ x1+x2)
summary(ajuste)

Call:
lm(formula = y ~ x1 + x2)

Residuals:
    Min      1Q  Median      3Q     Max 
-2.4894 -0.6994  0.2779  0.6104  2.5972 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)   0.09502    0.62532   0.152     0.88    
x1          -10.05787    0.27428 -36.670   <2e-16 ***
x2            1.00529    0.02537  39.619   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.189 on 37 degrees of freedom
Multiple R-squared:  0.9778,    Adjusted R-squared:  0.9766 
F-statistic:   814 on 2 and 37 DF,  p-value: < 2.2e-16

Note how the estimates of x1 and x2 match the coefficients defined in the creation of the variable y.

Next I create a new data frame, just to plot the regression results. I could have used geom_smooth(), but I think this way is more didactic.

regressao <- data.frame(x1, ajuste$fitted.values)
names(regressao) <- c("x1", "fitted")

ggplot(dados, aes(x=x1, y=y)) + geom_point() + 
geom_line(data=regressao, aes(x=x1, y=fitted, colour="red"))

inserir a descrição da imagem aqui

  • Thanks Marcus! I wanted to bend the curve even without y following a quadratic specification. But it helped me a lot! Thanks.

  • @If Marcus' answer solved your problem, don’t forget to choose it as the best answer!

  • 1

    Okay @Molx, done!

Browser other questions tagged

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