# [R] Help with regression

David Winsemius dwinsemius at comcast.net
Thu Sep 4 23:49:23 CEST 2014

```On Sep 4, 2014, at 8:40 AM, Basilius Sapientia wrote:

> I have this code:
> Vm <- c(6.2208, 4.9736, 4.1423, 3.1031, 2.4795, 1.6483, 1.2328, 0.98357,
> 0.81746, 0.60998); #Molvolume
> p <- c(0.4, 0.5, 0.6, 0.8, 1, 1.5, 2, 2.5, 3, 4)*1000; #Pressure
> Rydb <- 8.3144621; #Constant
> Tempi <- 300; #Temperature in Kelvin
>
> Vmi <- Vm^(-1); #To get Vm^(-1)
> Zi <- (p*Vm)/(Rydb*Tempi) #To get Z
>
> #Plot
> dframe <- data.frame(Vmi, Zi)
> plot(dframe, pch=19, col='red', main='Thermodynamic properties of Argon',
> xlab='1/Vm', ylab='Z')
>
> #Fit for B
> fitb <-lm(Zi ~ Vmi);
> fitb\$coefficients;
> fitb\$coefficients;
> summary(fitb)

The appropriate approach to regression on polynomials is to use poly(.)

> fitb <-lm(Zi ~ poly( Vmi, 2) );
> fitb

Call:
lm(formula = Zi ~ poly(Vmi, 2))

Coefficients:
(Intercept)  poly(Vmi, 2)1  poly(Vmi, 2)2
0.9907137     -0.0198321      0.0006682

> summary(fitb)

Call:
lm(formula = Zi ~ poly(Vmi, 2))

Residuals:
Min         1Q     Median         3Q        Max
-2.622e-05 -7.785e-06  3.268e-06  1.047e-05  1.557e-05

Coefficients:
Estimate Std. Error   t value Pr(>|t|)
(Intercept)    9.907e-01  4.884e-06 202853.74  < 2e-16 ***
poly(Vmi, 2)1 -1.983e-02  1.544e-05  -1284.12  < 2e-16 ***
poly(Vmi, 2)2  6.682e-04  1.544e-05     43.27  9.2e-10 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.544e-05 on 7 degrees of freedom
Multiple R-squared:      1,	Adjusted R-squared:      1
F-statistic: 8.254e+05 on 2 and 7 DF,  p-value: < 2.2e-16

The second order term has been constructed to not be highly correlated with the linear term.

> plot( Zi, predict(fitb) )

And now that you "know" that both terms are significant, construct that polynomial with:

> fitb <-lm(Zi ~  Vmi+I(Vmi^2) );
> fitb

Call:
lm(formula = Zi ~ Vmi + I(Vmi^2))

Coefficients:
(Intercept)          Vmi     I(Vmi^2)
0.999964    -0.015025     0.001063

>
> I want to make a regression on the data with this generel formula:
> y=1+Bx+Cx^2. I need to figure out what B and C in this formula is. Please
> help me! I want to become better to R.

Please read the Posting Guide. It's really very easy to post in plain-text from gmail.
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help