Error in linear regression

Hello again,
I made a post recently about having some issues when performing one way ANOVAs. I got a lot of help and managed to solve it, but now I am experiencing another issue when performing a simple linear regression with the same set of data. This is my code:

Species_measurement_merged <- read_excel("Species_measurement_merged.xlsx")
data <- Species_measurement_merged

library(tidyr) #Needed for all %>% in the code
library(tidyverse) #Needed for all %>% in the code
data <- Species_measurement_merged %>%
mutate(across(where(is.character), ~na_if(., "NA"))) %>%
Date.WPD.lm <- lm(Date ~ WaterPot_Dawn, data=data)

The error I get is:

Date.WPD.lm <- lm(Date ~ WaterPot_Dawn, data=data)
Error in, y, offset = offset, singular.ok = singular.ok, ...) :
NA/NaN/Inf in 'y'
In addition: Warning message:
In storage.mode(v) <- "double" : NAs introduced by coercion

I previously used the mutate command to eliminate the issue of the non-valid NAs, but now apparently that error has come back despite the fact that the ANOVAs are still working. When executing the mutate command I get these warning repeated 18 times:

Warning messages:
1: In type.convert.default(x[[i]], ...) :
'' should be specified by the caller; using TRUE

Any idea on what could be wrong? I did not expect to have this error again so this is quite confusing.

Please run the code

data <- Species_measurement_merged %>%
mutate(across(where(is.character), ~na_if(., "NA"))) %>%

and then run


Post the output of the dput() function.
You should check the class of the Date column. Run


Is Date characters?

1 Like

Some remarks:

If you refer to an earlier question, please provide a link to that question.
Of course we can find it, but more help from you leads to more help from us.

The first error/warning you get is often the most important.
When the mutate is not done correctly how reliable would be the succeeding regression?

I changed the call for the type.convert function with the data you provided in your last question.
Then the creation of data works without errors/warnings.

However the regression still fails because the date variable is not numeric (and in this dataset only one-valued).

The code I used you find below. My advice is to have a good look at the data before applying the software.

#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>     filter, lag
#> The following objects are masked from 'package:base':
#>     intersect, setdiff, setequal, union

