Polynomiale (polynomielle) Regression mit poly

Modelle zur Korrelations- und Regressionsanalyse

Moderator: EDi

Werekorden
Beiträge: 83
Registriert: So Feb 04, 2018 7:52 pm

Polynomiale (polynomielle) Regression mit poly

Beitrag von Werekorden »

Hi

Ich versuche gerade eine polynomiale Regression für unsere Linearitätsmessungen um das beste Fitting zu erhalten und zu überprüfen ob Linearität vorhanden ist und wenn nicht wie stark es abweicht.

Ich habe folgenden Code :

Code: Alles auswählen

#----Polynomiale Regression----
y<-c(20, 40, 60, 80, 100)
x<-c(26.35, 138.5, 271, 340, 406.5)
sample1 <- data.frame(y, x)
sample1
plot(sample1$x, sample1$y, type = "b")

fit1<- lm(sample1$x ~ sample1$y)
summary(fit1)

fit2<- lm(sample1$x ~ poly(sample1$y, 2, raw=TRUE))
summary(fit2)

fit3<- lm(sample1$x ~ poly(sample1$y, 3, raw=TRUE))
summary(fit3)

plot(sample1$x, sample1$y, type = "l", lwd=2)
points(sample1$x, predict(fit2), type="l", col="red", lwd=2)
points(sample1$x, predict(fit3), type="l", col="blue", lwd=2)
Leider geben die Plots Regressionsmodelle zweiten und dritten Grades irgendwie seltsame Graphen aus? Hat da jemand eine Idee.? Ich komme gerade nicht weiter.

Danke euch.

Andreas
Athomas
Beiträge: 768
Registriert: Mo Feb 26, 2018 8:19 pm

Re: Polynomiale (polynomielle) Regression mit poly

Beitrag von Athomas »

Ich hoffe, das sind keine Nachwirkungen des 1. April - aber Du hast die "vertrauten" Rollen von x und y vertauscht!
Dem solltest Du auch in Deinen Plots Rechnung tragen...

Code: Alles auswählen

plot(sample1$y, sample1$x, type = "l", lwd=2)
points(sample1$y, predict(fit2), type="l", col="red", lwd=2)
points(sample1$y, predict(fit3), type="l", col="blue", lwd=2)
Linien.jpeg
Werekorden
Beiträge: 83
Registriert: So Feb 04, 2018 7:52 pm

Re: Polynomiale (polynomielle) Regression mit poly

Beitrag von Werekorden »

Hi,

ja das habe ich mittlerweile auch herausgefunden. Ich versuche gerade ein CLSI-Beispiel nachzustellen damit wir das dann nutzen können für unserer Linearitäts-Austestungen.

Leider scheint es einen Fehler in dem CLSi-Dokument zu geben. Die bekommen ein anderes b1 raus als R bei der BErechnung der lm ersten Grades.

Ich habs mal in ggplot gemacht mit Formel :roll: :?: aber jetzt steh ich wieder auf dem Schlauch mit dem Überlagern der Regressionsmodellen zweiten und dritten Grades.

Code: Alles auswählen



library(Rcpp)
library(rlang)
library(gcookbook)
library(ggplot2)
library(hrbrthemes)
library(magick)
library(plyr)
library(dplyr)
library(MASS)
library(scales)
library(Cairo)
library(extrafont)


x<-c(20, 20, 40, 40, 60, 60, 80, 80, 100, 100)
y<-c(26.5, 26.2, 139, 138, 269, 273, 337, 343, 409, 404)
sample1 <- data.frame(y, x)
sample1
plot(sample1$x, sample1$y, type = "l")

fit1<- lm(sample1$y ~ sample1$x)
summary(fit1)

fit2<- lm(sample1$y ~ poly(sample1$x, 2, raw=TRUE))
summary(fit2)

fit3<- lm(sample1$y ~ poly(sample1$x, 3, raw=TRUE))
summary(fit3)

plot(sample1$x, sample1$y, type = "l", lwd=1, main = "Vergleich Polynomiale Regression", xlab = "Konzentration", ylab = "Wert")
points(sample1$x, predict(fit1), type="l", lty=2, col="black", lwd=1)
points(sample1$x, predict(fit2), type="l", lty=1, col="red", lwd=2)
points(sample1$x, predict(fit3), type="l", lty=3, col="blue", lwd=2)


