What is wrong with my lm function using lapply()?

Viewed 25

I'm having a dataset and want to create a linear regression model for each state (column called fylke). However, when using lapply together with a function, it does say: unexpected '}' I created first a list and used then lapply to loop through the list saving a regression model for each state. I cannot see why my code is wrong since I need to close the "{" with a "}".

Can someone tell me what is wrong with my code?

fylker <- list("Agder", "Innlandet", "Møre og Romsdal", "Nordland", "Rogaland", "Troms og Finnmark",
               "Trøndelag", "Vestfold og Telemark", "Vestland", "Viken")

reg_model <- lapply(fylker, function(x) {
  lm(df$hoyre ~ df$befolkningstetthet, subset = (df$fylke == x)
})

Here is a reproducible example:

structure(list(kommuner = c("Halden", "Moss", "Sarpsborg", "Fredrikstad", 
"Drammen", "Kongsberg", "Ringerike", "Hvaler", "Aremark", "Marker", 
"Indre �\u0098stfold", "Skiptvet", "Rakkestad", "Råde", "Våler (Viken)", 
"Vestby", "Nordre Follo", "�\u0085s", "Frogn", "Nesodden", "Bærum", 
"Asker", "Aurskog-Høland", "Rælingen", "Enebakk", "Lørenskog", 
"Lillestrøm", "Nittedal", "Gjerdrum", "Ullensaker", "Nes", "Eidsvoll", 
"Nannestad", "Hurdal", "Hole", "Flå", "Nesbyen", "Gol", "Hemsedal", 
"�\u0085l", "Hol", "Sigdal", "Krødsherad", "Modum", "�\u0098vre Eiker", 
"Lier", "Flesberg", "Rollag", "Nore og Uvdal", "Jevnaker", "Lunner", 
"Oslo", "Kongsvinger", "Hamar", "Lillehammer", "Gjøvik", "Ringsaker", 
"Løten", "Stange", "Nord-Odal", "Sør-Odal", "Eidskog", "Grue", 
"�\u0085snes", "Våler (Innlandet)", "Elverum", "Trysil", "�\u0085mot", 
"Stor-Elvdal", "Rendalen", "Engerdal", "Tolga", "Tynset", "Alvdal", 
"Folldal", "Os", "Dovre", "Lesja", "Skjåk", "Lom", "Vågå", 
"Nord-Fron", "Sel", "Sør-Fron", "Ringebu", "�\u0098yer", "Gausdal", 
"�\u0098stre Toten", "Vestre Toten", "Gran", "Søndre Land", 
"Nordre Land", "Sør-Aurdal", "Etnedal", "Nord-Aurdal", "Vestre Slidre", 
"�\u0098ystre Slidre", "Vang", "Horten", "Holmestrand"), fylker = c("Viken", 
"Viken", "Viken", "Viken", "Viken", "Viken", "Viken", "Viken", 
"Viken", "Viken", "Viken", "Viken", "Viken", "Viken", "Viken", 
"Viken", "Viken", "Viken", "Viken", "Viken", "Viken", "Viken", 
"Viken", "Viken", "Viken", "Viken", "Viken", "Viken", "Viken", 
"Viken", "Viken", "Viken", "Viken", "Viken", "Viken", "Viken", 
"Viken", "Viken", "Viken", "Viken", "Viken", "Viken", "Viken", 
"Viken", "Viken", "Viken", "Viken", "Viken", "Viken", "Viken", 
"Viken", "Oslo", "Innlandet", "Innlandet", "Innlandet", "Innlandet", 
"Innlandet", "Innlandet", "Innlandet", "Innlandet", "Innlandet", 
"Innlandet", "Innlandet", "Innlandet", "Innlandet", "Innlandet", 
"Innlandet", "Innlandet", "Innlandet", "Innlandet", "Innlandet", 
"Innlandet", "Innlandet", "Innlandet", "Innlandet", "Innlandet", 
"Innlandet", "Innlandet", "Innlandet", "Innlandet", "Innlandet", 
"Innlandet", "Innlandet", "Innlandet", "Innlandet", "Innlandet", 
"Innlandet", "Innlandet", "Innlandet", "Innlandet", "Innlandet", 
"Innlandet", "Innlandet", "Innlandet", "Innlandet", "Innlandet", 
"Innlandet", "Innlandet", "Vestfold og Telemark", "Vestfold og Telemark"
), arbeidsledighet = c(2.9, 2.7, 3.2625, 2.975, 2.5125, 1.8375, 
1.9375, 1.4125, 1.5375, 2.075, 2.5, 1.65, 2.7125, 2.0625, 2.0625, 
1.65, 2.0125, 2.1875, 1.6, 1.95, 1.6375, 1.7625, 1.9625, 1.9375, 
2.125, 2.6875, 2.275, 1.725, 1.7125, 3.2125, 1.7625, 2.1, 2.425, 
2, 1.6625, 1.21666666666667, 1.0625, 0.825, 1.075, 0.675, 1.275, 
0.825, 1.9875, 2, 1.4625, 1.65, 0.925, 1.275, 0.8625, 1.2875, 
1.4375, 2.4, 1.575, 2, 1.875, 1.125, 1.75, 1.4125, 1.9625, 1.7, 
2.025, 1.5875, 2.2625, 2.8125, 1.2, 2.575, 1.8125, 2.6125, 1.8375, 
1.6, 0.9625, 1.225, 1.325, 0.775, 0.7375, 0.9, 1.2, 1.1625, 0.78, 
0.775, 1.24285714285714, 1.525, 1.5, 1.8125, 0.95, 1.225, 1.3375, 
0.95, 1.525, 1.6, 1.6125, 2.025, 1.575, 0.7125, 1.4, 1.45, 0.95, 
0.975, 3.1, 2.2125), utdanningsniva = c(28.9, 32, 26.4, 31.4, 
32.8, 39.1, 28.4, 30.8, 20.4, 21, 23.8, 20, 20.2, 27.4, 26.4, 
35.5, 42.5, 46.3, 39.2, 48.6, 54.6, 46.2, 21, 34.4, 25.7, 36.8, 
33.5, 38.2, 32.2, 29.3, 24.1, 27.5, 24.8, 22.6, 40.7, 22.1, 24.3, 
23.6, 33.6, 27.5, 27.7, 18.4, 23.9, 23.3, 26.9, 37.6, 22.7, 23.6, 
20.1, 24.8, 26.8, 54, 25.1, 40.5, 42.6, 32.2, 25.8, 23.6, 30.7, 
18.2, 21.6, 17.8, 19.2, 20.4, 19.6, 31.7, 23.2, 24.2, 25.1, 21.1, 
20.7, 33.1, 31.2, 24, 26.6, 27.5, 21, 24.8, 22.1, 23.9, 22.3, 
21.7, 21.3, 21.5, 21.7, 26.3, 23, 25.3, 23, 23.6, 21.3, 19.6, 
21.4, 22.1, 25.2, 23.7, 26.6, 28, 33.9, 29.2), befolkningstetthet = c(53, 
388, 155, 293, 334, 37, 22, 52, 5, 10, 60, 41, 20, 72, 24, 136, 
306, 202, 188, 325, 678, 261, 17, 334, 57, 638, 212, 137, 86, 
162, 38, 67, 45, 11, 50, 2, 4, 9, 4, 4, 3, 4, 7, 31, 47, 96, 
5, 3, 1, 35, 33, 1636, 19, 93, 63, 48, 31, 21, 33, 11, 17, 10, 
6, 7, 5, 18, 2, 3, 1, 1, 1, 1, 3, 3, 1, 2, 2, 1, 1, 1, 3, 5, 
6, 4, 4, 8, 5, 31, 58, 21, 8, 7, 3, 3, 7, 5, 4, 1, 399, 61), 
    innvandrere = c(15.74, 21.02, 21.31, 19.51, 28.5, 17.21, 
    16.74, 5.94, 4.83, 10.05, 19.85, 11.84, 15.39, 14.81, 16.73, 
    19, 19.85, 24.34, 13.76, 15.7, 22.74, 20.79, 16.07, 27.11, 
    20.12, 32.85, 27.79, 19.74, 17.86, 30.22, 16.36, 19.25, 25.74, 
    11.21, 15.55, 16.21, 12.54, 17.77, 26.32, 11.21, 18.09, 9.81, 
    14.47, 13.23, 16.05, 23.16, 11.39, 8.37, 10.45, 12.12, 13.04, 
    33.75, 14.4, 14.55, 14.01, 15.98, 11.22, 10.11, 12.46, 5.32, 
    9.62, 9.33, 9.46, 8.04, 8.75, 12.12, 13.07, 8.94, 12.66, 
    8.1, 9.04, 14.78, 12.84, 8.48, 6.32, 12.89, 8.84, 7.73, 6.32, 
    5.44, 6.23, 8.33, 6.97, 11.98, 11.34, 15.06, 7.72, 10.36, 
    10.78, 11.32, 9.95, 8.02, 8.23, 8.43, 10.82, 10.09, 12.3, 
    17.1, 16.78, 14.78), hoyre = c(16.8, 23.1, 16.4, 18.7, 24.6, 
    19.8, 19.8, 23.1, 10.1, 12, 19.1, 12.9, 12.3, 22.2, 18.6, 
    24.5, 28.6, 19.2, 33.7, 20, 40.3, 33.2, 12.8, 22.1, 20.6, 
    24.4, 22.6, 24.1, 24.1, 22.1, 13.4, 17, 17.1, 12.6, 31.6, 
    17.8, 15.1, 20.1, 20.5, 13.8, 23, 14.1, 17.3, 14.3, 19.8, 
    29.6, 13.2, 7.5, 11.4, 17.4, 16.9, 23.5, 12.6, 14.8, 16.4, 
    15.9, 11.7, 6.8, 9.2, 6.5, 11.2, 10.3, 8.6, 7.1, 5.5, 11.6, 
    10.6, 8.8, 7, 4.9, 8.3, 3.9, 6.3, 8.5, 4.3, 3, 7.2, 5, 4.3, 
    4.6, 5.2, 9.2, 7.8, 5.7, 7, 9.1, 6.2, 11.7, 12.9, 17, 10.7, 
    9.2, 9.7, 9, 14.9, 10.8, 13.1, 11.5, 21.5, 21.9)), row.names = c(NA, 
-100L), class = c("tbl_df", "tbl", "data.frame"))

Thank you!

1 Answers

The subset can be

lapply(fylker, function(x) {
  tryCatch({lm(hoyre ~ befolkningstetthet, data =df, 
       subset = fylker == x)}, error = function(e) NA)

})

-output

[[1]]
[1] NA

[[2]]

Call:
lm(formula = hoyre ~ befolkningstetthet, data = df, subset = fylker == 
    x)

Coefficients:
       (Intercept)  befolkningstetthet  
            7.5367              0.1172  


[[3]]
[1] NA

[[4]]
[1] NA

[[5]]
[1] NA

[[6]]
[1] NA

[[7]]
[1] NA

[[8]]

Call:
lm(formula = hoyre ~ befolkningstetthet, data = df, subset = fylker == 
    x)

Coefficients:
       (Intercept)  befolkningstetthet  
         21.972189           -0.001183  


[[9]]
[1] NA

[[10]]

Call:
lm(formula = hoyre ~ befolkningstetthet, data = df, subset = fylker == 
    x)

Coefficients:
       (Intercept)  befolkningstetthet  
          16.73236             0.02618  

Related