{"id":1309,"date":"2022-02-14T16:25:24","date_gmt":"2022-02-14T15:25:24","guid":{"rendered":"https:\/\/microdata.spirehost.no\/?post_type=eksempel&#038;p=1309"},"modified":"2023-08-18T13:31:05","modified_gmt":"2023-08-18T12:31:05","slug":"how-to-aggregate-and-create-municipal-level-contextual-data","status":"publish","type":"eksempel","link":"https:\/\/www.microdata.no\/en\/eksempel\/how-to-aggregate-and-create-municipal-level-contextual-data\/","title":{"rendered":"How to aggregate and create municipal level contextual data"},"content":{"rendered":"\n<p>The example below shows how to create aggregated data at municipality level (contextual data) for use in a simple regression analysis.<\/p>\n\n\n\n<p>You begin by creating a regular personal data set with selected information, including a variable to be used as a link key for the aggregated data, in this case the variable <em>municipality<\/em> (actual municipality of residence). Two new data sets are then created which are used to retrieve wages\/wealth and unemployment\/residence figures at municipal level.<\/p>\n\n\n\n<p>The method to aggregate is to use the command <code>collapse()<\/code>, where one uses the option <code>by()<\/code> to specify the level at which one should aggregate (<em>municipality<\/em>). Then the command <code>merge<\/code> is used to connect the aggregated data on the personal data set via the provided link key (<em>municipality<\/em>).<\/p>\n\n\n\n<p>Remember to rename the aggregated variables before connecting them to the personal data set.<\/p>\n\n\n\n<p>The script is used as part of the thematic course on contextual data, where working with family data is also demonstrated. <a href=\"http:\/\/microdata.no\/kurs\">Click here for more about our courses.<\/a><\/p>\n\n\n<div id=\"rose-block_53df1040dab92edfcb2ef5c701b72c7e\" class=\"rose-code codeblock-wrapper\">\n<pre tabindex=\"0\" class=\"codeblock\"><code>\r\ntextblock\r\nHow to link contextual data into a person dataset\r\n-----------------------------------------------------\r\nendblock\r\n\r\n\/\/Connect to database\r\nrequire no.ssb.fdb:23 as db\r\n\r\n\/\/Create person dataset\r\ncreate-dataset persons\r\nimport db\/BEFOLKNING_KOMMNR_FAKTISK 2021-01-01 as municipality\r\nimport db\/BEFOLKNING_KJOENN as gender\r\nimport db\/INNTEKT_WLONN 2021-01-01 as wage\r\nimport db\/INNTEKT_BER_BRFORM 2021-01-01 as wealth\r\nsummarize wage wealth\r\nhistogram wage, freq\r\nhistogram wealth, freq\r\n\r\n\/\/Create figures for meanwage og meanwealth per municipality and link into person dataset\r\ncreate-dataset municipalitydata_wage_wealth\r\nimport db\/BEFOLKNING_KOMMNR_FAKTISK 2021-01-01 as municipality\r\nimport db\/INNTEKT_WLONN 2021-01-01 as wage\r\nimport db\/INNTEKT_BER_BRFORM 2021-01-01 as wealth\r\ncollapse (mean) wage wealth, by(municipality)\r\nrename wage meanwage_municipality\r\nrename wealth meanwealth_municipality\r\nsummarize meanwage_municipality meanwealth_municipality\r\nmerge meanwage_municipality meanwealth_municipality into persons on municipality\r\n\r\n\/\/Create figures for number of unemployed and residents per municipality and link into person dataset\r\ncreate-dataset municipalitydata_unempl_resident\r\nimport db\/BEFOLKNING_KOMMNR_FAKTISK 2021-01-01 as municipality\r\nimport db\/ARBSOEK2001FDT_HOVED 2021-01-01 as ws_status\r\ngenerate unemployed = 1 if ws_status == '1'\r\ngenerate resident = 1\r\ncollapse (sum) unemployed resident, by(municipality)\r\nsummarize unemployed resident\r\nmerge unemployed resident into persons on municipality\r\n\r\n\/\/Use the person dataset to perform a simple regression analysis\r\nuse persons\r\ngenerate unempl_pst = (unemployed \/ resident) * 100\r\n\r\ngenerate male = 0\r\nreplace male = 1 if gender == '1'\r\n\r\ngenerate oslo = 0\r\nreplace oslo = 1 if municipality == '0301'\r\n\r\ngenerate bigmunicipality = 0\r\nreplace bigmunicipality = 1 if resident > 80000\r\n\r\ngenerate rich_municipality = 0\r\nreplace rich_municipality = 1 if meanwealth_municipality > 2000000\r\n\r\nregress wage male oslo wealth meanwealth_municipality meanwage_municipality unempl_pst resident\r\nregress wage male oslo wealth rich_municipality meanwage_municipality unempl_pst bigmunicipality<\/code><\/pre>\n<\/div>","protected":false},"parent":0,"menu_order":107,"template":"","meta":{"_acf_changed":false,"inline_featured_image":false,"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":""},"class_list":["post-1309","eksempel","type-eksempel","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to aggregate and create municipal level contextual data - microdata.no<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.microdata.no\/eksempel\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to aggregate and create municipal level contextual data - microdata.no\" \/>\n<meta property=\"og:description\" content=\"The example below shows how to create aggregated data at municipality level (contextual data) for use in a simple regression analysis. You begin by creating a regular personal data set with selected information, including a variable to be used as a link key for the aggregated data, in this case the variable municipality (actual municipality...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.microdata.no\/eksempel\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\/\" \/>\n<meta property=\"og:site_name\" content=\"microdata.no\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-18T12:31:05+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\\\/\",\"url\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\\\/\",\"name\":\"How to aggregate and create municipal level contextual data - microdata.no\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.microdata.no\\\/#website\"},\"datePublished\":\"2022-02-14T15:25:24+00:00\",\"dateModified\":\"2023-08-18T12:31:05+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.microdata.no\\\/eksempel\\\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Hjem\",\"item\":\"https:\\\/\\\/www.microdata.no\\\/en\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to aggregate and create municipal level contextual data\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.microdata.no\\\/#website\",\"url\":\"https:\\\/\\\/www.microdata.no\\\/\",\"name\":\"microdata.no\",\"description\":\"Gj\u00f8r det enklere \u00e5 analysere registerdata\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.microdata.no\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to aggregate and create municipal level contextual data - microdata.no","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.microdata.no\/eksempel\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\/","og_locale":"en_US","og_type":"article","og_title":"How to aggregate and create municipal level contextual data - microdata.no","og_description":"The example below shows how to create aggregated data at municipality level (contextual data) for use in a simple regression analysis. You begin by creating a regular personal data set with selected information, including a variable to be used as a link key for the aggregated data, in this case the variable municipality (actual municipality...","og_url":"https:\/\/www.microdata.no\/eksempel\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\/","og_site_name":"microdata.no","article_modified_time":"2023-08-18T12:31:05+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.microdata.no\/eksempel\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\/","url":"https:\/\/www.microdata.no\/eksempel\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\/","name":"How to aggregate and create municipal level contextual data - microdata.no","isPartOf":{"@id":"https:\/\/www.microdata.no\/#website"},"datePublished":"2022-02-14T15:25:24+00:00","dateModified":"2023-08-18T12:31:05+00:00","breadcrumb":{"@id":"https:\/\/www.microdata.no\/eksempel\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.microdata.no\/eksempel\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.microdata.no\/eksempel\/hvordan-aggregere-og-lage-kontekstuelle-data-pa-kommuneniva\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Hjem","item":"https:\/\/www.microdata.no\/en\/"},{"@type":"ListItem","position":2,"name":"How to aggregate and create municipal level contextual data"}]},{"@type":"WebSite","@id":"https:\/\/www.microdata.no\/#website","url":"https:\/\/www.microdata.no\/","name":"microdata.no","description":"Gj\u00f8r det enklere \u00e5 analysere registerdata","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.microdata.no\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"taxonomy_info":[],"featured_image_src_large":[],"author_info":[],"comment_info":"","_links":{"self":[{"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/eksempel\/1309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/eksempel"}],"about":[{"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/types\/eksempel"}],"wp:attachment":[{"href":"https:\/\/www.microdata.no\/en\/wp-json\/wp\/v2\/media?parent=1309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}