Hi there!
I have a series of datasets that contain a log transformation. I am trying to create a linear model of these datasets and store the resulting coefficients into vectors, and I'm doing this by having R iterate through the datasets via a loop. However, in at least one of the datasets, there is an input of 0, which makes the function undefined, and then the whole code falls apart for the rest of the loop. I've created an if/else statement to remove the rows containing 0s in a particular column, but.... it's not working. I get the following errors:
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
NA/NaN/Inf in 'y'
In addition: Warning message:
In if (data.2$LIGHT_UW == 0) { :
the condition has length > 1 and only the first element will be used
Could someone take a look and see what I'm doing wrong?
Here is my code:
if(data.2$LIGHT_UW == 0) {
# remove any rows with a Light_UW value of 0
data.0 <- data.2[-which(data.2$LIGHT_UW==0),]
# create a linear model of the log-transformed FRLIGHT as a function of depth
lm <- lm(data.0$ln_FRLIGHT~data.0$DEPTH)
# Store the model information
summary <- summary(lm)
# store the r2 value
r2 <- summary$r.squared
# store the multiplier
Kd <- summary$coefficients[2,1]
} else {
# create linear model with the data as it is
lm <- lm(data.2$ln_FRLIGHT~data.2$DEPTH)
summary <- summary(lm)
r2 <- summary$r.squared
Kd <- summary$coefficients[2,1]
}
Here is the dataset containing the input of 0:
data.2 <- structure(list(SITE_ID = c("GBA20-10538", "GBA20-10538", "GBA20-10538",
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538",
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538",
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538",
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538",
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538"
), CAST = c("DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN",
"DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "DOWN", "UP",
"UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP", "UP",
"UP", "UP"), LINE = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28
), DEPTH = c(0.1, 0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20,
20, 15, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0.5, 0.1), TEMPERATURE = c(20.6,
20.6, 20.6, 20.6, 20.3, 18.5, 16.8, 15.8, 14.6, 14.5, 14.3, 13.7,
10.5, 9.5, 9.5, 10.4, 12.8, 13.3, 13.5, 14, 14.6, 18, 19.6, 19.9,
20.2, 20.2, 20.2, 20.2), DO = c(11.2, 11.2, 11.1, 11.3, 11.2,
11.1, 10.8, 10.5, 10.6, 10.5, 10.5, 10.5, 10.5, 10.7, 10.7, 10.5,
10.6, 10.5, 10.5, 10.6, 10.6, 11.4, 11.3, 11.5, 11.3, 11.3, 11.2,
11.2), PH = c(8.71, 8.71, 8.71, 8.71, 8.71, 8.66, 8.54, 8.47,
8.36, 8.33, 8.29, 8.25, 8.13, 8.08, 8.08, 8.06, 8.08, 8.12, 8.13,
8.17, 8.22, 8.47, 8.56, 8.61, 8.64, 8.67, 8.68, 8.67), CONDUCTIVITY = c(283.3,
283.4, 283.5, 283.5, 282.8, 274.4, 274.1, 272.1, 272.1, 272,
271.3, 268.4, 262.8, 260.4, 260.6, 265, 271.2, 271.4, 272.1,
273.3, 275, 283.5, 285.2, 287.3, 285.4, 285.5, 285.1, 285.2),
LIGHT_AMB = c(2103.7, 2114.5, 2102.6, 2115.7, 2130, 2146.9,
2132.7, 2130.2, 2132.2, 2164, 2210.3, 2217.5, 2299.9, 2291.2,
2339, 1972.5, 1706.8, 2093, 1511.2, 1959.2, 1540.1, 2166.4,
2246.1, 2246.1, 2141, 2073.5, 2161, 2151.3), LIGHT_UW = c(1234.9,
1063.8, 848.3, 698, 231.7, 136.5, 73.8, 38.1, 28.3, 16.2,
9, 7, 1.9, 0, 0.4, 0.3, 6.9, 8.6, 13.3, 24, 44.8, 76.5, 135.5,
216.4, 377.7, 1092.4, 1117.8, 1288.9), FRLIGHT = c(0.587013357417883,
0.503097659021045, 0.403452867877865, 0.329914449118495,
0.108779342723005, 0.0635800456472123, 0.0346040230693487,
0.0178856445404187, 0.013272676109183, 0.00748613678373383,
0.00407184545084378, 0.00315670800450958, 0.000826122874907605,
0, 0.000171013253527148, 0.000152091254752852, 0.0040426529177408,
0.00410893454371715, 0.00880095288512441, 0.0122498979175174,
0.0290890201934939, 0.0353120384047267, 0.0603267886558924,
0.0963447753884511, 0.176412891172349, 0.526838678562817,
0.517260527533549, 0.599126109794078), ln_FRLIGHT = c(-0.532707704016207,
-0.686970974604326, -0.907695606328041, -1.1089219032269,
-2.21843382726514, -2.7554556055505, -3.36378532999712, -4.02375686887641,
-4.32204778486718, -4.89470239772803, -5.50365895456918,
-5.75822556512842, -7.09876703653877, -Inf, -8.67376949845833,
-8.79102985704596, -5.51085413971063, -5.49459151918654,
-4.73289528095959, -4.40223767529001, -3.53739448894348,
-3.34353134188547, -2.80797901758499, -2.33982211093242,
-1.73492805887127, -0.640860890086886, -0.659208609671288,
-0.512283169145905)), row.names = c(NA, -28L), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), groups = structure(list(SITE_ID = c("GBA20-10538",
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538",
"GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538", "GBA20-10538",
"GBA20-10538", "GBA20-10538", "GBA20-10538"), DEPTH = c(0.1,
0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20), .rows = structure(list(
c(1L, 28L), c(2L, 27L), c(3L, 26L), c(4L, 25L), c(5L, 24L
), c(6L, 23L), c(7L, 22L), c(8L, 21L), c(9L, 20L), c(10L,
19L), c(11L, 18L), c(12L, 17L), c(13L, 16L), 14:15), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -14L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), na.action = structure(c(`1` = 1L), class = "omit"))
Any help would be greatly appreciated! Thanks so much!