#--------Formelerstellung------------
eqn <- as.character(as.expression(substitute(italic(y)==a+b*italic(x)*"," ~~ italic(r)^2~ "=" ~ r2,
                                             list(a  = format(summary(fit1)$coefficients[1,1], digits = 4), 
                                                  b  = format(summary(fit1)$coefficients[2,1], digits = 4), 
                                                  r2 = format(summary(fit1)$r.squared, digits = 2)
                                             ))))
eqn
parse(text = eqn)

#---------ggPlot-Erstellung----------------
point <- ggplot(sample1, aes(x=x, y=y)) 
point + geom_point() + stat_smooth(method=lm, se=0.99, fullrange = TRUE, colour="purple") +
  labs(title = 'Regressions Plot CLSI EP06-A Example 1 Appendix C',
       subtitle = 'Polynomial regression Plot to test Linearity',
       x = 'Concentration',
       y = 'Ct-value',
       caption = 'Andreas / Andreas@random.com') + 
  annotate("text", label = eqn, parse = TRUE, x=50 , y=400) +
  theme_ipsum() + 
  theme(plot.title = element_text(color = "#3a4fa7ff"), plot.caption = element_text(color = "#3a4fa7ff", face = 'bold'))
Athomas
Beiträge: 768
Registriert: Mo Feb 26, 2018 8:19 pm

Re: Polynomiale (polynomielle) Regression mit poly

Beitrag von Athomas »

ja das habe ich mittlerweile auch herausgefunden.
Ich habs mal in ggplot gemacht mit Formel :roll: :?: aber jetzt steh ich wieder auf dem Schlauch mit dem Überlagern der Regressionsmodellen zweiten und dritten Grades.
Ach, das kriegst Du bestimmt auch wieder selber raus :? !
Werekorden
Beiträge: 83
Registriert: So Feb 04, 2018 7:52 pm

Re: Polynomiale (polynomielle) Regression mit poly

Beitrag von Werekorden »

Ich werde es versuchen und morgen dann weinend zurückkehren und voller Demut rumjammern. :oops: ;)
Athomas
Beiträge: 768
Registriert: Mo Feb 26, 2018 8:19 pm

Re: Polynomiale (polynomielle) Regression mit poly

Beitrag von Athomas »

Ich werde es versuchen und morgen dann weinend zurückkehren und voller Demut rumjammern.
Genau so :) !
Jetzt sag mir bitte noch, was CLSI bedeutet!?
Werekorden
Beiträge: 83
Registriert: So Feb 04, 2018 7:52 pm

Re: Polynomiale (polynomielle) Regression mit poly

Beitrag von Werekorden »

Hi,

ds ist eine Organisation für Standards. Ähnlich ISO. DIN EN Iso referenziert gerne auf CLSI:

Hier die ersten Seiten des Dokuments, dass ich meine.
CLSI EP06-A
Werekorden
Beiträge: 83
Registriert: So Feb 04, 2018 7:52 pm

Re: Polynomiale (polynomielle) Regression mit poly

Beitrag von Werekorden »

Hi,

ALSO! Ich hab es so halb hinbekommen:

Code: Alles auswählen

#---Polynomiale Regression Einzelwerte aus Beispiel 1 Appendix C CSLI EP06-A----
#---Dateneingabe----
x<-c(20, 20, 40, 40, 60, 60, 80, 80, 100, 100)
y<-c(26.5, 26.2, 139, 138, 269, 273, 337, 343, 409, 404)
sample1 <- data.frame(y, x)
sample1
plot(sample1$x, sample1$y, type = "l")

#---Berechnung Regressionsmodelle (linear, quadratisch "2" und kubisch "3")----
fit1<- lm(sample1$y ~ sample1$x)
summary(fit1)

fit2<- lm(sample1$y ~ poly(sample1$x, 2, raw=TRUE))
summary(fit2)

