概述
Error in eval(predvars, data, env) : object '**' not found
目录
Error in eval(predvars, data, env) : object '**' not found
#问题
#解决
#完整错误
#问题
新数据中的变量的名称和训练模型中的变量名称不同
#create data
df <- data.frame(x1=c(3, 4, 4, 5, 5, 6, 7, 8, 11, 12),
x2=c(6, 6, 7, 7, 8, 9, 11, 13, 14, 14),
y=c(22, 24, 24, 25, 25, 27, 29, 31, 32, 36))
#fit multiple linear regression model
model <- lm(y ~ x1 + x2, data=df)
#define new observation
new <- data.frame(x_1=c(5),
x_2=c(10))
#use the fitted model to predict the value for the new observation
predict(model, newdata = new)
#解决
#create data
df <- data.frame(x1=c(3, 4, 4, 5, 5, 6, 7, 8, 11, 12),
x2=c(6, 6, 7, 7, 8, 9, 11, 13, 14, 14),
y=c(22, 24, 24, 25, 25, 27, 29, 31, 32, 36))
#fit multiple linear regression model
model <- lm(y ~ x1 + x2, data=df)
#define new observation
new <- data.frame(x1=c(5),
x2=c(10))
#use the fitted model to predict the value for the new observation
predict(model, newdata = new)
> #create data
> df <- data.frame(x1=c(3, 4, 4, 5, 5, 6, 7, 8, 11, 12),
+ x2=c(6, 6, 7, 7, 8, 9, 11, 13, 14, 14),
+ y=c(22, 24, 24, 25, 25, 27, 29, 31, 32, 36))
>
> #fit multiple linear regression model
> model <- lm(y ~ x1 + x2, data=df)
>
>
> #define new observation
> new <- data.frame(x1=c(5),
+ x2=c(10))
>
> #use the fitted model to predict the value for the new observation
> predict(model, newdata = new)
1
26.17073
#完整错误
> str(df)
'data.frame': 6 obs. of 3 variables:
$ team : chr "B" "B" "B" "A" ...
$ points : num 12 28 19 22 32 45
$ rebounds: num 5 7 7 12 11 4
> library(ggplot2)
>
> ggplot(df, aes(x=team)) +
+ geom_bar()
> #specify factor level order
> df$team = factor(df$team, levels = c('C', 'A', 'B'))
>
> #create bar chart again
> ggplot(df, aes(x=team)) +
+ geom_bar()
> library(ggplot2)
>
> ggplot(df, aes(x=reorder(team, team, function(x)-length(x)))) +
+ geom_bar()
> library(ggplot2)
>
> ggplot(df, aes(x=reorder(team, team, function(x) length(x)))) +
+ geom_bar()
> #create dataset
> data <- data.frame(y=c(6, 7, 7, 9, 12, 13, 13, 15, 16, 19, 22, 23, 23, 25, 26),
+ x=c(1, 2, 2, 3, 4, 4, 5, 6, 6, 8, 9, 9, 11, 12, 12))
>
> #fit linear regression model to dataset and view model summary
> model <- lm(y~x, data=data)
> summary(model)
Call:
lm(formula = y ~ x, data = data)
Residuals:
Min 1Q Median 3Q Max
-1.4444 -0.8013 -0.2426 0.5978 2.2363
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 4.20041 0.56730 7.404 5.16e-06 ***
x 1.84036 0.07857 23.423 5.13e-12 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.091 on 13 degrees of freedom
Multiple R-squared: 0.9769, Adjusted R-squared: 0.9751
F-statistic: 548.7 on 1 and 13 DF, p-value: 5.13e-12
> ggplot(data,aes(x, y)) +
+ geom_point() +
+ geom_smooth(method='lm')
`geom_smooth()` using formula 'y ~ x'
> library(ggplot2)
>
> #create regression plot with no standard error lines
> ggplot(data,aes(x, y)) +
+ geom_point() +
+ geom_smooth(method='lm', se=FALSE)
`geom_smooth()` using formula 'y ~ x'
> library(ggplot2)
>
> #create regression plot with customized style
> ggplot(data,aes(x, y)) +
+ geom_point() +
+ geom_smooth(method='lm', se=FALSE, color='turquoise4') +
+ theme_minimal() +
+ labs(x='X Values', y='Y Values', title='Linear Regression Plot') +
+ theme(plot.title = element_text(hjust=0.5, size=20, face='bold'))
`geom_smooth()` using formula 'y ~ x'
>
> #create dataset
> df <- data.frame(hours=c(1, 2, 3, 3, 4, 1, 2, 2, 3, 4, 1, 2, 3, 4, 4),
+ score=c(84, 86, 85, 87, 94, 74, 76, 75, 77, 79, 65, 67, 69, 72, 80),
+ technique=rep(c('A', 'B', 'C'), each=5))
>
> #view dataset
> df
hours score technique
1 1 84 A
2 2 86 A
3 3 85 A
4 3 87 A
5 4 94 A
6 1 74 B
7 2 76 B
8 2 75 B
9 3 77 B
10 4 79 B
11 1 65 C
12 2 67 C
13 3 69 C
14 4 72 C
15 4 80 C
> #load ggplot2
> library(ggplot2)
>
> #create regression lines for all three groups
> ggplot(df, aes(x = hours, y = score, color = technique)) +
+ geom_point() +
+ geom_smooth(method = "lm", fill = NA)
`geom_smooth()` using formula 'y ~ x'
> ggplot(df, aes(x = hours, y = score, color = technique, shape = technique)) +
+ geom_point() +
+ geom_smooth(method = "lm", fill = NA)
`geom_smooth()` using formula 'y ~ x'
> df <- data.frame(x=c(1, 2, 4, 5, 7, 9, 13, 14, 15, 17, 18, 20),
+ y=c(34, 35, 36, 23, 37, 38, 49, 45, 48, 51, 53, 55))
>
>
> library(ggplot2)
>
> ggplot(df, aes(x=x, y=y)) +
+ geom_point() +
+ geom_smooth()
`geom_smooth()` using method = 'loess' and formula 'y ~ x'
> ggplot(df, aes(x=x, y=y)) +
+ geom_point() +
+ geom_smooth(method='lm')
`geom_smooth()` using formula 'y ~ x'
> ggplot(df, aes(x=x, y=y)) +
+ geom_point() +
+ geom_smooth(method='lm', se=FALSE)
`geom_smooth()` using formula 'y ~ x'
> ggplot(df, aes(x=x, y=y)) +
+ geom_point() +
+ geom_smooth(method='lm', se=FALSE, col='red', size=2)
`geom_smooth()` using formula 'y ~ x'
>
>
> library(ggplot2)
>
> ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
+ geom_point()
>
> library(ggplot2)
>
> ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
+ geom_point() +
+ scale_color_manual(values = c("setosa" = "purple",
+ "versicolor="orange",
Error: unexpected symbol in:
" scale_color_manual(values = c("setosa" = "purple",
"versicolor="orange"
> "virginica"="steelblue"))
Error: unexpected ')' in " "virginica"="steelblue")"
> ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
+ geom_point() +
+ scale_color_manual(values = c("setosa" = "purple",
+ "versicolor"="orange",
+ "virginica"="steelblue"))
> library(ggplot2)
> library(RColorBrewer)
>
> #define custom color scale
> myColors <- brewer.pal(3, "Spectral")
> names(myColors) <- levels(iris$Species)
> custom_colors <- scale_colour_manual(name = "Species Names", values = myColors)
>
> ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width, color=Species)) +
+ geom_point() +
+ custom_colors
> library(ggplot2)
>
> #create data frame
> df <- data.frame(x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
+ y=c(11, 13, 15, 14, 19, 22, 28, 25, 30, 29))
>
> #create scatterplot
> ggplot(df, aes(x=x, y=y))+
+ geom_point() +
+ theme(axis.text.x=element_blank(),
+ axis.ticks.x=element_blank()
+ )
>
> library(ggplot2)
>
> #create data frame
> df <- data.frame(x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
+ y=c(11, 13, 15, 14, 19, 22, 28, 25, 30, 29))
>
> #create scatterplot
> ggplot(df, aes(x=x, y=y))+
+ geom_point()
> library(ggplot2)
>
> #create data frame
> df <- data.frame(x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
+ y=c(11, 13, 15, 14, 19, 22, 28, 25, 30, 29))
>
> #create scatterplot
> ggplot(df, aes(x=x, y=y))+
+ geom_point() +
+ theme(axis.text.y=element_blank(),
+ axis.ticks.y=element_blank()
+ )
> library(ggplot2)
>
> #create data frame
> df <- data.frame(x=c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
+ y=c(11, 13, 15, 14, 19, 22, 28, 25, 30, 29))
>
> #create scatterplot
> ggplot(df, aes(x=x, y=y))+
+ geom_point() +
+ theme(axis.text.x=element_blank(),
+ axis.ticks.x=element_blank(),
+ axis.text.y=element_blank(),
+ axis.ticks.y=element_blank()
+ )
> #load ggplot2 visualization package
> library(ggplot2)
>
> #create data
> df <- data.frame(x=c(1, 2, 3, 4, 5, 6, 7),
+ y=c(6, 8, 12, 14, 11, 10, 15))
>
> #create line plot
> ggplot(df, aes(x = x, y = y)) +
+ geom_line()
> #create line plot
> ggplot(df, aes(x = x, y = y)) +
+ geom_line(size = 2)
>
>
> library(ggplot2)
> library(gridExtra)
载入程辑包:‘gridExtra’
The following object is masked from ‘package:dplyr’:
combine
>
> #create data
> df <- data.frame(x=c(1, 2, 3, 4, 5, 6, 7),
+ y=c(6, 8, 12, 14, 11, 10, 15))
>
> #create four line plots
> plot1 <- ggplot(df, aes(x=x,y=y)) + geom_line() + ggtitle("Size = 1 (Default)")
> plot2 <- ggplot(df, aes(x=x,y=y)) + geom_line(size=1.5) + ggtitle("Size = 1.5")
> plot3 <- ggplot(df, aes(x=x,y=y)) + geom_line(size=2) + ggtitle("Size = 2")
> plot4 <- ggplot(df, aes(x=x,y=y)) + geom_line(size=3) + ggtitle("Size = 3")
>
> #display all line plots stacked on top of each other
> grid.arrange(plot1, plot2, plot3, plot4, ncol=1)
> Change line width in ggplot2
Error: unexpected symbol in "Change line"
> ggplot(mpg, aes(displ, hwy)) +
+ geom_point() +
+ facet_wrap(vars(class))
> #define custom labels
> plot_names <- c('2seater' = "2 Seater",
+ 'compact' = "Compact Vehicle",
+ 'midsize' = "Midsize Vehicle",
+ 'minivan' = "Minivan",
+ 'pickup' = "Pickup Truck",
+ 'subcompact' = "Subcompact Vehicle",
+ 'suv' = "Sport Utility Vehicle")
>
> #use facet_wrap with custom plot labels
> ggplot(mpg, aes(displ, hwy)) +
+ geom_point() +
+ facet_wrap(vars(class), labeller = as_labeller(plot_names))
> #use facet_wrap with custom scales
> ggplot(mpg, aes(displ, hwy)) +
+ geom_point() +
+ facet_wrap(vars(class), scales='free')
> #define order for plots
> mpg <- within(mpg, class <- factor(class, levels=c('compact', '2seater', 'suv',
+ 'subcompact', 'pickup',
+ 'minivan', 'midsize')))
>
> #use facet_wrap with custom order
> ggplot(mpg, aes(displ, hwy)) +
+ geom_point() +
+ facet_wrap(vars(class))
> library(ggplot2)
>
> #create data frame
> df <- data.frame(x=c(1, 2, 4, 5, 7, 8, 9, 10),
+ y=c(12, 17, 27, 39, 50, 57, 66, 80))
>
> #create scatterplot of x vs. y
> ggplot(df, aes(x=x, y=y)) +
+ geom_point()
> #create scatterplot of x vs. y with margin added on x-axis title
> ggplot(df, aes(x=x, y=y)) +
+ geom_point() +
+ theme(axis.title.x = element_text(margin = margin(t = 70)))
> #create scatterplot of x vs. y with margin added on y-axis title
> ggplot(df, aes(x=x, y=y)) +
+ geom_point() +
+ theme(axis.title.y = element_text(margin = margin(r = 70)))
> library(ggplot2)
>
> #make this example reproducible
> set.seed(0)
>
> #create data
> df <- data.frame(x=rnorm(n=5000))
>
> #create histogram using ggplot2
> ggplot(df, aes(x=x)) +
+ geom_histogram() +
+ ggtitle('Title of Histogram') +
+ theme(plot.background=element_rect(fill='#e3fbff'))
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
> library(ggplot2)
>
> #make this example reproducible
> set.seed(0)
>
> #create data
> df <- data.frame(x=rnorm(n=5000))
>
> #create histogram with significant margins on top and bottom
> ggplot(df, aes(x=x)) +
+ geom_histogram() +
+ ggtitle('Title of Histogram') +
+ theme(plot.margin=unit(c(5,1,5,1), 'cm'),
+ plot.background=element_rect(fill='#e3fbff'))
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
> library(ggplot2)
>
> #make this example reproducible
> set.seed(0)
>
> #create data
> df <- data.frame(x=rnorm(n=5000))
>
> #create histogram with significant margins on left and right
> ggplot(df, aes(x=x)) +
+ geom_histogram() +
+ ggtitle('Title of Histogram') +
+ theme(plot.margin=unit(c(1,5,1,5), 'cm'),
+ plot.background=element_rect(fill='#e3fbff'))
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
> #create data frame
> df <- data.frame(x=c(1, 2, 4, 5, 7, 8, 9, 10),
+ y=c(12, 17, 27, 39, 50, 57, 66, 80))
>
> #view data frame
> df
x y
1 1 12
2 2 17
3 4 27
4 5 39
5 7 50
6 8 57
7 9 66
8 10 80
> library(ggplot2)
>
> #create scatterplot of x vs. y
> ggplot(df, aes(x=x, y=y)) +
+ geom_point()
> #create scatterplot of x vs. y with custom breaks on y-axis
> ggplot(df, aes(x=x, y=y)) +
+ geom_point() +
+ scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, 10))
> #create scatterplot of x vs. y with custom breaks on x-axis
> ggplot(df, aes(x=x, y=y)) +
+ geom_point() +
+ scale_x_continuous(limits = c(0, 10), breaks = c(0, 2, 4, 6, 8, 10))
> #create scatterplot of x vs. y with custom breaks on x-axis
> ggplot(df, aes(x=x, y=y)) +
+ geom_point() +
+ scale_x_continuous(limits = c(0, 10), breaks = c(0, 7, 10))
> set.seed(1)
>
> #create dataset
> df <- data.frame(hours = runif(50, 5, 15), score=50)
> df$score = df$score + df$hours^3/150 + df$hours*runif(50, 1, 2)
>
> #view first six rows of data
> head(df)
hours score
1 7.655087 64.30191
2 8.721239 70.65430
3 10.728534 73.66114
4 14.082078 86.14630
5 7.016819 59.81595
6 13.983897 83.60510
> #load ggplot2 visualization package
> library(ggplot2)
>
> #create scatterplot
> ggplot(df, aes(x=hours, y=score)) +
+ geom_point()
> #fit a regression model
> model <- lm(mpg~disp+hp, data=mtcars)
>
> #view model summary
> summary(model)
Call:
lm(formula = mpg ~ disp + hp, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-4.7945 -2.3036 -0.8246 1.8582 6.9363
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 30.735904 1.331566 23.083 < 2e-16 ***
disp -0.030346 0.007405 -4.098 0.000306 ***
hp -0.024840 0.013385 -1.856 0.073679 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.127 on 29 degrees of freedom
Multiple R-squared: 0.7482, Adjusted R-squared: 0.7309
F-statistic: 43.09 on 2 and 29 DF, p-value: 2.062e-09
>
> Coefficients:
+ Estimate Std. Error t value Pr(>|t|)
Error: unexpected symbol in:
"Coefficients:
Estimate Std."
> (Intercept) 30.735904 1.331566 23.083 < 2e-16 ***
Error: unexpected numeric constant in "(Intercept) 30.735904"
> disp -0.030346 0.007405 -4.098 0.000306 ***
Error: unexpected numeric constant in " disp -0.030346 0.007405"
> hp -0.024840 0.013385 -1.856 0.073679 .
Error: unexpected numeric constant in " hp -0.024840 0.013385"
> ---
+ Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Error: unexpected symbol in:
"---
Signif. codes"
>
> Residual standard error: 3.127 on 29 degrees of freedom
Error: unexpected symbol in "Residual standard"
> Multiple R-squared: 0.7482, Adjusted R-squared: 0.7309
Error: unexpected symbol in "Multiple R"
> F-statistic: 43.09 on 2 and 29 DF, p-value: 2.062e-09
Error: unexpected symbol in "F-statistic: 43.09 on"
>
>
> #calculate DFBETAS for each observation in the model
> dfbetas <- as.data.frame(dfbetas(model))
>
> #display DFBETAS for each observation
> dfbetas
(Intercept) disp hp
Mazda RX4 -0.1174171253 0.030760632 1.748143e-02
Mazda RX4 Wag -0.1174171253 0.030760632 1.748143e-02
Datsun 710 -0.1694989349 0.086630144 -3.332781e-05
Hornet 4 Drive 0.0577309674 0.078971334 -8.705488e-02
Hornet Sportabout -0.0204333878 0.237526523 -1.366155e-01
Valiant -0.1711908285 -0.139135639 1.829038e-01
Duster 360 -0.0312338677 -0.005356209 3.581378e-02
Merc 240D -0.0312259577 -0.010409922 2.433256e-02
Merc 230 -0.0865872595 0.016428917 2.287867e-02
Merc 280 -0.1560683502 0.078667906 -1.911180e-02
Merc 280C -0.2254489597 0.113639937 -2.760800e-02
Merc 450SE 0.0022844093 0.002966155 -2.855985e-02
Merc 450SL 0.0009062022 0.001176644 -1.132941e-02
Merc 450SLC 0.0041566755 0.005397169 -5.196706e-02
Cadillac Fleetwood 0.0388832216 -0.134511133 7.277283e-02
Lincoln Continental 0.0483781688 -0.121146607 5.326220e-02
Chrysler Imperial -0.1645266331 0.236634429 -3.917771e-02
Fiat 128 0.5720358325 -0.181104179 -1.265475e-01
Honda Civic 0.3490872162 -0.053660545 -1.326422e-01
Toyota Corolla 0.7367058819 -0.268512348 -1.342384e-01
Toyota Corona -0.2181110386 0.101336902 5.945352e-03
Dodge Challenger -0.0270169005 -0.123610713 9.441241e-02
AMC Javelin -0.0406785103 -0.141711468 1.074514e-01
Camaro Z28 0.0390139262 0.012846225 -5.031588e-02
Pontiac Firebird -0.0549059340 0.574544346 -3.689584e-01
Fiat X1-9 0.0565157245 -0.017751582 -1.262221e-02
Porsche 914-2 0.0839169111 -0.028670987 -1.240452e-02
Lotus Europa 0.3444562478 -0.402678927 2.135224e-01
Ford Pantera L -0.1598854695 -0.094184733 2.320845e-01
Ferrari Dino -0.0343997122 0.248642444 -2.344154e-01
Maserati Bora -0.3436265545 -0.511285637 7.319066e-01
Volvo 142E -0.1784974091 0.132692956 -4.433915e-02
> #find number of observations
> n <- nrow(mtcars)
>
> #calculate DFBETAS threshold value
> thresh <- 2/sqrt(n)
>
> #specify 2 rows and 1 column in plotting region
> par(mfrow=c(2,1))
>
> #plot DFBETAS for disp with threshold lines
> plot(dfbetas$disp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
invalid graphics state
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
invalid graphics state
>
> #plot DFBETAS for hp with threshold lines
> plot(dfbetas$hp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
invalid graphics state
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
invalid graphics state
> par(mfrow=c(2,1))
>
> #plot DFBETAS for disp with threshold lines
> plot(dfbetas$disp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
invalid graphics state
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
invalid graphics state
>
> #plot DFBETAS for hp with threshold lines
> plot(dfbetas$hp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
invalid graphics state
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
invalid graphics state
>
>
>
>
>
>
>
>
>
>
>
>
> par(mfrow=c(2,1))
>
> #plot DFBETAS for disp with threshold lines
> plot(dfbetas$disp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
invalid graphics state
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
invalid graphics state
>
> #plot DFBETAS for hp with threshold lines
> plot(dfbetas$hp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
invalid graphics state
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
invalid graphics state
> #fit a regression model
> model <- lm(mpg~disp+hp, data=mtcars)
>
> #view model summary
> summary(model)
Call:
lm(formula = mpg ~ disp + hp, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-4.7945 -2.3036 -0.8246 1.8582 6.9363
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 30.735904 1.331566 23.083 < 2e-16 ***
disp -0.030346 0.007405 -4.098 0.000306 ***
hp -0.024840 0.013385 -1.856 0.073679 .
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 3.127 on 29 degrees of freedom
Multiple R-squared: 0.7482, Adjusted R-squared: 0.7309
F-statistic: 43.09 on 2 and 29 DF, p-value: 2.062e-09
> library(survival)
> #calculate DFBETAS for each observation in the model
> dfbetas <- as.data.frame(dfbetas(model))
>
> #display DFBETAS for each observation
> dfbetas
(Intercept) disp hp
Mazda RX4 -0.1174171253 0.030760632 1.748143e-02
Mazda RX4 Wag -0.1174171253 0.030760632 1.748143e-02
Datsun 710 -0.1694989349 0.086630144 -3.332781e-05
Hornet 4 Drive 0.0577309674 0.078971334 -8.705488e-02
Hornet Sportabout -0.0204333878 0.237526523 -1.366155e-01
Valiant -0.1711908285 -0.139135639 1.829038e-01
Duster 360 -0.0312338677 -0.005356209 3.581378e-02
Merc 240D -0.0312259577 -0.010409922 2.433256e-02
Merc 230 -0.0865872595 0.016428917 2.287867e-02
Merc 280 -0.1560683502 0.078667906 -1.911180e-02
Merc 280C -0.2254489597 0.113639937 -2.760800e-02
Merc 450SE 0.0022844093 0.002966155 -2.855985e-02
Merc 450SL 0.0009062022 0.001176644 -1.132941e-02
Merc 450SLC 0.0041566755 0.005397169 -5.196706e-02
Cadillac Fleetwood 0.0388832216 -0.134511133 7.277283e-02
Lincoln Continental 0.0483781688 -0.121146607 5.326220e-02
Chrysler Imperial -0.1645266331 0.236634429 -3.917771e-02
Fiat 128 0.5720358325 -0.181104179 -1.265475e-01
Honda Civic 0.3490872162 -0.053660545 -1.326422e-01
Toyota Corolla 0.7367058819 -0.268512348 -1.342384e-01
Toyota Corona -0.2181110386 0.101336902 5.945352e-03
Dodge Challenger -0.0270169005 -0.123610713 9.441241e-02
AMC Javelin -0.0406785103 -0.141711468 1.074514e-01
Camaro Z28 0.0390139262 0.012846225 -5.031588e-02
Pontiac Firebird -0.0549059340 0.574544346 -3.689584e-01
Fiat X1-9 0.0565157245 -0.017751582 -1.262221e-02
Porsche 914-2 0.0839169111 -0.028670987 -1.240452e-02
Lotus Europa 0.3444562478 -0.402678927 2.135224e-01
Ford Pantera L -0.1598854695 -0.094184733 2.320845e-01
Ferrari Dino -0.0343997122 0.248642444 -2.344154e-01
Maserati Bora -0.3436265545 -0.511285637 7.319066e-01
Volvo 142E -0.1784974091 0.132692956 -4.433915e-02
> #find number of observations
> n <- nrow(mtcars)
>
> #calculate DFBETAS threshold value
> thresh <- 2/sqrt(n)
>
> thresh
[1] 0.3535534
> #specify 2 rows and 1 column in plotting region
> par(mfrow=c(2,1))
>
> #plot DFBETAS for disp with threshold lines
> plot(dfbetas$disp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
> #plot DFBETAS for hp with threshold lines
> plot(dfbetas$hp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> plot(dfbetas$disp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
> #plot DFBETAS for hp with threshold lines
> plot(dfbetas$hp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
>
>
>
>
>
>
>
>
> plot(dfbetas$disp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
> #plot DFBETAS for hp with threshold lines
> plot(dfbetas$hp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
>
>
> par(mfrow=c(2,1))
>
> #plot DFBETAS for disp with threshold lines
> plot(dfbetas$disp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
> #plot DFBETAS for hp with threshold lines
> plot(dfbetas$hp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
> #specify 2 rows and 1 column in plotting region
>
> dev.off()
null device
1
> par(mfrow=c(2,1))
>
> #plot DFBETAS for disp with threshold lines
> plot(dfbetas$disp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
> #plot DFBETAS for hp with threshold lines
> plot(dfbetas$hp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
> #specify 2 rows and 1 column in plotting region
>
> dev.off()
Error in dev.off() : 不能关闭一号装置(无效装置)
> par(mfrow=c(2,1))
>
> #plot DFBETAS for disp with threshold lines
> plot(dfbetas$disp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
> #plot DFBETAS for hp with threshold lines
> plot(dfbetas$hp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> par(mfrow=c(2,1))
>
> #plot DFBETAS for disp with threshold lines
> plot(dfbetas$disp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
> #plot DFBETAS for hp with threshold lines
> plot(dfbetas$hp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> graphics.off()
> par(mfrow=c(2,1))
>
> #plot DFBETAS for disp with threshold lines
> plot(dfbetas$disp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
> #plot DFBETAS for hp with threshold lines
> plot(dfbetas$hp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> par(mfrow=c(2,1))
> plot(dfbetas$disp, type='h')
Error in plot.new() : figure margins too large
> par(mar = c(1, 1, 1, 1))
>
> par(mfrow=c(2,1))
>
> #plot DFBETAS for disp with threshold lines
> plot(dfbetas$disp, type='h')
> abline(h = thresh, lty = 2)
> abline(h = -thresh, lty = 2)
>
> #plot DFBETAS for hp with threshold lines
> plot(dfbetas$hp, type='h')
> abline(h = thresh, lty = 2)
> abline(h = -thresh, lty = 2)
> graphics.off()
>
>
>
> par(mfrow=c(2,1))
>
> #plot DFBETAS for disp with threshold lines
> plot(dfbetas$disp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
> #plot DFBETAS for hp with threshold lines
> plot(dfbetas$hp, type='h')
Error in plot.new() : figure margins too large
> abline(h = thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
> abline(h = -thresh, lty = 2)
Error in int_abline(a = a, b = b, h = h, v = v, untf = untf, ...) :
plot.new has not been called yet
>
>
> #create data frame
> df <- data.frame(hours=c(1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6),
+ score=c(67, 65, 68, 77, 73, 79, 81, 88, 80, 67, 84, 93, 90, 91))
>
> #fit linear regression model
> model = lm(score ~ hours, data=df)
> #produce diagnostic plots for regression model
> plot(model)
Hit <Return> to see next plot:
Error in plot.new() : figure margins too large
>
>
> par(mar = c(1, 1, 1, 1))
> #produce diagnostic plots for regression model
> plot(model)
Hit <Return> to see next plot:
Hit <Return> to see next plot:
>
>
> #produce diagnostic plots for regression model
> plot(model)
Hit <Return> to see next plot:
Hit <Return> to see next plot:
>
>
>
>
>
>
> par(mar = c(1, 1, 1, 1))
> par(mfrow=c(2,2))
> #produce diagnostic plots for regression model
> plot(model)
>
>
> #fit simple linear regression model
> model <- lm(Ozone ~ Temp, data = airquality)
>
> #produce scale-location plot
> plot(model)
> #fit simple linear regression model
> model <- lm(Ozone ~ Temp, data = airquality)
>
> #produce scale-location plot
> plot(model)
>
> par(mfrow=c(1,1))
> #fit simple linear regression model
> model <- lm(Ozone ~ Temp, data = airquality)
>
> #produce scale-location plot
> plot(model)
Hit <Return> to see next plot:
Hit <Return> to see next plot:
Hit <Return> to see next plot:
Hit <Return> to see next plot:
>
>
>
> #load lmtest package
> library(lmtest)
载入需要的程辑包:zoo
载入程辑包:‘zoo’
The following objects are masked from ‘package:base’:
as.Date, as.Date.numeric
载入程辑包:‘lmtest’
The following object is masked from ‘package:rms’:
lrtest
>
> #perform Breusch-Pagan Test
> bptest(model)
studentized Breusch-Pagan test
data: model
BP = 1.4798, df = 1, p-value = 0.2238
> #load the dataset
> data(mtcars)
>
> #fit a regression model
> model <- lm(mpg~disp+hp, data=mtcars)
>
> #get list of residuals
> res <- resid(model)
> #produce residual vs. fitted plot
> plot(fitted(model), res)
>
> #add a horizontal line at 0
> abline(0,0)
>
>
> #create data
> df <- data.frame(x1=c(3, 4, 4, 5, 5, 6, 7, 8, 11, 12),
+ x2=c(6, 6, 7, 7, 8, 9, 11, 13, 14, 14),
+ y=c(22, 24, 24, 25, 25, 27, 29, 31, 32, 36))
>
> #fit multiple linear regression model
> model <- lm(y ~ x1 + x2, data=df)
>
>
> #define new observation
> new <- data.frame(x_1=c(5),
+ x_2=c(10))
>
> #use the fitted model to predict the value for the new observation
> predict(model, newdata = new)
Error in eval(predvars, data, env) : object 'x1' not found
参考:R
参考:How to Predict a Single Value Using a Regression Model in R
最后
以上就是神勇仙人掌为你收集整理的Error in eval(predvars, data, env) : object ‘**‘ not foundError in eval(predvars, data, env) : object '**' not found的全部内容,希望文章能够帮你解决Error in eval(predvars, data, env) : object ‘**‘ not foundError in eval(predvars, data, env) : object '**' not found所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复