



Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!




Bardoscia Silvia 226070 #Esercizio
library(sasld) x <- c (150:189) alfa <- - beta <- 0. set.seed(226070) e <- rnorm (40 , 0, 5) y <- ym + e cor (x,y) [1] 0. plot (x,y) fit_1 <- lm(y ~ x) b_1 <- fit_1$coef[2] #valore pendenza a_1 <- fit_1$coef[1] #valore intercetta abline(fit_1)
tss <- sum ((y - mean (y))^2) #devianza totale [1] 4365. ssr <- sum (( fit_1$ fitted - mean (y)) ^2) [1] 3325.229 #devianza spiegata sse <- sum ( fit_1$ res ^2) [1] 1040.293 #devianza residua s <- sqrt (sse/40 -2) [1] 4.899727 #residual standard error s Var.cond <- s^ [1] 24.00733 #varianza condizionata Rquadro <- (tss - sse )/ tss [1] 0.7617025 #valore di R^2 se <- s/ sqrt ( sum ((x- mean (x))^2) ) [1] 0.06711325 #errore standard del coeff. di correlazione lineare t <- qt (0.025 , length (y) -2, lower.tail = FALSE ) Intervallo_conf <- c(b_1 - tse , b_1 + tse) #intervallo confidenza al 95% per la pendenza Intervallo_conf[1] < 0.8 & Intervallo_conf [2] > 0. xcen <- c(x- mean (x)) ycen <- c(y- mean (y)) fit_2 = lm( ycen ~ xcen ) a_2 = round ( fit_2$ coef [1] , 10) a_2 == 0 b_2 = round ( fit_2$ coef [2] , 5) b_1_arrotondato = round (fit_1$ coef [2] , 5) b_1_arrotondato==b_
y_x2$ coeff[2] fit.x2_x1$res
fit_tot<- lm(y ~ x1+x2) summary (fit_tot) Call: lm(formula = y ~ x1 + x2) Residuals: Min 1Q Median 3Q Max -7.9194 -2.0351 -0.1987 1.4771 24. Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -60.4982 24.6153 -2.458 0.0176 * x1 1.6049 0.3010 5.332 2.58e-06 *** x2 0.5877 0.2605 2.256 0.0287 *
Signif. codes: 0 ‘’ 0.001 ‘’ 0.01 ‘’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 4.764 on 48 degrees of freedom Multiple R-squared: 0.4049, Adjusted R-squared: 0. F-statistic: 16.33 on 2 and 48 DF, p-value: 3.891e-
ok <- df$Violent < fit.ok_y_x2 <- lm(y[ok] ~ x2[ok]) fit.ok_x1_x2 <- lm(x1[ok] ~ x2[ok]) plot ( fit.ok_x1_x2$res , fit.ok_y_x2$res , xlab ="x1_ok", ylab ="y_ok") y_x1_ok <- lm( fit.ok_y_x2$res ~ fit.ok_x1_x2$res ) abline (y_x1_ok) y_x1_ok$ coeff[2] fit.ok_x1_x2$res
fit.ok_y_x1 <- lm(y[ok] ~ x1[ok]) fit.ok_x2_x1 <- lm(x2[ok] ~ x1[ok]) plot ( fit.ok_x2_x1$res , fit.ok_y_x1$res , xlab ="x2_ok", ylab ="y_ok") y_x2_ok <- lm( fit.ok_y_x1$res ~ fit.ok_x2_x1$ res ) abline (y_x2_ok)