{"id":1546,"date":"2023-07-25T22:47:19","date_gmt":"2023-07-25T22:47:19","guid":{"rendered":"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/"},"modified":"2023-07-25T22:47:19","modified_gmt":"2023-07-25T22:47:19","slug":"glm-vs-lm-em-r","status":"publish","type":"post","link":"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/","title":{"rendered":"A diferen\u00e7a entre glm e lm em r"},"content":{"rendered":"<p><\/p>\n<hr>\n<p><span style=\"color: #000000;\">A linguagem de programa\u00e7\u00e3o R fornece as seguintes fun\u00e7\u00f5es para ajuste de modelos lineares:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>1. lm \u2013 Usado para ajustar modelos lineares<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Esta fun\u00e7\u00e3o usa a seguinte sintaxe:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>lm(f\u00f3rmula, dados,\u2026)<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Ouro:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>f\u00f3rmula:<\/strong> A f\u00f3rmula do modelo linear (por exemplo, y ~ x1 + x2)<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>dados:<\/strong> o nome do bloco de dados que cont\u00e9m os dados<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\"><strong>2. glm \u2013 Usado para ajustar modelos lineares generalizados<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Esta fun\u00e7\u00e3o usa a seguinte sintaxe:<\/span><\/p>\n<p> <span style=\"color: #000000;\"><strong>glm(f\u00f3rmula, fam\u00edlia=Gaussiano, dados,\u2026)<\/strong><\/span><\/p>\n<p> <span style=\"color: #000000;\">Ouro:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\"><strong>f\u00f3rmula:<\/strong> A f\u00f3rmula do modelo linear (por exemplo, y ~ x1 + x2)<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>fam\u00edlia:<\/strong> a fam\u00edlia estat\u00edstica a ser usada para ajustar o modelo. O padr\u00e3o \u00e9 Gaussiano, mas outras op\u00e7\u00f5es incluem Binomial, Gamma e Poisson, entre outras.<\/span><\/li>\n<li> <span style=\"color: #000000;\"><strong>dados:<\/strong> o nome do bloco de dados que cont\u00e9m os dados<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Observe que a \u00fanica diferen\u00e7a entre essas duas fun\u00e7\u00f5es \u00e9 o argumento <strong>family<\/strong> inclu\u00eddo na fun\u00e7\u00e3o <strong>glm()<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">Se voc\u00ea usar lm() ou glm() para ajustar um modelo de regress\u00e3o linear, <strong>eles produzir\u00e3o exatamente os mesmos resultados<\/strong> .<\/span><\/p>\n<p> <span style=\"color: #000000;\">No entanto, a fun\u00e7\u00e3o glm() tamb\u00e9m pode ser usada para ajustar modelos mais complexos, como:<\/span><\/p>\n<ul>\n<li> <span style=\"color: #000000;\">Regress\u00e3o log\u00edstica (fam\u00edlia=binomial)<\/span><\/li>\n<li> <span style=\"color: #000000;\"><a href=\"https:\/\/statorials.org\/pt\/regressao-de-peixe\/\" target=\"_blank\" rel=\"noopener\">Regress\u00e3o de Poisson<\/a> (fam\u00edlia=peixe)<\/span><\/li>\n<\/ul>\n<p> <span style=\"color: #000000;\">Os exemplos a seguir mostram como usar as fun\u00e7\u00f5es lm() e glm() na pr\u00e1tica.<\/span><\/p>\n<h3> <span style=\"color: #000000;\"><strong>Exemplo de uso da fun\u00e7\u00e3o lm()<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como ajustar um <strong>modelo de regress\u00e3o linear<\/strong> usando a fun\u00e7\u00e3o lm():<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#fit multiple linear regression model\n<\/span>model &lt;- lm(mpg ~ disp + hp, data=mtcars)\n\n<span style=\"color: #008080;\">#view model summary\n<\/span>summary(model)\n\nCall:\nlm(formula = mpg ~ disp + hp, data = mtcars)\n\nResiduals:\n    Min 1Q Median 3Q Max \n-4.7945 -2.3036 -0.8246 1.8582 6.9363 \n\nCoefficients:\n             Estimate Std. Error t value Pr(&gt;|t|)    \n(Intercept) 30.735904 1.331566 23.083 &lt; 2nd-16 ***\navailable -0.030346 0.007405 -4.098 0.000306 ***\nhp -0.024840 0.013385 -1.856 0.073679 .  \n---\nSignificant. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\nResidual standard error: 3.127 on 29 degrees of freedom\nMultiple R-squared: 0.7482, Adjusted R-squared: 0.7309 \nF-statistic: 43.09 on 2 and 29 DF, p-value: 2.062e-09<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Exemplos de uso da fun\u00e7\u00e3o glm()<\/strong><\/span><\/h3>\n<p> <span style=\"color: #000000;\">O c\u00f3digo a seguir mostra como ajustar exatamente o mesmo <strong>modelo de regress\u00e3o linear<\/strong> usando a fun\u00e7\u00e3o glm():<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#fit multiple linear regression model\n<\/span>model &lt;- glm(mpg ~ disp + hp, data=mtcars)\n\n<span style=\"color: #008080;\">#view model summary\n<\/span>summary(model)\n\nCall:\nglm(formula = mpg ~ disp + hp, data = mtcars)\n\nDeviance Residuals: \n    Min 1Q Median 3Q Max  \n-4.7945 -2.3036 -0.8246 1.8582 6.9363  \n\nCoefficients:\n             Estimate Std. Error t value Pr(&gt;|t|)    \n(Intercept) 30.735904 1.331566 23.083 &lt; 2nd-16 ***\navailable -0.030346 0.007405 -4.098 0.000306 ***\nhp -0.024840 0.013385 -1.856 0.073679 .  \n---\nSignificant. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\n(Dispersion parameter for gaussian family taken to be 9.775636)\n\n    Null deviance: 1126.05 on 31 degrees of freedom\nResidual deviance: 283.49 on 29 degrees of freedom\nAIC: 168.62\n\nNumber of Fisher Scoring iterations: 2<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Observe que as estimativas dos coeficientes e os erros padr\u00e3o das estimativas dos coeficientes s\u00e3o exatamente iguais aos produzidos pela fun\u00e7\u00e3o lm().<\/span><\/p>\n<p> <span style=\"color: #000000;\">Observe que tamb\u00e9m podemos usar a fun\u00e7\u00e3o glm() para ajustar um <strong>modelo de regress\u00e3o log\u00edstica<\/strong> especificando family=binomial da seguinte forma:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#fit logistic regression model\n<\/span>model &lt;- glm(am ~ disp + hp, data=mtcars, family=binomial)\n\n<span style=\"color: #008080;\">#view model summary\n<\/span>summary(model)\n\nCall:\nglm(formula = am ~ disp + hp, family = binomial, data = mtcars)\n\nDeviance Residuals: \n    Min 1Q Median 3Q Max  \n-1.9665 -0.3090 -0.0017 0.3934 1.3682  \n\nCoefficients:\n            Estimate Std. Error z value Pr(&gt;|z|)  \n(Intercept) 1.40342 1.36757 1.026 0.3048  \navailable -0.09518 0.04800 -1.983 0.0474 *\nhp 0.12170 0.06777 1.796 0.0725 .\n---\nSignificant. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\n(Dispersion parameter for binomial family taken to be 1)\n\n    Null deviance: 43,230 on 31 degrees of freedom\nResidual deviance: 16,713 on 29 degrees of freedom\nAIC: 22,713\n\nNumber of Fisher Scoring iterations: 8\n<\/strong><\/pre>\n<p> <span style=\"color: #000000;\">Tamb\u00e9m podemos usar a fun\u00e7\u00e3o glm() para ajustar um <strong>modelo de regress\u00e3o de Poisson<\/strong> especificando family=poisson da seguinte forma:<\/span><\/p>\n<pre style=\"background-color: #ececec; font-size: 15px;\"> <strong><span style=\"color: #008080;\">#fit Poisson regression model\n<\/span>model &lt;- glm(am ~ disp + hp, data=mtcars, family=fish)\n\n<span style=\"color: #008080;\">#view model summary\n<\/span>summary(model)\n\nCall:\nglm(formula = am ~ disp + hp, family = fish, data = mtcars)\n\nDeviance Residuals: \n    Min 1Q Median 3Q Max  \n-1.1266 -0.4629 -0.2453 0.1797 1.5428  \n\nCoefficients:\n             Estimate Std. Error z value Pr(&gt;|z|)   \n(Intercept) 0.214255 0.593463 0.361 0.71808   \navailable -0.018915 0.007072 -2.674 0.00749 **\nhp 0.016522 0.007163 2.307 0.02107 * \n---\nSignificant. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1\n\n(Dispersion parameter for fish family taken to be 1)\n\n    Null deviance: 23,420 on 31 degrees of freedom\nResidual deviance: 10,526 on 29 degrees of freedom\nAIC: 42,526\n\nNumber of Fisher Scoring iterations: 6\n<\/strong><\/pre>\n<h3> <span style=\"color: #000000;\"><strong>Recursos adicionais<\/strong><\/span><\/h3>\n<p> <a href=\"https:\/\/statorials.org\/pt\/regressao-linear-simples-em-r\/\" target=\"_blank\" rel=\"noopener\">Como realizar regress\u00e3o linear simples em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/regressao-linear-multipla-r\/\" target=\"_blank\" rel=\"noopener\">Como realizar regress\u00e3o linear m\u00faltipla em R<\/a><br \/> <a href=\"https:\/\/statorials.org\/pt\/r-glm-prever\/\" target=\"_blank\" rel=\"noopener\">Como usar a fun\u00e7\u00e3o de previs\u00e3o com glm em R<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A linguagem de programa\u00e7\u00e3o R fornece as seguintes fun\u00e7\u00f5es para ajuste de modelos lineares: 1. lm \u2013 Usado para ajustar modelos lineares Esta fun\u00e7\u00e3o usa a seguinte sintaxe: lm(f\u00f3rmula, dados,\u2026) Ouro: f\u00f3rmula: A f\u00f3rmula do modelo linear (por exemplo, y ~ x1 + x2) dados: o nome do bloco de dados que cont\u00e9m os dados [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-1546","post","type-post","status-publish","format-standard","hentry","category-guia"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>A diferen\u00e7a entre glm e lm em R<\/title>\n<meta name=\"description\" content=\"Este tutorial explica a diferen\u00e7a entre as fun\u00e7\u00f5es glm e lm em R, com v\u00e1rios exemplos.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/\" \/>\n<meta property=\"og:locale\" content=\"pt_PT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A diferen\u00e7a entre glm e lm em R\" \/>\n<meta property=\"og:description\" content=\"Este tutorial explica a diferen\u00e7a entre as fun\u00e7\u00f5es glm e lm em R, com v\u00e1rios exemplos.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/\" \/>\n<meta property=\"og:site_name\" content=\"Statorials\" \/>\n<meta property=\"article:published_time\" content=\"2023-07-25T22:47:19+00:00\" \/>\n<meta name=\"author\" content=\"Dr. benjamim anderson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"Dr. benjamim anderson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tempo estimado de leitura\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/\",\"url\":\"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/\",\"name\":\"A diferen\u00e7a entre glm e lm em R\",\"isPartOf\":{\"@id\":\"https:\/\/statorials.org\/pt\/#website\"},\"datePublished\":\"2023-07-25T22:47:19+00:00\",\"dateModified\":\"2023-07-25T22:47:19+00:00\",\"author\":{\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\"},\"description\":\"Este tutorial explica a diferen\u00e7a entre as fun\u00e7\u00f5es glm e lm em R, com v\u00e1rios exemplos.\",\"breadcrumb\":{\"@id\":\"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/#breadcrumb\"},\"inLanguage\":\"pt-PT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Lar\",\"item\":\"https:\/\/statorials.org\/pt\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A diferen\u00e7a entre glm e lm em r\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/statorials.org\/pt\/#website\",\"url\":\"https:\/\/statorials.org\/pt\/\",\"name\":\"Statorials\",\"description\":\"O seu guia para a literacia estat\u00edstica!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/statorials.org\/pt\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"pt-PT\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666\",\"name\":\"Dr. benjamim anderson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-PT\",\"@id\":\"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"contentUrl\":\"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg\",\"caption\":\"Dr. benjamim anderson\"},\"description\":\"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais\",\"sameAs\":[\"https:\/\/statorials.org\/pt\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A diferen\u00e7a entre glm e lm em R","description":"Este tutorial explica a diferen\u00e7a entre as fun\u00e7\u00f5es glm e lm em R, com v\u00e1rios exemplos.","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:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/","og_locale":"pt_PT","og_type":"article","og_title":"A diferen\u00e7a entre glm e lm em R","og_description":"Este tutorial explica a diferen\u00e7a entre as fun\u00e7\u00f5es glm e lm em R, com v\u00e1rios exemplos.","og_url":"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/","og_site_name":"Statorials","article_published_time":"2023-07-25T22:47:19+00:00","author":"Dr. benjamim anderson","twitter_card":"summary_large_image","twitter_misc":{"Escrito por":"Dr. benjamim anderson","Tempo estimado de leitura":"4 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/","url":"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/","name":"A diferen\u00e7a entre glm e lm em R","isPartOf":{"@id":"https:\/\/statorials.org\/pt\/#website"},"datePublished":"2023-07-25T22:47:19+00:00","dateModified":"2023-07-25T22:47:19+00:00","author":{"@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666"},"description":"Este tutorial explica a diferen\u00e7a entre as fun\u00e7\u00f5es glm e lm em R, com v\u00e1rios exemplos.","breadcrumb":{"@id":"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/#breadcrumb"},"inLanguage":"pt-PT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/statorials.org\/pt\/glm-vs-lm-em-r\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Lar","item":"https:\/\/statorials.org\/pt\/"},{"@type":"ListItem","position":2,"name":"A diferen\u00e7a entre glm e lm em r"}]},{"@type":"WebSite","@id":"https:\/\/statorials.org\/pt\/#website","url":"https:\/\/statorials.org\/pt\/","name":"Statorials","description":"O seu guia para a literacia estat\u00edstica!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/statorials.org\/pt\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"pt-PT"},{"@type":"Person","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/e08f98e8db95e0aa9c310e1b27c9c666","name":"Dr. benjamim anderson","image":{"@type":"ImageObject","inLanguage":"pt-PT","@id":"https:\/\/statorials.org\/pt\/#\/schema\/person\/image\/","url":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","contentUrl":"https:\/\/statorials.org\/pt\/wp-content\/uploads\/2023\/10\/Dr.-Benjamin-Anderson-96x96.jpg","caption":"Dr. benjamim anderson"},"description":"Ol\u00e1, sou Benjamin, um professor aposentado de estat\u00edstica que se tornou professor dedicado na Statorials. Com vasta experi\u00eancia e conhecimento na \u00e1rea de estat\u00edstica, estou empenhado em compartilhar meu conhecimento para capacitar os alunos por meio de Statorials. Saber mais","sameAs":["https:\/\/statorials.org\/pt"]}]}},"yoast_meta":{"yoast_wpseo_title":"","yoast_wpseo_metadesc":"","yoast_wpseo_canonical":""},"_links":{"self":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/1546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/comments?post=1546"}],"version-history":[{"count":0,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/posts\/1546\/revisions"}],"wp:attachment":[{"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/media?parent=1546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/categories?post=1546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/statorials.org\/pt\/wp-json\/wp\/v2\/tags?post=1546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}