I am having troubling fitting two linear regression models.
My dataset, named 'heart', contains mode of treatment (trtment) for congenital heart disease in infants, with 0 standing for circulatory arrest treatment and 1 for low-flow bypass treatment. Psychomotor Development Index (PDI) score and Mental Development Index (MDI) score are then noted against the type of treatment administered to the infant.
I am to fit two linear regression models, one with PDI score as the response and the another with Mental Development Index (MDI) score as the response.
I am using the following command for PDI and I am not getting the correct output to construct a linear model.
If I use this following command instead, I am still getting an error message and incorrect output;
fit <- with(heart, lm(pdi~trtment))
Warning messages:
1: In model.response(mf, "numeric") :
using type = "numeric" with a factor response will be ignored
2: In Ops.factor(y, z$residuals) : ‘-’ not meaningful for factors
I will really appreciate if someone could help me troubleshoot my commands as I need to model both PDI and MDI.
It seems your pdi column is a factor rather than numeric. It is hard to say why without seeing more of your code, especially the reading in of the data. Please make a Reproducible Example (reprex).
Just a few lines of your data and then the fitting process should be enough.
Your example data work fine for me, as shown below.
DATA <- data.frame(trtment = c(0,1,1,0,0,0),
pdi = c(80,118,122,98,98,111),
mdi = c(74, 124, 109,78,91,130)
)
DATA
#> trtment pdi mdi
#> 1 0 80 74
#> 2 1 118 124
#> 3 1 122 109
#> 4 0 98 78
#> 5 0 98 91
#> 6 0 111 130
summary(DATA)
#> trtment pdi mdi
#> Min. :0.0000 Min. : 80.0 Min. : 74.00
#> 1st Qu.:0.0000 1st Qu.: 98.0 1st Qu.: 81.25
#> Median :0.0000 Median :104.5 Median :100.00
#> Mean :0.3333 Mean :104.5 Mean :101.00
#> 3rd Qu.:0.7500 3rd Qu.:116.2 3rd Qu.:120.25
#> Max. :1.0000 Max. :122.0 Max. :130.00
fit <- with(DATA, lm(pdi ~ trtment))
fit
#>
#> Call:
#> lm(formula = pdi ~ trtment)
#>
#> Coefficients:
#> (Intercept) trtment
#> 96.75 23.25
Created on 2019-04-22 by the reprex package (v0.2.1)
What do you see when you do
summary(heart)
Is pdi treated as numbers, with a Mean and Median, or is it treated as a factor or character? If it is not numeric, I suspect you have some bad data or possibly NA-denoting strings somewhere in the column.
Great! Thank you so much for the quick reply. I am going to try that again although your post prompted me to check my dataset and lo and behold, there is a row that has no PDI data -
trtment PDI MDI
0 . 109
Literally a dot there!
I think I am going to omit this reading and proceed with my modeling. Really appreciate your help.