Species_measurement_merged <- structure(list(Art=c("Acerbuergerianum","Acerbuergerianum",
"tbl","data.frame")) %>% as_tibble()

data <- Species_measurement_merged %>%
mutate(across(where(is.character), ~na_if(., "NA"))) %>%

Date.WPD.lm <- lm(Date ~ WaterPot_Dawn, data=data)
#> Warning in model.response(mf, "numeric"): using type = "numeric" with a factor
#> response will be ignored
#> Warning in Ops.factor(y, z$residuals): '-' not meaningful for factors

#>           Art          Date           Ind WaterPot_Dawn WaterPot_Noon 
#>      "factor"      "factor"     "integer"     "numeric"     "numeric" 
#>    ChloroCont     Leaf_area  Fresh_weight    Dry_weight           DBH 
#>     "numeric"     "numeric"     "numeric"     "numeric"     "numeric" 
#>        Height      1st_leaf        Axis_1        Axis_2   Canopy_size 
#>     "integer"     "numeric"     "integer"     "integer"     "numeric" 
#> Leaf_dry_cont    Crown_area Specific_leaf 
#>     "numeric"     "numeric"     "numeric"
Created on 2023-01-02 with reprex v2.0.2
1 Like

I have used the same code before, and Date is indeed characters. Would changing them into numeric fix this? Here is the link from my previous post (sorry for not providing it first, I am rather new in this):

I have added the as per your suggestion and now it runs with no errors. Thank you. I will change the Date characters to anumeric values and try again.

@HanOostdijk @FJCC I changed the characters in Date from month names to numbers and tried again. Now I get the linear regression with no errors, but with a couple warnings:

Date.WPD.lm <- lm(Date ~ WaterPot_Dawn, data=dataNUM)
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

lm(formula = Date ~ WaterPot_Dawn, data = dataNUM)
(Intercept) WaterPot_Dawn
3.788 -2.471

Are these warning normal? Or have I made another mistake? Thank you.

Having a factor as a left-hand side variable can be okay, but because the numeric value isn't meaningful the residuals aren't meaningful.

I am not sure I follow. Do you mean that the linear regression has not found any significan relation between the two variables? As if the numeric variable that represents the date has no impact on the other one? Thank you.

Its unclear what form your dates are after the changes you spoke about. it seems they are yet factors when they perhaps should be properly numeric.
An easy way to check is to use str() on them.

They appear to be from a cathegory named "int", but the values I assigned are indeed numbers. It is a different number per month (May=1, June=2, July=3, August=4, September=5). Here is the feedback from str():

tibble [450 Γ— 18] (S3: tbl_df/tbl/data.frame)
Art : Factor w/ 20 levels "Acer buergerianum",..: 1 1 1 1 1 2 2 2 2 2 ... Date : int [1:450] 1 1 1 1 1 1 1 1 1 1 ...
Ind : int [1:450] 1 2 3 4 5 1 2 3 4 5 ... WaterPot_Dawn: num [1:450] NA NA 0.41 0.596 0.78 NA NA 0.62 0.75 0.448 ...
$ WaterPot_Noon: num [1:450] NA NA 0.14 0.28 0.31 NA NA 0.255 0.42 0.182

All other variables are num too. I don't see any other way to do this, so does this mean I should do a different type of analysis other than a simple linear regression?

Do you really mean that the outcome for September is fives times the outcome for May? That seems unlikely. If it is what you mean, then a linear regression is fine. If the months are just different outcomes, you may want an ordered logit or a multinomial logit.

The outcome of september is not 5 times the outcome of May. I wanted to analiyse whether the month when the data was taken had any effect on the data itself. Would an ordered logit or a multinomial logit work for this? Thank you.

Yes, an ordered or multinomial logit would work.

But are you asking whether the month affects the data or whether the data affects the month?

I will look into those two then.
The data I am trying to analyse was taken from May to September. I want to test whether there are any differences in data that could be explained by the passing of time (AKA if data was affected by the month it was taken in). I want to check if the variability in data could be explained by the month it was taken in.
Thank you very much for your persistent help.

Then this may be easier. The explanatory variables go on the right in a regression. The data being explained goes on the left. There is nothing problematic with having a factor on the right. lm() will translate a factor into dummy variables.

1 Like

I see. I will try again as per your suggestion. Thank you very much.

It seems it finally worked properly. These are the results:

Date.WPD.lm <- lm(WaterPot_Dawn ~ Date, data=dataNUM)

lm(formula = WaterPot_Dawn ~ Date, data = dataNUM)
(Intercept) Date
0.61622 -0.06975

lm(formula = WaterPot_Dawn ~ Date, data = dataNUM)
Min 1Q Median 3Q Max
-0.42647 -0.13647 -0.02497 0.09991 0.83528
Estimate Std. Error t value Pr(>|t|)
(Intercept) 0.61622 0.03059 20.145 < 2e-16 ***
Date -0.06975 0.01017 -6.861 6.5e-11 ***

Signif. codes: 0 β€˜β€™ 0.001 β€˜β€™ 0.01 β€˜β€™ 0.05 β€˜.’ 0.1 β€˜ ’ 1
Residual standard error: 0.1919 on 226 degrees of freedom
(222 observations deleted due to missingness)
Multiple R-squared: 0.1724, Adjusted R-squared: 0.1687
F-statistic: 47.07 on 1 and 226 DF, p-value: 6.501e-11

Do these results look ok or is there anything concerning? The p-value is 6.5e-11 = 6.5 x 10-11 = 0.000000000065, which would mean the relation between measurements and date (predictor) is significant, right? Thank you.

the R squared is relatively low; therefore the model hasn't revealed a particularly powerful relationship (explaining a lot of the variance); the significance is a sign that the weak signal you found is unlikely to be a pure accident, so is potentially worth more of a look into. a linear model is good at drawing straight lines on charts; have you charted your data, and the fit to look at it?; probably unless September has the most WaterPot_Dawn, and May has the least, with a monotonic trend between; I would expect there to be rather gnarly residuals.


I have not charted my data, and I do not know what that is. I will have to look into it. Thank you for your help.

It looks like date is no longer coded as a factor. If you put it in as a factor, you'll get an estimate for each month if that's what you want.

1 Like