R version 4.2.3 (2023-03-15 ucrt) -- "Shortstop Beagle" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > x <- rnorm(30) > y <- 2*x -3 + rnorm(30) > plot(x,y) > lm(y ~ 1 + x) Call: lm(formula = y ~ 1 + x) Coefficients: (Intercept) x -2.740 2.018 > lm(y ~ x) Call: lm(formula = y ~ x) Coefficients: (Intercept) x -2.740 2.018 > lm(y ~ 0 + x) Call: lm(formula = y ~ 0 + x) Coefficients: x 1.776 > reg1 <- lm(y ~ x) > reg1 Call: lm(formula = y ~ x) Coefficients: (Intercept) x -2.740 2.018 > reg1$coef (Intercept) x -2.740491 2.017643 > coef(reg1) (Intercept) x -2.740491 2.017643 > plot(x,y) > abline(reg1$coef) > reg1$fitted 1 2 3 4 -1.3251749 -5.2710408 -4.1121753 -2.9256397 5 6 7 8 -3.1220225 -1.4063942 -3.1214447 -0.8905963 9 10 11 12 -0.3847513 -3.4980010 -4.9577325 -0.8221440 13 14 15 16 -1.2396261 -5.0137336 -0.6389268 -4.3856314 17 18 19 20 0.9284030 -2.7138658 0.6835133 -1.0005437 21 22 23 24 -3.8661891 -4.4807969 -4.5218197 -2.8757003 25 26 27 28 -1.2010849 -3.9056531 -2.3932222 -2.7626299 29 30 -3.3076761 -3.9146694 > points(x,reg1$fitted) > reg1$resid # residuals, hibák 1 2 3 4 -0.89217623 -0.03358074 -0.77540866 -2.66060812 5 6 7 8 -1.78066813 -0.95313011 0.74591976 -0.05831520 9 10 11 12 -0.02874831 0.59897872 -0.14321249 0.79810465 13 14 15 16 0.62651230 0.71862230 -1.47590747 0.77194998 17 18 19 20 1.91300506 0.07123948 -0.93829340 0.32345718 21 22 23 24 -1.15022009 0.24091508 -0.35073173 0.60653270 25 26 27 28 -0.04143946 0.88568259 1.93911217 0.21633810 29 30 -0.07939263 0.90546269 > qqnorm(reg1$resid) > qqline(reg1$resid) > plot(x,reg1$resid) > plot(reg1) Waiting to confirm page change... Waiting to confirm page change... Waiting to confirm page change... Waiting to confirm page change... > plot(reg1$fitted,reg1$resid) > z <- rnorm(30) > reg2 <- lm(z ~ x) > reg2 Call: lm(formula = z ~ x) Coefficients: (Intercept) x -0.4814 0.2858 > plot(x,z) > abline(reg2$coef) > summary(reg2) Call: lm(formula = z ~ x) Residuals: Min 1Q Median 3Q Max -1.5019 -0.7440 -0.1642 0.6126 2.2247 Coefficients: Estimate Std. Error t value (Intercept) -0.4814 0.1911 -2.519 x 0.2858 0.2273 1.257 Pr(>|t|) (Intercept) 0.0178 * x 0.2191 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 1.044 on 28 degrees of freedom Multiple R-squared: 0.05342, Adjusted R-squared: 0.01962 F-statistic: 1.58 on 1 and 28 DF, p-value: 0.2191 > summary(reg2) Call: lm(formula = z ~ x) Residuals: Min 1Q Median 3Q Max -1.5019 -0.7440 -0.1642 0.6126 2.2247 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.4814 0.1911 -2.519 0.0178 * x 0.2858 0.2273 1.257 0.2191 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 1.044 on 28 degrees of freedom Multiple R-squared: 0.05342, Adjusted R-squared: 0.01962 F-statistic: 1.58 on 1 and 28 DF, p-value: 0.2191 > x <- rnorm(30) > y <- x^2 + x + rnorm(30) > reg1 <- lm(y ~ x) > plot(x,y) > abline(reg1$coef) > plot(x,reg1$resid) > plot(reg1$fitted,reg1$resid) > reg2 <- lm(y ~ x + I(x^2)) > reg2 Call: lm(formula = y ~ x + I(x^2)) Coefficients: (Intercept) x I(x^2) -0.2127 1.1152 1.2286 > lm(y ~ x + x^2) Call: lm(formula = y ~ x + x^2) Coefficients: (Intercept) x 0.9939 1.7421 > plot(x,reg2$fitted) > plot(x,reg2$fitted,type="l") > sort(x) [1] -1.99196706 -1.19096626 -1.03488407 -0.97235622 [5] -0.92489372 -0.89405407 -0.76075144 -0.60460101 [9] -0.52321368 -0.44841373 -0.42905162 -0.42612160 [13] -0.41182089 -0.17958619 -0.14116787 -0.09009809 [17] -0.07701307 0.04150317 0.08311077 0.39270177 [21] 0.39446773 0.62494769 0.66604958 1.06753858 [25] 1.30719221 1.43091471 1.56311862 1.86008696 [29] 1.86109455 2.02477961 > order(x) [1] 4 12 7 19 23 5 6 27 14 26 16 11 20 17 21 28 2 13 [19] 18 1 29 25 24 8 10 15 22 9 30 3 > x[order(x)] [1] -1.99196706 -1.19096626 -1.03488407 -0.97235622 [5] -0.92489372 -0.89405407 -0.76075144 -0.60460101 [9] -0.52321368 -0.44841373 -0.42905162 -0.42612160 [13] -0.41182089 -0.17958619 -0.14116787 -0.09009809 [17] -0.07701307 0.04150317 0.08311077 0.39270177 [21] 0.39446773 0.62494769 0.66604958 1.06753858 [25] 1.30719221 1.43091471 1.56311862 1.86008696 [29] 1.86109455 2.02477961 > plot(x[order(x)],reg2$fitted[order(x)]) > plot(x[order(x)],reg2$fitted[order(x)],type="l") > f <- predict(reg2,z) Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 'data' must be a data.frame, environment, or list > as.data.frame(z) z 1 -0.273620304 2 -1.661995642 3 -0.195540208 4 -0.665910693 5 -0.095568583 6 1.932320254 7 -0.239102315 8 0.562627886 9 1.301974429 10 -0.789140420 11 -2.297389546 12 -1.572333937 13 1.882863371 14 0.757930935 15 -0.700446827 16 0.514363376 17 -0.709157578 18 -0.644175183 19 -0.462828189 20 -1.197562196 21 -0.802681619 22 -2.161587929 23 0.181550321 24 -0.979633275 25 -1.406971321 26 -1.207264397 27 -1.166046236 28 -0.211714512 29 -1.610263532 30 0.009099006 > f <- predict(reg2,as.data.frame(z)) > f 1 2 3 4 5 0.41475173 -0.29126274 7.08222818 2.44072724 -0.22769813 6 7 8 9 10 -0.35004578 -0.05101147 2.37800829 6.11250909 3.34447065 11 12 13 14 15 -0.46480074 0.20174597 -0.16426091 -0.45983939 3.89864591 16 17 18 19 20 -0.46498998 -0.37331865 -0.11148908 -0.13547466 -0.46357440 21 22 23 24 25 -0.34561305 4.53237892 -0.19317380 1.07515304 0.96412432 26 27 28 29 30 -0.46571021 -0.43783439 -0.30316902 0.41842901 6.11823918 > plot(x,y) > lines(z,f) > z <- seq(-2.5,2.5,0.1) > f <- predict(reg2,as.data.frame(z)) Warning message: 'newdata' had 51 rows but variables found have 30 rows > plot(x,y) > lines(z,f) Error in xy.coords(x, y) : 'x' and 'y' lengths differ > f <- predict(reg2,as.data.frame(z)) Warning message: 'newdata' had 51 rows but variables found have 30 rows > lines(z,f) Error in xy.coords(x, y) : 'x' and 'y' lengths differ > z <- seq(-2.5,2.5,0.1) > f <- predict(reg2,as.data.frame(z)) Warning message: 'newdata' had 51 rows but variables found have 30 rows > plot(x,y) > lines(z,f) Error in xy.coords(x, y) : 'x' and 'y' lengths differ > plot(x,y) > lines(z,f) Error in xy.coords(x, y) : 'x' and 'y' lengths differ > f <- predict(reg2,as.data.frame(z)) Warning message: 'newdata' had 51 rows but variables found have 30 rows > reg2 Call: lm(formula = y ~ x + I(x^2)) Coefficients: (Intercept) x I(x^2) -0.2127 1.1152 1.2286 > z <- seq(-2.5,2.5,0.2) > f <- predict(reg2,as.data.frame(z)) Warning message: 'newdata' had 26 rows but variables found have 30 rows > ?predict starting httpd help server ... done > predict(reg2,as.data.frame(x)) 1 2 3 4 5 0.41475173 -0.29126274 7.08222818 2.44072724 -0.22769813 6 7 8 9 10 -0.35004578 -0.05101147 2.37800829 6.11250909 3.34447065 11 12 13 14 15 -0.46480074 0.20174597 -0.16426091 -0.45983939 3.89864591 16 17 18 19 20 -0.46498998 -0.37331865 -0.11148908 -0.13547466 -0.46357440 21 22 23 24 25 -0.34561305 4.53237892 -0.19317380 1.07515304 0.96412432 26 27 28 29 30 -0.46571021 -0.43783439 -0.30316902 0.41842901 6.11823918 > predict(reg2,new.data=as.data.frame(z)) 1 2 3 4 5 0.41475173 -0.29126274 7.08222818 2.44072724 -0.22769813 6 7 8 9 10 -0.35004578 -0.05101147 2.37800829 6.11250909 3.34447065 11 12 13 14 15 -0.46480074 0.20174597 -0.16426091 -0.45983939 3.89864591 16 17 18 19 20 -0.46498998 -0.37331865 -0.11148908 -0.13547466 -0.46357440 21 22 23 24 25 -0.34561305 4.53237892 -0.19317380 1.07515304 0.96412432 26 27 28 29 30 -0.46571021 -0.43783439 -0.30316902 0.41842901 6.11823918 > f <- predict(reg2,new.data=as.data.frame(z)) > plot(x,y) > lines(z,f) Error in xy.coords(x, y) : 'x' and 'y' lengths differ > f <- predict(reg2,new.data=as.data.frame(z)) > lines(z,f) Error in xy.coords(x, y) : 'x' and 'y' lengths differ > predict(reg2,new.data=as.data.frame(z)) 1 2 3 4 5 0.41475173 -0.29126274 7.08222818 2.44072724 -0.22769813 6 7 8 9 10 -0.35004578 -0.05101147 2.37800829 6.11250909 3.34447065 11 12 13 14 15 -0.46480074 0.20174597 -0.16426091 -0.45983939 3.89864591 16 17 18 19 20 -0.46498998 -0.37331865 -0.11148908 -0.13547466 -0.46357440 21 22 23 24 25 -0.34561305 4.53237892 -0.19317380 1.07515304 0.96412432 26 27 28 29 30 -0.46571021 -0.43783439 -0.30316902 0.41842901 6.11823918 > length(z) [1] 26 > z <- seq(-2.5,2.5,length=30) > f <- predict(reg2,new.data=as.data.frame(z)) > plot(x,y) > lines(z,f) > f <- predict(reg2,newdata=as.data.frame(z)) > plot(x,y) > lines(z,f) > z <- seq(-2.5,2.5,0.1) > f <- predict(reg2,newdata=data.frame(x=z)) > plot(x,y) > lines(z,f) > plot(x,reg2$resid) > plot(x,reg1$resid) > plot(x,reg2$resid) > summary(reg2) Call: lm(formula = y ~ x + I(x^2)) Residuals: Min 1Q Median 3Q Max -1.47203 -0.63103 0.07065 0.41699 1.84062 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.2127 0.1875 -1.134 0.267 x 1.1152 0.1541 7.235 8.81e-08 *** I(x^2) 1.2286 0.1251 9.821 2.09e-10 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.774 on 27 degrees of freedom Multiple R-squared: 0.9027, Adjusted R-squared: 0.8955 F-statistic: 125.3 on 2 and 27 DF, p-value: 2.173e-14 > reg3 <- lm(y ~ 0+x+I(x^2) ) > reg3 Call: lm(formula = y ~ 0 + x + I(x^2)) Coefficients: x I(x^2) 1.154 1.136 > plot(x,y) > points(x,reg3$fitted,col="red") > plot(x,reg3$resid) > summary(reg3) Call: lm(formula = y ~ 0 + x + I(x^2)) Residuals: Min 1Q Median 3Q Max -1.62002 -0.74397 -0.08064 0.26340 1.80576 Coefficients: Estimate Std. Error t value Pr(>|t|) x 1.1539 0.1511 7.636 2.56e-08 *** I(x^2) 1.1356 0.0950 11.955 1.63e-12 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.7779 on 28 degrees of freedom Multiple R-squared: 0.917, Adjusted R-squared: 0.9111 F-statistic: 154.6 on 2 and 28 DF, p-value: 7.385e-16 > reg1 Call: lm(formula = y ~ x) Coefficients: (Intercept) x 0.9939 1.7421 > reg2 Call: lm(formula = y ~ x + I(x^2)) Coefficients: (Intercept) x I(x^2) -0.2127 1.1152 1.2286 > reg3 Call: lm(formula = y ~ 0 + x + I(x^2)) Coefficients: x I(x^2) 1.154 1.136 > anova(reg1,reg2) Analysis of Variance Table Model 1: y ~ x Model 2: y ~ x + I(x^2) Res.Df RSS Df Sum of Sq F Pr(>F) 1 28 73.957 2 27 16.175 1 57.783 96.454 2.088e-10 *** --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 > anova(reg2,reg3) Analysis of Variance Table Model 1: y ~ x + I(x^2) Model 2: y ~ 0 + x + I(x^2) Res.Df RSS Df Sum of Sq F Pr(>F) 1 27 16.175 2 28 16.945 -1 -0.77036 1.2859 0.2668 > a <- rnorm(30) > b <- rnorm(30) > c <- 2 *a -b + rnorm(30) > reg4 <- lm(c ~ a + b) > reg4 Call: lm(formula = c ~ a + b) Coefficients: (Intercept) a b 0.06035 2.03639 -1.05219 > plot(x,y) > read.table("adat3B.txt") V1 V2 1 x y 2 -1.8128998382017 1.26669337503769 3 -1.78000617120415 1.71519363544058 4 -1.53761506639421 1.53039595994982 5 -1.38548593875021 2.72491049194419 6 -1.36370046436787 1.54231349922645 7 -1.06372281443328 2.88488442579417 8 -1.0034651523456 2.3685727509976 9 -0.576038711704314 3.84214062542247 10 -0.511751984246075 3.26162721906042 11 -0.501689574681222 3.91782015629168 12 -0.418183164671063 3.39803187033859 13 -0.263308608904481 4.39116859949933 14 -0.202671359293163 3.03084561916818 15 -0.171649440191686 2.88538221901632 16 -0.136144533753395 4.16882140549909 17 -0.122023028321564 4.56867890337556 18 0.111457922495902 4.56005239290003 19 0.187089855782688 4.3856096725553 20 0.211925454437733 3.72069757748924 21 0.496581580489874 2.7570106560698 22 0.624563951045275 3.28052419828612 23 0.677349402569234 3.57347895464101 24 0.69881484284997 4.3505365426658 25 0.712974446825683 2.68153536193289 26 0.754332346841693 3.09814415196959 27 0.923411292955279 3.06342903785826 28 1.06195071432739 2.80959437953245 29 1.30101988278329 2.54548965410528 30 1.42867687996477 2.06092751859977 31 1.75527362432331 0.941456016204296 > read.table("adat3B.txt",head=TRUE) -> adat > head(adat) x y 1 -1.812900 1.266693 2 -1.780006 1.715194 3 -1.537615 1.530396 4 -1.385486 2.724910 5 -1.363700 1.542313 6 -1.063723 2.884884 >