fit3<- lm(sample1$y ~ poly(sample1$x, 3, raw=TRUE))
summary(fit3)
#---Formelerstellung lineare Regression für ggplot----
eqn <- as.character(as.expression(substitute(italic(y)==a+b*italic(x)*"," ~~ italic(r)^2~ "=" ~ r2,
                                             list(a  = format(summary(fit1)$coefficients[1,1], digits = 4), 
                                                  b  = format(summary(fit1)$coefficients[2,1], digits = 4), 
                                                  r2 = format(summary(fit1)$r.squared, digits = 2)
                                             ))))
eqn
parse(text = eqn)

#---Plot-Erstellung mit ggplot2---------
point <- ggplot(sample1, aes(x=x, y=y)) 
point + geom_point() + 
  geom_line(color='red',data =fit2, aes(x=x, y=y)) +
  geom_line(color='blue',data =fit3, aes(x=x, y=y)) +
  stat_smooth(method=lm, se=0.99, fullrange = TRUE, colour="purple") +
  labs(title = 'Regressions Plot CLSI EP06-A Example 1 Appendix C',
       subtitle = 'Polynomial regression Plot to test Linearity',
       x = 'Concentration',
       y = 'Ct-value',
       caption = 'Andreas / andreas@random.com') + 
  annotate("text", label = eqn, parse = TRUE, x=50 , y=400) +
  theme_ipsum() + 
  theme(plot.title = element_text(color = "#3a4fa7ff"), plot.caption = element_text(color = "#3a4fa7ff", face = 'bold'))
Tja nur das das quadratische Regression eine punktgenaue Linie erstellt und keine smooth Version wie bei der plot Funktion.

Mühsam ernährt sich das Eichhörnchen.
bigben
Beiträge: 2771
Registriert: Mi Okt 12, 2016 9:09 am

Re: Polynomiale (polynomielle) Regression mit poly

Beitrag von bigben »

tl;dr; -- Hilft das hier?
Tja nur das das quadratische Regression eine punktgenaue Linie erstellt und keine smooth Version wie bei der plot Funktion.

Code: Alles auswählen

y<-c(20, 40, 60, 80, 100)
x<-c(26.35, 138.5, 271, 340, 406.5)
sample1 <- data.frame(y, x)

library(ggplot2)
ggplot(sample1, aes(x = x, y = y)) +
  geom_point() +
  geom_smooth(method="lm", color = 1) + 
  geom_smooth(method="lm", formula=y ~ I(x^2) + x, color = 2) +
  geom_smooth(method="lm", formula=y ~ I(x^3) + I(x^2)+x, color = 3) +
  theme_bw()
  

Um wirklich zu sehen, was Deine Regression macht ist predict oft hilfreich, aber das kriege ich mit poli ngerade nicht ans laufen. Wenn es nur um Polynome dritten Grades geht, geht das aber auch von Hand, wie hier gezeigt:

Code: Alles auswählen

neue.punkte <- data.frame(x=seq(15, 405, 5)) # viele neue x-Stellen zum Zeichnen
neue.punkte$y.fit1 <- predict((lm(y ~ x))                , neue.punkte)
neue.punkte$y.fit2 <- predict((lm(y ~ I(x^2) + x))       , neue.punkte)
neue.punkte$y.fit3 <- predict((lm(y ~ I(x^3) + I(x^2)+x)), neue.punkte)

ggplot(sample1) +
  geom_line(data = neue.punkte, mapping = aes(x=x, y=y.fit1), color="purple") +
  geom_line(data = neue.punkte, mapping = aes(x=x, y=y.fit2), color ="red") +
  geom_line(data = neue.punkte, mapping = aes(x=x, y=y.fit3), color="blue") +
  geom_point(aes(x=x, y=y))
LG,
Bernhard
---
Programmiere stets so, dass die Maxime Deines Programmierstils Grundlage allgemeiner Gesetzgebung sein könnte
Werekorden
Beiträge: 83
Registriert: So Feb 04, 2018 7:52 pm

Re: Polynomiale (polynomielle) Regression mit poly

Beitrag von Werekorden »

Hi Bernhard,

naja es kommt darauf an ab/bis wann man eine Signifikanz findet kann auch sein, das es vierten oder fünften Grades wird aber das liegt in der Zukunft.
Erst einmal langt es genau so wie gezeigt.

Dein zweites Beispiel muss ich erst einmal auseinander nehmen um es zu verstehen.

Vielen Dank bis hierhin.
Andreas
Antworten