Introduction to Excel Lecturer Notes Nilgün Çokça ECO319 Computer Applications and Data Analysis I EXCEL FOR ECONOMICS Linear Programming Consider the problem of nutrition in its economic aspects. A consumer buys a range of foods: breads, meat, milk and so on. In determining his purchases, e.g. given prices on the market and his income, his “preferences” for the various foods are taken into account, as in the usual theory of consumer behavior. What are these “preferences”? In part, they are his tastes in food and his habits in consumption. But, in part, they may also be an expression of his nutritional needs in terms of calories, protein, vitamins and so on. The problem is one of transformation: the foods bought at market prices are transformed into intake of nutrients. There are two sides to the problem: the consumers’ diet is specified as to aggregate content of calories, grams of protein, and so on; foods are then bought at given market prices to minimize the cost of achieving the given nutritional content. The given technical data of the problem consist of the specification of the nutrients in one unit of each food; this is a transformation function, here assumed can be achieved in all kinds of ways by buying foods in various quantities. A feasible solution of the problem is any such set of purchases. The problem is then resolved, and an optimum feasible solution obtained, if the set of purchases for smallest cost is found. Example: An airway firm has two different types of planes. DK-1 and BW-10. DK-1 can carry 40 passengers, 30 tons cargo. BW-10 can carry 60 passengers, 15 tons cargo. Airway firm has signed an agreement to carry minimum 480 passengers and minimum of 180 tons cargo for everyday. Fly cost for DK-1 is 500$ and for BW-10 is 600$. For minimum total cost how many flight must be done with each plane? Our target is minimizing the cost and while minimizing it we have restricts. [2] DK-1 → X BW-10 → Y Z=500X+600Y 40X+60Y 480 passenger X0 30X+15Y 180 cargos Y0 Steps for solving in Excel (1) Type X= in C1 Y= in C2 (2) =40*D1+60*D2 into C4 (3) =30*D1+15*D2 into C5 (4) =500*D1+600*D2 Z= in E1 into G1 D1 represents X; D2 represents Y. Select Solver from Tools menu. See the results X=3 in D1 Y=6 in D2 Z=5100 in G1 Cost of DK-1 = 480 in C4 Cost of BW-10 = 180 in C5. [3] Adding Solver Commands to Data menu Office button ~ Excel Options (in 2007) File menu ~ Options (in 2010) Select Add-Ins [4] Click to Go… Click to Solver and then OK [5] Example 1. Transporting Refrigerators Suppose GE wishes to determine a shipping schedule to deliver 3000 side-by-side refrigerators from two plants to three regional warehouses. The cost of shipping a refrigerator (via truck) from each plant to each warehouse is given here: To Warehouse From Plant 1 2 3 1 10 15 13 2 8 20 11 Plant 1 can supply 1.200 of the total demand for refrigerators, and Plant 2 can supply 1.800. If each of the three warehouses requires 1.000 refrigerators, what is the optimal shipping plan? To solve this transportation problem as a LP model requires us to define the decision variables to be the amount shipped from each plant to each location. It will be convenient to use a bit different notation for this model; using subscripts to keep track of plants and warehouses Xi,J will denote the amount shipped from Plant i to Warehouse j. For example: X Plant1 to Warehouse1: X11 = Amount shipped from Plant1 to Warehouse1 X Plant2 to Warehouse3: X23 = Amount shipped from Plant2 to Warehouse3 This model requires six decision variables. In the following model, you see that the specification becomes a bit tedious with so many variables. To make the formulation simpler we have not listed all of the non-negativity constraints together on one line. 𝑍 = 10𝑋11 15𝑋12 13𝑋13 8𝑋21 20𝑋22 11𝑋23 (𝑡𝑜𝑏𝑒𝑚𝑖𝑛𝑖𝑚𝑖𝑧𝑒𝑑) (𝑃𝑙𝑎𝑛𝑡 1 𝐶𝑎𝑝𝑎𝑐𝑖𝑡𝑦) 1𝑋11 +𝑋12 +𝑋13 ≤ 1.200 (𝑃𝑙𝑎𝑛𝑡 2 𝐶𝑎𝑝𝑎𝑐𝑖𝑡𝑦) 1𝑋21 +𝑋22 +𝑋23 ≤ 1.800 (𝑊𝑎𝑟𝑒ℎ𝑜𝑢𝑠𝑒 1 𝐶𝑎𝑝𝑎𝑐𝑖𝑡𝑦) 1𝑋11 +𝑋12 = 1.000 (𝑊𝑎𝑟𝑒ℎ𝑜𝑢𝑠𝑒 2 𝐶𝑎𝑝𝑎𝑐𝑖𝑡𝑦) 1𝑋12 +𝑋22 = 1.000 (𝑊𝑎𝑟𝑒ℎ𝑜𝑢𝑠𝑒 3 𝐶𝑎𝑝𝑎𝑐𝑖𝑡𝑦) 1𝑋31 +𝑋32 = 1.000 (𝑁𝑜𝑛 − 𝑛𝑒𝑔𝑎𝑡𝑖𝑣𝑖𝑡𝑦𝑐𝑜𝑛𝑠𝑡𝑟𝑎𝑖𝑛𝑡𝑠) 𝑋11 , 𝑋12 , 𝑋13 , 𝑋21 , 𝑋22 , 𝑋23 ≥ 0 [6] [7] Example 2.A farmer has 100 acres on which to plant two crops: corn and wheat. To produce these crops, there are certain expenses, as shown in the table: Item Cost per acre Item Corn Cost per acre Wheat Seed $ 12 Seed $ 40 Fertilizer $ 58 Fertilizer $ 80 Planting/care/harvesting $ 50 Planting/care/harvesting $ 90 Total $ 120 Total $ 210 After the harvest, the farmer must usually store the crops while awaiting favorable market conditions. Each acre yields an average of 110 bushels of corn or 30 bushels of wheat. The limitations of resources are Available capital: $ 15.000 Available storage facilities: 4000 bushels If the net harvest profit (after all expenses have been subtracted) per bushel of corn is $ 1,30 and for wheat is $ 2,00, how should the farmer plant the 100 acres to maximize profits? X: # of acres of Corn Y: # of acres of Wheat P: Profit X Y Constraints Acres x y ≤100 Expenses 120x 210y ≤15.000 Yield 110x 30y ≤4.000 P 143x 60 Mathematical Form: 𝑋 + 𝑌 ≤ 100 120𝑋 + 210𝑌 ≤ 15.000 110𝑋 + 30𝑌 ≤ 4.000 (110𝑥1,30)𝑋 + (30𝑥2)𝑌 → 𝑚𝑎𝑥. [8] [9] Example 3.A company making animal feed uses two ingredients (X and Y) in one of their best-selling products. A decision must be made regarding the amount of X and Y (measured in tons) to include in the mix of this product to minimize total cost. Ingredient X costs the company 60$ per ton, and ingredient Y costs 20$ per ton. The constraints on the mix each day follow: 1. Company policy is that the mix must have an average of 35 units per ton of a nutritional ingredient. Ingredient X provides 42 units per ton of this ingredient, whereas ingredient Y provides 30 units per ton. 2. The company must make at least 13 tons each day: hence they consider X+Y13 to be constraint. A decision must be made regarding the amount of X and Y (measured in tons) to include in the mix of this product to minimize total cost. In formulating the LP model for this problem, some may have difficulty specifying the first constraint. This constraint says that the sums of 42X and 30Y must at least equal 35(X+Y). Thus, the first constraint can be written in either of the following two ways: 42X+30Y ≥ 35(X+Y) or 7X – 5Y ≥ 0 Constraints are: X+Y ≥ 13 42X+30Y ≥ 35(X+Y) → 7X – 5Y ≥ 0 Target: Z = 60X+20Y → Minimum Cost [10] #REF “Illegal Cell Reference” The #REF! error occurs when you refer to a call that is not valid. The error is displayed whenever Excel is unable to evaluate a formula because of an illegal cell reference. The most common cause of the error is deleting the row, column, or all that contained the original cell reference. [11] Example 4. Transco, Inc. makes a deluxe and a standard model of a Global Positioning System (GPS) –a handle device for navigating via a system of 24 Satellites. They are currently trying to determine the optimal number of each model to produce each day. Their profit is $ 120 for each deluxe model and $ 80 for each standard model. Transco has 80 working-hours per day available (that is, 10 people working 8-hours shifts per day) producing a deluxe model takes 2 hours, whereas producing a standard model takes 1 hour. No more than 30 deluxe models per day can be produced, and company policy is that the number of deluxe models produced must be at least half of the number of standard models. (For example, if 20 standard models are produced, the number of deluxe models must be at least 10.) The company can sell all of the GPS models it produces. X: Number of deluxe models sold Y: Number of standard models sold Z= $120X+$80Y 1. Hours: 2X+1Y ≤ 80 2. # Deluxe: X ≤ 30 3. Mix of deluxe and standard Policy: X ≥ 1/2Y → 4. X , Y ≥ 0 [12] ½ Y –X ≤ 0 # of deluxe sold : 20 # of standard sold : 40 Profit : 5600 $ [13] Pivot Tables A pivot table extends the capability of individual database functions by presenting the data in summary form. It divides the records in a list into categories, then computes summary statistics for those categories. A pivot table is created by using the Pivot Table Wizard, which prompts you for the information to develop the pivot table. You indicate the field names for the row and column labels. You also indicate the field on which the computation is to be based and means of computation. The Pivot Table Wizard does the rest. It creates the pivot table in its own worksheet within the same Workbook as the list on which it is based. Pivot tables provide the almost in flexibility, in that you can vary the row or column categories and/or the way the statistics are computed. The pivot table dialog box shows just how easy it is to create a pivot table. The field names within the associated list appear at the right of the dialog box. To create the pivot table, you simply drag a field name(s) to the row, column, or data areas of the table. Click the Next command button to supply the finishing touches to the pivot table, after which the pivot table will appear in its own worksheet. Once a pivot table has been created, you can easily add or remove categories by dragging the field names on or off the table within the PivotTable Wizard. You can also switch the orientation (pivot the table) by dragging a field to or from the row or column area. And finally, you can [14] modify the worksheet on which the pivot table is based then refresh the pivot table to reflect the changes made to the worksheet. Cross-Tabulation: Suppose you have been looking at a university department’s web page which lists staff alphabetically by name, and gives an indication of their position in the department ( showing who is a Professor, a senior lecturer or ordinary lecturer). You notice that of the twenty staff listed six are women, but only one of the four Professors is a woman. You decide to investigate the relationship between position and gender more systematically. You enter the person’s name, gender and position into a spreadsheet as shown in Figure What you need to do is to draw up a table, known as a contingency table, which has two columns to distinguish the gender of the individuals and three rows to distinguish the staff according to their position or status. The table would show the number of cases falling into each two-way category. Now with a small data set such as we have here it is easy enough just to count up the cases manually. Excel steps for count up gender and position for staffs. Select through B1:C21 then Data Menu Pivot Table wizard. [15] Put Gender to column and Position to row, and again Position to data. E1, an area of the worksheet where you would like the table to appear. What is a Pivot Table? Pivot tables are an Excel feature that you should learn how to use. Instead of analyzing rows upon rows of records, a pivot table can aggregate your data and show a new perspective and few clicks. You can also move columns to rows or vice versa. The problem is people believe creating a pivot table is too difficult to learn. You might think of a pivot table as a user created summary table of your original spreadsheet. You create the table by defining which fields to view and how the data should be displayed. Based on your field selections, Excel aggregates and organizes the data so you see a different view of your data. As example, I’ve uploaded a sample spreadsheet of 4000 fictitious voters, which includes the following data fields: • Voter ID • Party Affiliation • Their precinct • Age group [16] • When they last voted • Years they’ve been registered • Ballot status Looking at the first 20 data records, you can see the data is boring. It’s enough to make you roll your eyes and fall asleep. In this format, the key question it answers is how many voters exist in all the precincts. Using Excel pivot tables, you can organize and group the same data in ways that start to answer questions such as: • What is the party breakdown by precinct? • Do voters use permanent absentee ballots? [17] • Which precincts have the most Democrats? • How many voter pamphlets do I need for Precinct 2416? • Do 18-21 year olds vote? A pivot tables allow you to group the spreadsheet or external data source by any of your data fields. The thumbnail below shows a count of voters by party by precinct. Using a pivot table, I can continue to slice the data by selecting additional fields from the PivotTable Field List. For example, I can take the same data and segment by voter age group. [18] Understanding the Pivot Table Structure In the thumbnail above, I’ve labeled the main areas of the pivot table. 1. PivotTable Field List – this section in the top right displays the fields in your spreadsheet. You may check a field or drag it to a quadrant in the lower portion. 2. The lower right quadrants - this area defines where and how the data shows on your pivot table. You can have a field show in either a column or row. You may also indicate if the data should be counted, summed, averaged, filtered and so on. 3. The red outlined area to the left is the result of your selections from (1) and (2). You’ll see that the only difference I made in the last pivot table was to drag the AGE GROUP field underneath the PRECINCT field in the Row Labels quadrant. How to Create a Pivot Table There are several ways to build a pivot table. Excel has logic that knows the field type and will try to place it in the correct row or column if you check the box. For example, data that is numeric such as Precinct counts tends to appear to the right in columns. Data, which is textual, such as Party would appear in rows. While you can simply check fields to display and let Excel build your pivot table, I prefer to use the “drag and drop” method. This is partly because I like to visualize my data in columns and rows. I think it may also be easier if you have fields, which can appear to be numbers like a precinct value. • Open your original spreadsheet and remove any blank rows or columns. • Make sure each column has a heading, as it will be carried over to the Field List. • Make sure your cells are properly formatted for their data type. • Highlight your data range, Click the Insert tab. • Select the PivotTable button from the Tables group. • Select PivotTable from the list. [19] The Create PivotTable dialog appears. • Double-check your Table/Range: value. • Select the radio button for New Worksheet. • Click OK. A new worksheet opens with a blank pivot table. You’ll see that the fields from our source spreadsheet were carried over to the PivotTable Field List [20] • Drag an item such as PRECINCT from the PivotTable Field List down to the Row Labels quadrant. The left side of your Excel spreadsheet should show a row for each precinct value. You should also see a checkmark appear next to PRECINCT • The next step is to ask what you would like to know about each precinct. I’ll drag the PARTY field from the PivotTable Field List to the Column Labels quadrant. This will provide an additional column for each party. Note that you won’t see any numerical data [21] • To see the count for each party, I need to drag the same field to the Values quadrant. In this case, Excel determines I want a Count of PARTY. I could double-click the entry and choose another Field Setting. Excel has also added Grand Totals. As you build your pivot table, you’ll probably think of additional ways to group the information. For example, you might want to know the Age Range of voters by Precinct by Party. In this [22] case, I would drag the AGE GROUP column from the PivotTable Field List down below the PRECINCT value in Row Labels. Each age group is broken out and indented by precinct. At this stage, you might also be thinking of usability. As with a regular spreadsheet, you may manipulate the fields. For example, you might want to rename “Grand Total” to “Total” or even collapse the age values for one or more precincts. You can also hide or show rows and columns. These features work the same way as a regular spreadsheet. One area that is different is the pivot table has its own options. You can access these options by right-clicking a cell within and selecting PivotTable Options… For example, you might only want Grand Totals for columns and not rows. There are also ways to filter the data using the controls next to Row Labels or Column labels on the pivot table. You may also drag fields to the Report Filter quadrant. Troubleshooting Pivot Tables You might encounter several “gotchas” with this example file or another spreadsheet. Sometimes when you move around your pivot table the PivotTable Field List disappears. To get it back, click any cell with a value. [23] You can also move or “pivot” your data by right clicking a data field on the table and selecting the “Move” menu. From here, you can move a column to a row or even change the position. An example of this might be the values for “LAST VOTED” since Excel will sort by the month first. You might prefer to move the data so the election dates are in a chronological order. Pivot tables may not make the election data exciting, but it can make the analysis process easier. Without these tables, you’d probably spend more time filtering, sorting and subtotaling. The other benefit is that it’s easy to start over by deselecting fields or moving them to another location. Feel free to download the tutorial spreadsheet below and play with the data. This will probably be the only time you’re allowed to manipulate election data. [24] An Example for Pivot Table Staffs in a company have been sent a questionnaire asking them whether or not have received any IT training. The questionnaire also asks respondents to indicate their gender, status in the company (junior or senior) and their employment mode (Full-time or Part-time). The results for one branch are shown in the table below: Use PivotTable Wizard to produce contingency tables showing IT training responses crosstabulated against each of the other variables. [25]