Andreas Gammelgaard Lauritsen - 1 year ago 64

R Question

I'm struggling with a problem in R.

This list contains data of pregnant women, birthweight and parity.

I want to predict the difference in the weight gain of two future women giving birth for the first time (parity 0) to respectively a child of 3200 g and 3700 g.

I have this data set:

`str(birth_all)`

'data.frame': 910 obs. of 13 variables:

$ birthweight: int 3270 3580 3030 2460 3400 4250 4260 3450 3880 3600 ...

$ parity : int 0 0 0 0 0 0 2 0 1 0 ...

$ weightgain : num 17.9 15.9 10.4 11.5 11.6 ...

I can do like this:

`bweight<-birth_all$birthweight[birth_all$parity==0]`

wgain<-birth_all$weightgain[birth_all$parity==0]

P1<-data.frame(bweight=c(3200,3700))

pred_val<-predict(lm(wgain~bweight),newdata = P1,interval = "confidence")

pred_val

fit lwr upr

1 15.36842 14.62545 16.11138

2 16.71779 16.07556 17.36002

The easy calculation will be this:

`pred_val[2,1]-pred_val[1,1]`

[1] 1.349368

I don't think this solution is very elegant, and I loose the confidence intervals.

Alternatively I could use the multcomp-package, but I get similar results. Can anybody help me with a better solution? Thanks a lot.

Answer Source

One way to get the intervals would be doing the regression with differences:

```
bweighti=bweight-3200
wgaini=wgain-pred_val[1,1]
P1i<-data.frame(bweighti=c(3700-3200))
pred_vali<-predict(lm(wgaini~bweighti),newdata = P1i,interval = "confidence")
pred_vali
```